Java base string string and object class

Source: Internet
Author: User

One, string strings

Q: The written question: New String ("ABC") created several objects?
A: Two objects, one object is in heap memory, one object is in a string constant pool

1  Public classDemo1 {2      Public Static voidMain (string[] args) {3String str1= "Hello";4String str2= "Hello";5String str3=NewString ("Hello");6String str4=NewString ("Hello");7System.out.println ("Str1==str2?") + (STR1==STR2) + "+++++++");//true8System.out.println ("Str2==str3?") + (STR2==STR3) + "-----");//false9System.out.println ("Str3==str4?") + (STR3==STR4) + "=====");//falseTenSystem.out.println ("Str3.equals (STR4)?" + (Str3.equals (STR4)));//true One     } A  -}

Conclusion:

1) for = =:
If the variable that is acting on the base data type, compares the value of its stored values directly for equality;
If you are acting on a variable of a reference type, the address of the object to which you are pointing is compared

2) for the Equals method:
Note: The Equals method cannot act on a variable of the base data type
If the Equals method is not overridden, the address of the object to which the variable of the reference type is to be compared;
Classes such as String, date, and so on have overridden the Equals method to compare the contents of the object being pointed to.

Resolution of "equals" and "= ="

Second, object class

The object class is the ultimate parent class for all classes, and any class inherits the object class's

Java is an object-oriented language, the core idea: to find the right object to do the right thing
Method One: Customize the class to create the object through a custom class.
Way Two: Sun provides a lot of classes for me to use, we just need to know these classes, we can create objects through these classes.

toString: Returns the string representation of the object. The return string is used to describe the object
Question: How does tostring work? A: After overriding the ToString, we can output a new object directly.


Equals (object obj) is used to compare the memory addresses of two objects and to determine whether two objects are the same object.
Hashcode (): Returns the hash code value of the object. (You can now treat the hash code as a memory address.)

2, the way to view the source code

Mode one: Hold down the CTRL key and click the source code you want to see
Way two: Move the cursor to the point where you need to see the code F3

See the source code the most taboo point: not every line of code to understand what the meaning, can read the approximate, guess the meaning is enough.

1  Public classperson{2     3     intID;4 String name;5     6      PublicPerson (intid,string name) {7          This. id=ID;8          This. name=name;9     }Ten     //currently we need to output an object directly when the output format: No.: 110 Name: Dog doll this format. Current Object One     //The tostring method does not meet the requirements of the subclass, so it should be written from the ToString of the object class. A @Override -      PublicString toString () { -         return"No.:" + This. id+ "name +" + This. Name; the     } -     //Why write it: The Equals method of object compares the memory address of two objects by default, we need to compare the ID of two objects at present, and the Equals method of all object class does not conform to our present requirement - @Override -      Public Booleanequals (Object obj) { +Person p=(person) obj; -         return  This. id==p.id; +     } A      PublicPerson () { at          -     } -      Public Static voidMain (string[] args) { -Person p=NewPerson (110, "Dog Doll")); -Person p1=NewPerson (110, "Chen Fubin"); - System.out.println (P.hashcode ()); inSYSTEM.OUT.PRINTLN (P);//[email protected] full class name [email protected]+ object hash code output result: No.: 110 Name: Dog Doll -          to     } +}

Original Dshore

originating from:http://www.cnblogs.com/dshore123/

Welcome reprint, reprint must explain the source. ( If this article is useful for you, you can click on the lower right corner of the recommendation , thank you!) )

Java base string string and object class

Related Article

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.