Strings in Java

Source: Internet
Author: User

string Constants in Java

The string in Java is immutable, of course, with string constants, such as:

publicstaticfinal String a="123";

Is different, here refers to a can only point to the string 123 , and what we mean today is immutable refers to the string in memory is immutable, and now we look at an example:

package com.test;/** * Created by siege on 2015-08-02. */publicclass TestString {    publicstaticvoidmain(String[] args) {        String a="0";        for (int1; i <10 ; i++) {            a+=i;            System.out.println(a);        }    }}

The output is:

01
012
0123
01234
012345
0123456
01234567
012345678
0123456789

Of course, the result is not strange, on the surface, the value of a has been changing, as if a originally pointed to the "0" became "0123456789" , in fact, is not so, in fact, the above-mentioned string is in memory, but a reference has been changing.

In fact, there is a string pool in Java (string pools, maintained by the JVM), and when we assign a string to variable a (such as "0"), first the JVM will look for the string "0" in the string pool, if any, So directly return the string in string pool, so that a point to the reference, if not, then create the string, and then return the string reference to a, for example:

package com.test;/** * Created by siege on 2015-08-02. */publicclass TestString {    publicstaticvoidmain(String[] args) {       String a="hello";        String b="hello";        System.out.println(a==b);    }}

As a result true , this shows that A and B point to a reference to the same string, but if we create this:

package com.test;/** * Created by siege on 2015-08-02. */publicclass TestString {    publicstaticvoidmain(String[] args) {       String a="hello";        String b=new String("hello");        System.out.println(a==b);    }}

The result is false that, for the reasons, the string objects created with new are present in the heap, so their addresses are different. Keep looking:

publicclass TestString {    publicstaticvoidmain(String[] args) {        String a="abc";        String b="edf";        String c=a+b;        System.out.println(c=="abcdef");    }}

As a result false , this shows that C is pointing to the "abcdef" in heap memory, and we continue to see:

publicclass TestString {    publicstaticvoidmain(String[] args) {        String a="abc";        String b="edf";        String c=a+b;        String d="abcdef";        System.out.println(c==d);    }}

The result is false that it proves that C is not pointing to the constant "abcdef" in the string pool, so it is necessarily pointing to the "abcdef" in the heap, further deepening, in fact, during compilation, the String a="abc" JVM has already put the value of the A variable "abc" Put into the string pool, string c=a+b can only know the value of C during run time, so it is created in the heap, there are two string objects created in the heap, A and B, they are assigning a value of a A, a, a, a, and a to the heap in the string pool. Create two objects in the heap, then build object C and "abcdef" assign the heap address to C.

publicclass TestString {    publicstaticvoidmain(String[] args) {        String a="abc"+"def";        System.out.println(a=="abcdef");    }}

The output is true that the JVM is "abcdef" put into the string pool.

publicclass TestString {    publicstaticvoidmain(String[] args) {        String a="abc";        String b=a+"def";        System.out.println(b=="abcdef");    }}

As a result false , this also shows that B is created in the heap.

publicclass TestString {    publicstaticvoidmain(String[] args) {        String a=new String("abc");        String b=a.intern();        String c="abc";        System.out.println(b==c);    }}

The Intern () method is to put a string of the A object created in the heap into string pool, but before putting it in, check if there is a change to the string, and then put it in the string pool without the words, and return its reference to B. Therefore, the above results are true .

The significance of setting string pool ' is to reduce the creation of the same content string, save memory space, and the disadvantage is that the string in the string pool is to be evaluated if it is already there.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Strings in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.