Java string First-recognition __java

Source: Internet
Author: User

One, strings (string)

1. In Java, strings are processed as objects of type string. The String class is located in the Java.lang package. By default, the package is automatically imported into all programs.

2, how to initialize the string

Defines a string
S1 = "Java";

Creates an empty string
s2 = new string ();

Creates an empty string and assigns a
string s3 = new String ("Com.lemon");

3, the invariance of the string: string objects can not be modified after creation, is immutable, the so-called modification is actually created a new object, pointing to different memory space.

4. Once a string is created in memory, the string will not be changed. If you need a string that can be changed, we can use StringBuffer or StringBuilder (as we'll find in later chapters).

5, each time new a string is to produce a new object, even if the contents of two strings are the same, use "= =" compared to "false", if only to compare the same content, you should use the "Equals ()" method

Example:

        String S01 = "Java";
        String S02 = "Java";
        String S03 = "I love" + S01;
        String s4 = "I Love" + S01;


        Comparing strings S01 and S02
        //IMOOC is a constant string, which is optimized by the compiler when it occurs multiple times, creating only one object, True
        System.out.println ("S01 and S02 memory addresses are the same." "+ (S01 = = S02));

        Compare string S01 and S03  false
        System.out.println ("S01 and S03 memory addresses the same. "+   (S01 = = S03)              );

        S01 is a variable, S04 knows the specific value at run time, so S03 and S04 are different objects  false
        System.out.println ("S03 and S04 memory addresses the same." "+ (S4 = = S03));


        Analogy
        String object1 = new String ("OC");
        String object2 = new String ("OC");
        String object3 = "OC";
        = = comparison is the memory address of the referencing object false
        System.out.println (Object1 = = object2);
        = = compares the memory address of two referenced objects false
        System.out.println (Object1 = = object3);  equals comparison value True
        System.out.println (Object1.equals (object2));

Demo

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.