Object class
Object is the parent of all classes, that is, all classes can be received with Object.
Except for Object classes, all classes have inheritance relationships.
Obtaining object information
Getting object information, using the toString method
package obj.exe.bit; class Person{ private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } } public class MyToString { public static void toStr(Object object){ System.out.println(object.toString()); } public static void main(String[] args) { Person person = new Person("Jack",20); toStr(person);//Receive an object toStr("hello world!!!");//Receive a string } }
The result shows that the toString() method provided by the Object class obtains the address of the object, so the toString() method is overridden on the required subclass.
package obj.exe.bit; class Person{ private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } @Override public String toString() { return "Full name:"+this.name+",Age:"+this.age; } } public class MyToString { public static void toStr(Object object){ System.out.println(object.toString()); } public static void main(String[] args) { Person person = new Person("Jack",20); toStr(person);//Receive an object toStr("hello world!!!");//Receive a string } }
Object comparison
The equals() method in the String class is an override of the equals() method in the Object class.
package obj.exe.bit; class Person1{ private String name; private int age; public Person1(String name,int age){ this.name = name; this.age = age; } @Override public String toString() { return "Full name:"+this.name+",Age:"+this.age; } @Override public boolean equals(Object obj) { Person1 person1 = (Person1) obj; return this.name.equals(person1.name) && this.age == person1.age; } } public class MyEquals { public static void main(String[] args) { Person1 person11 = new Person1("Jack",20); Person1 person12 = new Person1("Jack",20); System.out.println(person11.equals(person12));//true } }
Similarly, Objcect can receive classes, arrays, interfaces
Receive array:
package obj.exe.bit; import java.util.Arrays; public class MyArray { public static void main(String[] args) { Object object = new int[] {1,2,3,4,5,6};//Upward transformation int[] arr = (int[]) object;//Downward transition System.out.println(Arrays.toString(arr)); } }
Receiving interface:
package obj.exe.bit; interface Student{ public void getStudent(); } class StudentImpl implements Student{ @Override public void getStudent() { System.out.println("i am a student"); } @Override public String toString() { return "i am not a student"; } } public class MyInterface { public static void main(String[] args) { Student student = new StudentImpl(); Object object = student;//Upward transformation System.out.println(object); Student student1 = (Student) object;//Downward transition student.getStudent(); } }
Packaging
A wrapper class encapsulates basic data types into a class.
Boxing and UnBoxing
Packing: Change the basic data type into the object of packing class, and use the construction method provided by each packing class to realize packing processing.
Unpacking: Remove the basic data types wrapped in the wrapper class. Use the six methods provided in the Number class.
After JDK 1.5, provide the mechanism of automatic disassembly and packing.
Arrow Coding Specification: Comparisons of values between all wrapper class objects of the same type, all using equals method
Standards for the use of basic data types and packaging data types:
- [Mandatory] All POJO class attributes must use wrapped data types
- The return value and parameters of the RPC method must use the wrapped data type.
- [Recommendation] All local variables use basic data types.
String and Basic Data Type Conversion
Change strings to int
String str = "123" ; // String type int num = Integer.parseInt(str) ; System.out.println(num);
Change string to double
String str = "123" ; // String type double num = Double.parseDouble(str) ; System.out.println(num);
String and boolean conversion
String str = "truesds" ; // String type boolean num = Boolean.parseBoolean(str) ; System.out.println(num);
- Any data type that uses'+'to connect blank characters becomes a character type.
- Using the valueOf() method provided in the String class, this method does not generate garbage.