Java foundation 18 String string and Object class

1. String string

Q: Written question: How many objects does new String("abc") create?
Answer: Two objects, one in heap memory and one in string constant pool

 1 public class Demo1 {
 2     public static void main(String[] args) {
 3         String str1="hello";
 4         String str2="hello";
 5         String str3=new String("hello");
 6         String str4=new String("hello");
 7         System.out.println("str1==str2?"+(str1==str2)+"+++++++");//true
 8         System.out.println("str2==str3?"+(str2==str3)+"-----");//false
 9         System.out.println("str3==str4?"+(str3==str4)+"=====");//false
10         System.out.println("str3.equals(str4)?"+(str3.equals(str4)));//true
11     }
12 
13 }

in conclusion:

1) For ==:
    if it acts on a variable of a basic data type, it directly compares the stored "value" for equality;
    if it acts on a variable of a reference type, it compares the address of the pointed object

2) For the equals method :
    Note: The equals method cannot act on variables of basic data types.
    If the equals method is not overridden, the address of the object pointed to by the variable of the reference type is compared;
    classes such as String and Date override the equals method. If so, the contents of the pointed-to object are compared.

Resolution of " equals " and " == "

2. Object class

    The Object class is the ultimate parent class of all classes, and any class inherits the Object class.

Java is an object-oriented language. The core idea is to find suitable objects to do suitable things
        . Method 1: Customize classes, create objects through custom classes.
        Method 2: Sun provides many classes for me to use, we only need to know These classes, we can create objects through these classes.

toString: Returns the string representation of this object. The returned string is used to describe the object
     Question: What does toString do? Answer: After rewriting the toString sub, we can directly output a new object.


     equals(Object obj) is used to compare the memory addresses of two objects to determine whether the two objects are the same object. 
     hashCode(): Returns the hash code value of the object. (You can now treat hash codes as memory addresses)

2. How to view the source code

    Method 1: Hold down the ctrl key and click the source code you want to see
    Method 2: Move the cursor to the place where you need to view the code F3

The most taboo thing about reading source code: don't understand what each line of code means, if you can understand it, it is enough to guess the meaning.

1  public  class Person{
 2      
3      int id;
 4      String name;
 5      
6      public Person( int id,String name) {
 7          this .id= id;
 8          this .name= name;
 9      }
 10      // Currently we need to output directly When it is an object, the output format: number: 110 name: dog baby. Currently Object
 11      // The toString method cannot meet the needs of subclasses, so at this time, it should be written from the toString of the object class. 
12      @ Override
 13      public String toString() {
 14          return "Number:"+ this.id+"name+"+ this .name;
 15      }
 16      // Why write it: The equals method of object compares the memory addresses of the two objects by default. What we need to compare now is the ID card of the two objects , the equals method of all object classes does not meet our current requirements 
17      @Override
 18      public  boolean equals(Object obj) {
 19          Person p= (Person)obj;
 20          return  this .id== p.id;
 21      }
 22      public Person () {
 23          
24      }
 25      public  static  void main(String[] args) {
 26          Person p= new Person(110,"Doll");
 27          Person p1= new Person(110,"Chen Fubin" );
 28          System.out.println(p.hashCode());
 29          System.out.println(p); // com.bw.objects.Person@ 7ea06d25 Full class name+@+The hash code of the object Output result: ID: 110 Name: Gouwa 
30          
31      }
 32 }

 

 

 

 

 

Original Author: DSHORE

From: http://www.cnblogs.com/dshore123/

Welcome to reprint, reprint must indicate the source. ( If this article is useful to you, you can click Recommend , thank you! )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850188&siteId=291194637