Deep understanding of string in Java

Source: Internet
Author: User

The structure of a string in memory

Note: The variable name declared by the string class is placed in the stack, the new object is placed in the heap, and the string concrete constant class is placed in the constant pool

B. String is an immutable object

the Java.lang.String class uses the final decoration and cannot be inherited. A string is a constant whose object can no longer be changed once it is constructed.

 package   Com.yyx.pratice;  public  class   teststring { public  static  void   main (string[] args) {S        Tring str1  = "Hello World" ;        System.out.println (STR1);        String str2  = upcase (STR1);        System.out.println (STR2);    System.out.println (STR1);  public  static   string upcase (string s) { return      S.touppercase (); }}

Iii. number of objects created

1.String a = new String ("1" + "2") build up several objects?

2 objects were created, because the compiler would merge the right side of the equals sign, so he would create a "12" string in the constant area, and then, creating an object on the heap called a

2.String str=new String ("abc") how many objects do you build?

2 objects created

Deep understanding of string in Java

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.