Java String Explanation

Source: Internet
Author: User
Tags object object

    1. String explanation

Package Com.yuanzijian02;public class Objecttest {public static void main (string[] args) {//TODO auto-generated method Stub Object object = New Object (), Object object2 = new Object ();            System.out.println (Object = = Object2); System.out.println ("---------------------"); String str = new String ("AAA"); String str2 = new String ("AAA"); System.out.println (str = = STR2); System.out.println ("---------------------"); String STR3 = "BBB"; String STR4 = "BBB"; System.out.println (STR3 = = STR4); System.out.println ("---------------------"); String STR5 = "CCC"; String STR6 = new String ("CCC"); System.out.println (STR5 = = STR6);}}

Execution results

False---------------------False---------------------true---------------------false

2.equals () method, which is defined in the object class, so that every class in Java has that method, and for Equals () of the object class, it is the same as the reference that invokes the Eqals () method to pass in. That is, whether the two references point to the same object. For the Equals () method of the object class, it is equivalent to = =

Package com.yuanzijian03;public class stringtest {public static void main ( String[] args)  {// TODO  Auto-generated method stub string str = new string ("AAA"); String str2 = new string ("AAA");         System.out.println (Str.equals (str2));                 String str3 =  "AAA";         string str4 =  "AAA";         system.out.println ( Str3.equals (STR4));                 object object = new object ();         object  object2 = new object ();         system.out.println (Object.Equals (Object2));}}

Execution results

Truetruefalse

3. For the Equals method of the string class, it is to determine whether the current string is consistent with the contents of the string passed in

4. For the string object equality judgment, use the Equals () method instead of the = =

5. Override the Equals method to determine whether two strings are equal

Package Com.yuanzijian04;public class Equalstest {public static void main (string[] args) {//TODO auto-generated method stub student S1 = NE W Student ("Zhangsan"); Student s2 = new Student ("Zhangsan"); System.out.println (S1 = = s2); System.out.println (s1.equals (S2));}} Class student{string Name;public Student (String name) {this.name = name;} public boolean equals (Object anobject) {if (this = = AnObject) {return true;} if (anobject instanceof Student) {Student Student = (Student) anobject;if (Student.name.equals (this.name)) {return true;}} return false;}}

Execution results

Falsetrue

6.String is a constant, and once the object has been created, it cannot be changed, and when we use the plus sign to stitch the string, a new string object is generated instead of appending the content to the original string object

Package Com.yuanzijian05;public class StringTest2 {public static void main (string[] args) {//TODO auto-generated method stub String S1 = "H Ello "; String s2 = "World"; String s3 = s1 + s2; System.out.println (S3);}}

Execution results

HelloWorld

7.String pool, first check that the string pool has no current object, and if so, it will not add the creation object to the string pool, but return the string object directly

8.String s = "AAA"; (see program in 1)

1. Look for the "AAA" object in the string pool, create an object in the string pool if it does not exist, and then return the address of the "AAA" object in the string pool to the reference variable s so that s points to the string The "AAA" in the pool

2. If present, do not create any objects, directly return the "AAA" object address in string pool, directly assign the value

9.String s = new String ("AAA"); (see Program in 1)

    1. First in the string pool to find there is no "AAA" This string object, if there is no longer in the string pool to create "AAA" object, directly in the heap to create an "AAA" string object, and then the heap of this "AAA" object address returned to the S reference , causing S to point to the "AAA" string object created in the heap

    2. 2. If not, first create an "AAA" object in the string pool, then create an "AAA" object in the heap and then return the address of the "AAA" object in the heap to the S reference, causing S to point to the "AAA" object created in the heap

Java String Explanation

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.