Java String

Posted rysz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java String相关的知识,希望对你有一定的参考价值。

部分整理自Stack Overflow:

Java String:
  1. length()
  2. indexOf()
  3. isEmpty()
  4. charAt(i)
  5. startsWith() — used for checking prefix of a String, returns a boolean value true or false based on whether the specified string is prefix of the particular String or not.
  6. Substring(x,y)

Q: I have some question that I wonder about. I know that string are immutable in Java and therefore a new string object is created rather than changed when for example assigning to a existing string object. 

Now to my question. Let‘s suppose that I have the following piece of code:

  String a = "Hello World";

  String b = "Hello World";

  String res = a.substring(0,4) + b.substring(6,10);

How many string objects will be created by the code at line 3 ? Will each call to substring create a new string object ? Will my code above generate 3 new string objects ?

 

A:

Strings in Java are immutable. Basically this means that, once you create a string object, you won‘t be able to modify/change the content of a string. As a result, if you perform any manipulation on a string object which "appears to" change the content of the string, Java creates a new string object, and performs the manipulation on the newly created one.

Based on this, your code above appears to create five string objects - two are created by the declaration, two are created by calls to substring, and the last one is created after you concatenate the two pieces.

Immutability however leads to another interesting consequence. JVM internally maintains something like a string pool for creating string literals. For saving up memory, JVM will try to use string objects from this pool. Whenever you create a new string literal, JVM will loop into the pool to see if any existing strings can be used. If there is, JVM will simply use it and return it.

So, technically, before Java 7, JVM will create only one string object for your whole code. Even your substring calls won‘t create new string objects in the pool, it will use the existing "Hello World" one, but in this case it will only use characters from position 0 to 3 for your first call to substring, for example. Starting from Java 7, substring will not share the characters, but will create a new one. So, total object count will be 4 - the last one will be created with the concatenation of the two substrings.

 

In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by ‘u0000‘ (the NUL character).

A String object is immutable, that is, its contents never change, while an array of char has mutable elements.

The method toCharArray in class String returns an array of characters containing the same character sequence as a String. The class StringBuffer implements useful methods on mutable arrays of characters.

 

So, no, char arrays are not immutable in Java, they are mutable.

 

 
Leetcode 14  — longest common prefix: 
        method1: horizontal scanning LCP(S1…Sn)=LCP(LCP(LCP(S1,S2),S3),…Sn).
        method2: vertical scanning
        method3: divide and conquer ?
        method4: binary search 先找到最短的string字符数minLen,二分法, 先看0~minlen/2 个字符是否都一样,若一样则prefix字符数+1再查,否则prefix字符数-1再查
        Method5: Arrays.sort方法对对象数组按照自然顺序进行排序,比较第一个和最后一个string
 

以上是关于Java String的主要内容,如果未能解决你的问题,请参考以下文章

Jsp获取Java的重定向赋值(String)

java Ftp上传创建多层文件的代码片段

11.按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的(代码片段

按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的载重量(代码片段

20个非常有用的Java程序片段

Java语言基础之方法的设计