Class Object
The Object class (base class) is the root parent of all java classes.
Multi level inheritance. The parent class at the highest level must be the Object class.
For example, if you want to set a parameter parameter for the test method, which class will be passed in if you are not sure about the parameter, what type will be set for the parameter of the test method?
public class Test{ public void test(Object obj){ ...... } public static void main(String[] args){ Test t = new Test(); Person p = new Person(); Student s = new Student(); t.test(p); t.test(s); t.test(new Kk()); } }
Main methods in Object class
public boolean equals(Object obj) -- object comparison (reference object)
public static void main(String[] args){ Person p = new Person(); Person e = new Person(); System.out.println(p.equal(e));//false, pointing to two different reference objects e = p; System.out.println(p.equal(e));//true
public int hashCode() -- get Hash code
Object obj = new Object(); System.out.println(obj.hasCode());//31168322,Hash
public String toString -- called when printing objects
Person p = new Person(); System.out.println(p.toString());//The memory address of the current reference object
Casting of objects
Basic type conversion:
1. Automatic type conversion: small to large (data type)
For example: long = 20; double D = 12.0f;
2. Cast: big to small (data type)
For example: float f =(float)12.0; int a = (int)1200L;
Casts on Java objects are called shapes:
The type conversion from child class to parent class can be done automatically;
The type conversion from the parent class to the child class must be implemented by modeling (cast type);
Illegal conversion between reference types without inheritance.
Example: the Student class is a subclass of the Person class.
Student s = new Student(); Person p = s; Person p = new Person(); Student s = (Student) p; String s = "hello"; Object obj = s; System.out.println(obj);//hello Object obj = "hello"; String s = (String) obj; System.out.println(s);//hello
Child class to parent class, upward transformation.
From parent class to child class, use instanceof to judge and transform downward.
public class Person(){ public void test(){ System.out.println("Person"); } } public class Student(){ public void getSchool(){ System.out.println("Student"); } } public class Test(){ public void method(Person e){ if(e instanceof Student){ Student s = (Student) e; s.getSchool(); } else{ e.test(); } }
==Operators and equals methods
== :
1. Basic type comparison value: true as long as the values of two variables are equal.
2. Reference type comparison reference (whether pointing to the same object or not): only when pointing to the same object, = = returns true.
Person p1 = new Person(); Person p2 = new Person(); System.out.println(p1 == p2);//false Person p1 = new Person(); Person p2 = p1; System.out.println(p1 == p2);//true
Note: when "= =" is used, the data types on both sides of the symbol must be compatible.
equals method
equals(): all classes inherit the Object and get the equals() method. It can also be overridden.
Only reference types can be compared. Their functions are the same as "= =" to compare whether they point to the same object.
Person p1 = new Person(); Person p2 = new Person(); System.out.println(p1.equals(p2));//false
Special case:
When using the equals() method for comparison, for the classes File, String, Date and wrapper class, it is to compare the type and content without considering whether the reference is the same object or not;
Reason: the equals() method of the Object class is overridden in these classes.
String s1 = new String("abc"); String s2 = new String("abc"); System.out.println(s1 == s2);//false System.out.println(s1.equals(s2));//true
Creating a String object
Wrapper class
For the eight basic definition of the corresponding reference type - wrapper class (wrapper class).
Most of the initials can be capitalized, special: int - integer, char - Character.
The basic data type is packaged as an instance of a wrapper class -- boxing.
//Implemented by the constructor of the wrapper class: int i = 500; Integer t = new Integer(i); //You can also construct wrapper class objects with string parameters Float f = new Float("4.56"); Long l = new Long("asdf");//NumberFormatException
Get the basic type variable of packing in the packing class object -- unpacking.
//Call the. * * Value() method of the wrapper class; Integer i = new Integer(112); int i0 = i.Value(); System.out.println(i0);//112
Automatic disassembly box
Integer i = new Integer(112); int i0 = i.intValue(); Integer i1 = 112;//Automatic boxing int i2 = i1;//Automatic dismantling boolean b = new Boolean("false");//Automatic dismantling Boolean b1 = true;//Automatic boxing
Main applications:
String to basic data type
int i = Integer.parseInt("123");
Convert basic data type to string
//Calling valueOf() method of string overload String f = String.valueOf(i); //Direct approach String in = 5 + "";
toString() method:
If the toString() method is not overridden, the memory address of the object is printed out, and m.toString() is equivalent to M.
Keyword static
public class Chinese{ //Class variables (static variables), which are not instantiated, are part of a class and can be shared by instantiated objects of this class static String country; //Instance variables, which can only be used after instantiation, are part of the instantiated object and cannot be shared String name; int age; } public class Test{ public static void main(String[] args){ Chinese.country = "China"; Chinese c = new Chinese(); c.name = "+++"; c.age = 123; } }
Design idea of class attribute and class method:
Analysis class properties do not change according to different objects. Set these properties as class properties.
Method is independent of the caller and is declared as a class method.
Tool classes usually use static methods.
Characteristics of modified members:
1. Load as class loads
2. Priority over object existence
3. Decorated member, shared by all objects
4. When the access permission is running, it can be called directly by the class without creating the object
this cannot be used inside a static method.