String type details in Java

Source: Internet
Author: User

String type details in Java A. String two types of initialization methods

1. String str1= "ABC";//string class-specific ways to create character objects, more efficient

Detects if "ABC" exists in the string buffer
If there is no duplicate creation, assign the address to str1.
If it does not exist, the object is created in the string buffer and the address is assigned to STR1.

2. String str1= new String ("abc"); Constructor initialization
Or
Char [] ch={' A ', ' B ', ' C '};
String Str1=new string (ch);
First the "ABC" object, and then copy the object created to the constructor (equivalent to str1 getting a copy of the constructor)

The string object is immutable, its contents cannot be changed, and the string is used frequently in the program, in order to improve efficiency, to use the same instance with the same string sequence 字符串直接量 , such an instance is called 限定的 (interned)

Note that the second way of argument only supports string literals or character array creation, this way is 错误 string stra = "ASD";
String STRBB = new Strint (stra);

Compare two ways of creating, the first more efficient, just one object created, and the second creating two objects.

Two. Initialization details

A reference to the base type and object is saved in the stack, and the base type will see if it already exists in the stack before it is created.

String str1= "ABC";
String str1= new String ("abc");

The former first creates a reference variable str1 in the stack, and then checks to see if there is "ABC" in the stack, and if not, put "ABC" in the stack and point the reference variable STR to it, and if so, direct str1 to it; the latter is the standard method of object creation in Java. The objects it creates are placed directly into the heap, and a new object is created each time it is called.

String str = "abc" + "Def";
What is the number of objects created by this statement? 1 of them.
The compiler calls StringBuilder's Append method itself to synthesize the abcdef, and then only generates an object.

In fact, the string literal is a constant, at compile time has been determined, two constants added, first detect whether there is "abcdef" in the stack memory if there is, point to the existing stack "abcdef" space, if not, then create.

Package Stringdemo;PublicClass stringinitial{PublicStaticvoidMain (string[] args) {String str1 ="ABC"; String str2 =New String ("ABC");String str2 = new string (new char[] {' A ', ' B ', ' C '});//string str2 = new String (str1), error notation System. out.println (str1 = = str2); //false System. out.println (Str1.equals (str2)); //true here the Equals () method has been overwritten, compared to the string is not the address string str3 =  "123"; String STR4 =  "abc123"; String STR5 =  "abc" +  "123"; String str = str1 + str3; System. out.println (STR4 = = STR5); //true System. out.println (STR4 = Str1+ "123"); out.println (STR4 = = str); //false}}            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
As you can see, a new object is generated in the heap as long as there is a non-string direct amount in the +, and the stack memory is not detected three. about String str=null; String str; String str= "";

String Str=null;
Declares a string reference variable and initializes it to null, and does not point to any address and takes up no space

String str;
Only a reference variable of string is declared, not initialized (there will be a default implicit initialization of Str=null as an object property), and if not later compiled with this variable will pass

String str= "";
Normal string initialization, except that the string contents are empty.

String type details 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.