码迷,mamicode.com
首页 > 编程语言 > 详细

Java String

时间:2019-03-17 13:57:23      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:ever   substr   and   hat   com   sort   win   follow   ast   

部分整理自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

标签:ever   substr   and   hat   com   sort   win   follow   ast   

原文地址:https://www.cnblogs.com/rySZ/p/10546256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!