The substring() Method in JDK 6 and JDK 7
The substring() Method in JDK 6 and JDK 7 The substring(int beginIndex, int endIndex) method in JDK 6 and JDK 7 are different. Knowing the difference can help you better use them. For simplicity reasons, in the following substring() represent the substring(int beginIndex, int endIndex) method. 1. What substring() does? The substring(int beginIndex, int endIndex) method returns a string that starts with beginIndex and ends with endIndex-1. String x = "abcdef" ; x = x. substring ( 1 , 3 ) ; System . out . println ( x ) ; Output: bc 2. What happens when substring() is called? You may know that because x is immutable, when x is assigned with the result of x.substring(1,3), it points to a totally new string like the following: However, this diagram is not exactly right or it represents what really happens in the heap. What really happens when substring() is called is different between JDK 6 and JDK 7. 3. substring() in JDK 6 String is...