"Getting Started with Java" the first quarter and the like (string strings cannot be changed once they are assigned)

Source: Internet
Author: User

There is no doubt that the string class is one of the most important classes in Java. So it has many ways to understand and master.

Once a string has been assigned, the value cannot be changed:

Package cn.itcast_02;/* * String features: Once assigned, it cannot be changed. */public class Stringdemo {public static void main (string[] args) {string s = "Hello";//hello is staying in the string constant pool. After the assignment of Hello, it can't be changed. s + = "world"; System.out.println ("s:" + s); HelloWorld}} opens up three spaces throughout the process.
Look at the assignment process with a picture.

/* * String s = new string ("Hello") and string s = "Hello"; the former creates 2 objects (Hello creates an object in a constant pool, then creates it in heap memory (new), and then the new object points to the string constant pool , which creates 1 objects that   directly point to a string constant pool. *  = =: Comparing the reference type is the same as the address value * equals: Comparing the reference type by default is also the same as comparing the address value, and the string class overrides the Equals () method, comparing whether the content is the same. */public class StringDemo2 {public static void main (string[] args) {string S1 = new String ("Hello"); String s2 = "Hello"; System.out.println (S1 = = s2);//FalseSystem.out.println (s1.equals (S2));//True}}

/* * See Program Write Results */public class StringDemo3 {public static void main (string[] args) {String S1 = new String ("Hello"); String s2 = new string ("Hello"); System.out.println (S1 = = s2);//FalseSystem.out.println (s1.equals (S2));//truestring S3 = new String ("Hello"); String s4 = "Hello"; System.out.println (s3 = = S4);//FalseSystem.out.println (S3.equals (S4));//truestring S5 = "Hello"; String s6 = "Hello"; System.out.println (S5 = = s6);//TrueSystem.out.println (S5.equals (S6));//True}}



String in the end to create a few object problems, and then do in detail later in the topic.


"Getting Started with Java" the first quarter and the like (string strings cannot be changed once they are assigned)

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.