Java String
Posted rysz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java String相关的知识,希望对你有一定的参考价值。
部分整理自Stack Overflow:
- length()
- indexOf()
- isEmpty()
- charAt(i)
- 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.
- 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.
以上是关于Java String的主要内容,如果未能解决你的问题,请参考以下文章
11.按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的(代码片段
按要求编写Java应用程序。 创建一个叫做机动车的类: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)减速(车速自减)修改车牌号,查询车的载重量(代码片段