Creation of a JAVA string object

Source: Internet
Author: User

The creation of a string object is special, and ordinary object creation is created in the heap, and the string object does not necessarily, see a piece of code below

public class StringTest1 {public static void main (string[] args) throws exception{string a = "abc"; String B = "abc"; String C = "AB"; String d = new String ("AB"); System.out.println ("a=" + a);//a=abcsystem.out.println ("b=" + b);//b=abcsystem.out.println ("c=" + C);//c=  AbSystem.out.println ("d=" + D);//d=absystem.out.println ("A==b is" + (a = = b));//a==b is TrueSystem.out.println ("C==d is" + (c = = d);//c==d is Falsefield field = String.class.getDeclaredField ("value"); Field.setaccessible (true); char[] Valuea = (char[]) field.get (a); char[] Valueb = (char[]) field.get (b); char[] Valuec = (char[]) field.get (c); char[] valued = (char[ ]) Field.get (d); System.out.println ("A.value (" + valuea.hashcode () + ") =" + arrays.tostring (Valuea));//a.value ([1829164700) =[a, B, c] System.out.println ("B.value (" + valueb.hashcode () + ") =" + arrays.tostring (VALUEB));//b.value ([1829164700) =[a, B, c] System.out.println ("C.value (" + valuec.hashcode () + ") =" + arrays.tostring (VALUEC));//c.value ([2018699554] =[a, b] System.ouT.println ("D.value (" + valued.hashcode () + ") =" + arrays.tostring (valued) ")//d.value ([2018699554] =[a, b]valuea[0] = ' Z ' ; valued[0] = ' x '; System.out.println ("a=" + a);//a=zbcsystem.out.println ("b=" + b);//b=zbcsystem.out.println ("c=" + C);//c= XbSystem.out.println ("d=" + D);//D=XB}}


A and B are all "ABC", the test result A and B reference is equal, the value array is equal, change the first bit of a, B also changed.

C and D are "AB", but two references are not equal, D is created with new, this time the object is created in the heap, a,b,c three references are directly to the constant pool object, but the following C and D's value seems to be the same array, change the first bit of D, C also changed, Indicates that D objects created in the heap are still pointing to a constant pool, so you create more objects with new.


There is also a classic question, String a = "a" + "B", and several objects have been created.

Generally it should be 3, a "a", a "B", a result "AB", A simple class is written below

public class StringTest2 {@SuppressWarnings ("unused") public static void main (string[] args) {String a = "a" + "B";}}

Use JAVAP to look at bytecode

Javap-c stringtest2.classcompiled from the "Stringtest2.java" public class test. StringTest2 {public  test. StringTest2 ();    Code:       0:aload_0       1:invokespecial #8                  //Method java/lang/object. " <init> ":() V       4:return public  static void Main (java.lang.string[]);    Code:       0:ldc           #16                 //String ab       2:astore_1       3:return}

There's only one directive. LDC #16, constant #16 from the constant pool, it is clear that this constant is AB, I do not parse the class file, it should be true, the direct anti-compilation, get the result is

Package Test;public class stringtest2{public  static void Main (string[] args)  {    String a = "AB";}  }

So the reference point directly to the constant pool "ab", just an object, it might be the result of compiler optimization, if there is time to separate the class file of one byte a byte to parse out the result should be more clear.

Creation of a JAVA string object

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.