java - Object class

1. What is the Object class? What methods are used?

  A: 1) .Object class is the class of the parent class, located java.lang package.

   2) Arrays are also subclasses of the Object class.

   3) common method .Object class are:

        --- toString();

        --- equals();

        --- hashCode();

        ---.....

Two .equals method

  * Equals method 1.Object class
  * public Boolean equals (Object obj) {
     return (the this == obj);
    }

  * 2. like String, Data These classes have rewritten the equals method to compare the content

  * 3. we will often override the equals method in a custom class used to compare the content.
    If you do not override the equals method, it will default to call the equals method of the Object class is compared address values. +

Face questions] == and equals the difference?

1 == : If the basic data types are compared, then the comparison is a specific value
  if the comparison is a reference data type, then the comparison value is the address (directed only two references to the same object)
2.equals: if not override equals method, then compares the address value equals the default method calls the Object class.
  If you override the equals method, then compare the content.

    // override attribute equals method of comparing the contents 
    
    @Override 
    public  Boolean equals (Object obj) {
         IF ( the this == obj) {     // Description same two objects is a 
            return  to true ; 
        } 
        IF (obj the instanceof the Person) { 
            the Person P = (the Person) obj;     // downcast 
            return  the this .name.equals (p.name) && the this .age == p.age; 
        } 
        return  to false ;
        

III. Design Module

1. Design Patterns: Design mode is the preferred code structure, coding style and then summarize the theory of a large number of practices, ways of thinking and problem-solving

2. There are 23 common design patterns: Singleton design pattern, proxy design pattern, decorative design pattern, the observer design pattern .....

3. Singleton design pattern: a class in a project that only one object

4. lazy man and starving type of difference?
  1. starving type: thread-safe   2 lazy type: thread-safe, but delayed the timing of object creation.

// lazy type: thread-safe, but delayed the timing of object creation, reducing memory overhead to some extent 

class Bank2 {
     // privatization constructor 
    Private Bank2 () {}; 
    
    Private  static Bank2 Bank = null ; 
    
    public  static bank Bank2 getSingleton () {
         IF (Bank == null ) { 
            Bank = new new bank Bank2 (); 
        } 
        return Bank; 
    } 
} 

// starving formula: thread-safe 

class Bank {
     // privatization constructor 
    Private Bank () { }
     // class interior create a privatization target 
    private static Bank Bank = new new Bank ();
     // Create a common method of obtaining an object 
    public  static Bank getSingleton () {
         return Bank; 
    } 
}

Four .static

1.static modifying variables: class variables 1. The co-owner of a class variable with a class object. Each object has a respective instance variables     when a class variable to modify objects, see other objects are class variables modified   2. Class variable is loaded with the loading of class, instance variables are with the creation of objects created   3. class loader loaded only once   4. how to call class variables: variable name class name class name of the object class variable name.
  



2.static modification methods: static methods 1. static method is loaded with class and loaded.   2. static method call: the static method name class name object name static method name.   3. A static method can not be called, non-static instance variables and methods, may be non-static method call static class variables and methods   4. The static methods can not be use "this" and "super"
  


3. Thoughts? When using a modified static properties and methods

static modification attributes:
  1. When a static variable must be modified to a constant
  2. When using a static modification when multiple objects using a common attribute.
static modification methods:
  1. tools are generally used in static modification
  2. Sometimes in order to call a class variable, use the static method will be modified

Class loading process
  a. When we create an object, the method will first look at the class area information
  b. If there is no information of such methods zone, which is the class will be loaded. If the method area information directly create an object class
  c class loader: the file is loaded into the JVM byte code, while the class used to store a particular variable region of a method area
  . After the class D loaded, again creating an object
  f still in the method area to find information when you create a class object again. If there will not be a class loader (classloader loaded only once)

Five. HashCode ()

Object class defined as:

public native int hashCode();

It is a native method, the value of the return address of the object.

However, the same lines, in other packages String class rewritten to this method. Method calls to obtain a calculated value obtained int

 

VI. What is the toString method? for example

 

  A: toString method can convert any object to a string returns a return value generation algorithm is:. GetClass () getName () + '@' + Integer toHexString (hashCode ())..

 

   toString method when printing an object class called directly: Example 1)

 

    public static void main(){

 

     Student stu1 = new Student();

 

      stu1.setName ( "Joe Smith");

 

      stu1.setAge(23);

 

      System.out.println(stu1);

 

      System.out.println(stu1);

 

}

 

Print: Student @ 152b6651;

 

After Example 2). ToString method class itself rewrite toString method Student class rewritable

 

            public static void main(){

 

              Student stu1 = new Student();

 

              stu1.setName ( "Joe Smith");

 

              Stu1.setAge(23);

 

              System.out.println(stu1);

 

}

 

Printing: Student: name = Joe Smith, age = 23;

 

      例3).public class Student{

 

            Private String name = "123"; // Student Name

 

            private int age; // Age

 

            public String getName(){

 

              Return name;

 

}

 

public void setName(String name){

 

  this.name = name;

 

}

 

Public int getAge(){

 

   Return age;

 

}

 

Public void setAge(int age){

 

    this.age = age;

 

}

 

public String toString(){

 

  Return “Student:” + “name=” + name + “,age=” + age;

 

}

 

}

 

 

Guess you like

Origin www.cnblogs.com/jiujiang/p/11613848.html