Overview of the String class

Source: Internet
Author: User

String: a common data type in programming. It is not a simple basic data type, but a composite data type.

Its biggest feature is that it cannot be modified or changed after creation.

It can be used without import, and this class does not need to be extended. It is defined as a final class (final class) and cannot be inherited by other classes.

 

 String s1 =  String("java01" String s2 = "java01";

The above two methods are used to create a String. The results are the same, but the second method is relatively simple.

The difference between the two creation methods:

S1 has two objects in memory (one is new and the other is string objects in heap memory)

S2 has only one object in the memory (this character object exists in the String constant pool)
S2 is a class type variable pointing to an object, and "java01" is an object. A string is a special object and a constant. It cannot be changed after initialization. All objects in double quotation marks are objects (exist in the constant pool)

 String s1 = "java01" s1 = "java02" System.out.println(s2);

The final result is java02, but not the java01 object changed, but the s1 point is changed to the java02 object. The java01 object still exists in the memory.

 

 String s1 =  String("java01" String s2 =  String("java01" System.out.println(s1==s2); System.out.println(s1.equals(s2));

= And equals are both compared to the memory address value of the object, but the equals method is rewritten by the String class and defines its own comparison method.

String s1 = "java01"=  String("java01"= "java01"==s2);System.out.println(s1==s3);

 

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.