String type in Java (don't know how to understand, ask the great God)

Source: Internet
Author: User

When executing string a = "abc";

Time (the precondition is: there is no ABC in the constant pool before executing this line of code, if any, a direct reference in the constant pool ABC does not create an object, if none, execute the following description)



Java Virtual Opportunity creates a char-type value ' A ', ' B ', ' C ' in the stack, and then creates a string in the heap

Object, whose value is the three char type value that you just created in the stack, consists of the array {' A ', ' B ', ' C '}, and finally the newly created string

The object is added to the string pool.

String b = new String ("abc");

, you can create at least one object, or 2

Because the new keyword is used, a string object of B is created in the heap, whose value is "abc", and if the string "ABC" does not exist in the Java string pool, a string object is created in the Java string pool (" ABC ")


String str1 = new String ("ABC"); String str2 = new String ("ABC"); str1 = = is the value of str2 true or false? False.string STR3 = "ABC"; String STR4 = "ABC"; String STR5 = "A" + "BC"; str3 = = is the value of STR4 true or false? TRUE.STR3 = = is the value of STR5 true or false? True. When writing code, do not normally string str2 = new String ("ABC"); String a = "ABC"; String b= "AB"; String c=b+ "C"; System.out.println (A==C); Both Falsea and B are string constants, so they are determined at compile time! A b in C is a reference not a string constant, so it is not determined at compile time.
What is the most efficient way to int to string?
int i = 123;
String s = "";
The first method: s=i+ "";//generates 2 string objects
The second method: s = string.valueof (i); directly using the static method of the String class, producing only one object


String type in Java (don't know how to understand, ask the great God)

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.