Java_Day13(import,Object,equals,finalize)

import

  • Class does not need to import (the java.lang) under lang package, automatically imported, such as the String class, System, java.util.Scanner full class name, class name referred Scanner.
  • However Scanner class, we need to import.
  • Classes in java.util. ; " " indicates that the transfection package util all classes.
  • IEDA class package when using other prompts guide the package.
  • Classes and interfaces can only be modified by the public and by default.
  • What is an API?
    Application programming interface. (APPlication Program Interface)
    entire JDK class library is a javase of API. (Ie the sun's advance written library)
    for each API will be equipped with a help document.

Root class: Object, common to all subclasses

toString () method, the object memory address into a hexadecimal string. Recommended to rewrite toString method, the result is simple, easy to read.
; Object .toString (): Default call
* rewrite toString method;
public String toString () {
return this.year + "of" + this.month + "month";
}
output references, call the default toString method.

equals

What is the use equals method?
Later used to determine whether two objects are equal programming.
Analyzing two basic data types are equal: double equal sign.
Two types of reference data to determine it?
Double equal sign can not be used, two double equal sign is determined store a reference address.
Boolean the equals public (Object obj) {
return (the this == obj);
}
Boolean that t1.equals B = (T2); // for instance

  • Then it came to power equals :( default is "==" was used in the ancestors Object, and we have to judge the content of the two objects are equal, so we have to override the equals method subclass)
  • the equals boolean public (Object obj) {
    // how to rewrite your own set, if you think the two objects are equal, you set
    if (thisobj)
    return to true;
    // Get a property of the first object
    int = YEAR1 this.year1;
    // Get property of the second object, downcast
    IF (obj = null || the instanceof Mytime obj) {!
    return flase;
    }
    Mytime T = (Mytime) obj;
    int = YEAR2 t.year2;
    return YEAR1
    YEAR2;
    }
    with the rewriting tool IDEA: can be generated directly.
  • Reference data type is String, String is a class, have a constructor.
    It can also be used
    String s3 = new String ( "jia ");
    thus, string comparisons are not double equal sign (new form) may be used without direct == defined, but String override equals method, can be used equals method compares two strings are equal.
  • And the String class, rewrite toStiring method.
  • java determine what data can be used ==?
    - the basic data types
  • What type of data using java equals judgment?
    - reference data types
    rewrite toSting, equals method Exercise:
    Here Insert Picture Description
public class Test6 {
	public static void main(String[] args) {
		Mine a1=new Mine("xinkong",new Address("Shanghai","fei"));
		Mine a2=new Mine("xinkong",new Address("Shanghai","fei"));
		System.out.println(a1.equals(a2));
	}

}
class Mine{
	String name;
	Address addr;
	/**
	 * @param name
	 * @param addr
	 */
	public Mine(String name, Address addr) {
		super();
		this.name = name;
		this.addr = addr;
	}
	public boolean equals(Object obj) {
		if(obj ==null||!(obj instanceof Mine))
			return false;
		if(this == obj)
			return true;
		Mine U =(Mine)obj;
		if(this.name.equals(U.name)&&this.addr.equals(U.addr))
			return true;
		return false;
	}
   
}
class Address{
	String city;
	String street;
	
	/**
	 * @param city
	 * @param street
	 */
	public Address(String city, String street) {
		super();
		this.city = city;
		this.street = street;
	}
	public boolean equals(Object obj) {
		if(obj ==null||!(obj instanceof Address))
			return false;
		if(this == obj)
			return true;
		Address aa = (Address)obj;//向下转型,强制类型转换,将父类型object转换成Mine
		if(this.city.equals(aa.city)&&this.street.equals(aa.street))
			return true;
		return false;
	}
	
	
}

To completely override the equals method.
Here Insert Picture Description

finalize () method

  • Garbage collector automatically invoked when a java object about to be recycled time.
  • finalize () method, is an opportunity in front of sun garbage collection company for the java programmers, to prepare. Can perform finalize () method before garbage collection. Similar to the static block of code, the class loading time. {static
    } such record release time.
  • Do not need to call, call back its GC. But need to rewrite their own. The Java garbage collector is not easy to start, such as garbage too little time not to like.
  • System.gc (); // recommended to start the garbage collector.
  • Results of hashCode () method may be equivalent to a java object memory address (based on a hash algorithm).
Published 50 original articles · won praise 8 · views 3070

Guess you like

Origin blog.csdn.net/jiahuan_/article/details/105056384