Object method
1. Object method
Object: superclass of all classes, you can accept all classes of objects
1.1 public String toString () {}: acquiring object information
Default output is the address of the object code, changing by overwriting Method
Example: Object class output
public class Test {
public static void fun(Object obj){
//输出对象的地址码
//默认调用的是Object的toString方法
System.out.println(obj); //等价于obj.toString()
}
public static void main(String[] args) {
Person person = new Person();
person.setAge(20);
Object object = person;
fun(person);
fun(object);
}
}
/**
com.csdn.qh.Person@4554617c
com.csdn.qh.Person@4554617c
*/
Example: override toString () method
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String toString(){
return "Name:"+this.getName()+",Age:"+this.getAge();
}
}
public class Test {
public static void fun(Object obj){
//默认调用的是Object的toString方法
//通过覆写父类方法,调用的是子类覆写的方法
System.out.println(obj);
}
public static void main(String[] args) {
Person person = new Person();
person.setAge(20);
person.setName("张三");
Object object = person;
fun(person);
fun(object);
}
}
/**
Name:张三,Age:20
Name:张三,Age:20
*/
1.2.public boolean equals (Object obj) {}: Comparison Object
There are used in the String class equals method, str.equals (str2) ---- for comparing the contents of the two strings are equal, described String class overrides the equals method of Object.
Overrides the equals method in the String class, source code analysis:
public boolean equals(Object anObject) {
if (this == anObject) { //判断对象是否相等(地址进行判断)
return true;
}
if (anObject instanceof String) { //判断anObject是否是String的实例化对象
String anotherString = (String)anObject; //转换成String类
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false; //不是String类
}
Example: Write a Person class equals method
public boolean equals(Object obj) {
if (obj == null){
return false;
}
//this和obj是同一个对象(地址)
if(this == obj){
return true;
}
//this和obj不是同一个对象(比较内容)
//比较的是name和age
if(obj instanceof Person){ //obj是否是Person类的实例化对象
//比较所有属性
Person that = (Person)obj;
return this.getName().equals(that.getName())&& this.getAge()== that.getAge();
}
//不是Person类对象实例化的
return false;
}
2. Accept all references data types
Object can accept all types of Java objects (classes, interfaces, array)
for example as follows:
interface Message{
void print();
}
public class Test {
public static void main(String[] args) {
//Object能够接受Java所有类型的对象(类,接口,数组)
//1.Object接受数组
Object object = new int[]{1,2,3,4};
//int[] 是引用类型数据,是objec子类,必须进行强转
int[] intArray = (int[]) object;
for(int value:intArray){
System.out.println(value+",");
}
//2.object接收接口类型
Object object1 = new Message(){
@Override
public void print() {
System.out.println("hello java");
}
};
//向下转型
Message message = (Message) object1;
message.print();
//基本数据类型转到引用数据类型
Object object2 = 10;
System.out.println(object2);
}
}
3. packaging
- Role: is to convert basic data types as reference data types
- Basic data packaging:
- The object type (Object direct subclass - the direct successor Object): Boolean,Character(char)
- Numeric (inherited from Number The): Byte, Double, Short, Integer(int),Float
- Packing: int -----> Integer
- Unpacking: Integer ----> int
Example: packing and unpacking
public class Test1 {
public static void main(String[] args) {
Integer integer = new Integer(10); //10->装箱->integer
int intValue = integer.intValue(); //integer ->拆箱->intValue
System.out.println(intValue); //10
}
}
Example: JDK automatic packing and unpacking
public class Test1 {
public static void main(String[] args) {
//JDK自动装箱和拆箱
//1.包装类
//2.自动装箱、拆箱
Object object = 10;
int value = (int)object;
Number number = 10; //Integer直接继承Number
Object object1 = false; //Boolean直接继承自Object
Integer x = 55; //自动装箱,变成new Integer(55);
System.out.println(++x*5);//自动拆箱 x.intValue
}
}
Integer Compare
public class Test1 {
public static void main(String[] args) {
//基于String赋值的方式 ---->内存池
//包装Integer直接跟内存有关,而是Integer内部做了缓存
//不直接new,直接赋值,会产生缓存,值在[-128,127]
Integer a = 100;
Integer b = 100;
Integer c = new Integer(100);
Integer d = new Integer(100);
Integer g = 666;
Integer h = 666;
//使用缓存
System.out.println(a == b); //true
System.out.println(a.equals(b)); //true
System.out.println(g == h); //false
System.out.println(g.equals(h)); //true
//不使用缓存
System.out.println(c == d); //false
System.out.println(c.equals(d)); //true
}
}