class lat extends Object {
String name;
int age;
// 覆盖了Object里面toString 方法
public String toString() {
return this.name + "," + this.age;
}
}
class User {
String name;
int age;
public String toString() {
return this.name + "," + this.age;
}
}
// Object 类
public class ObjectDemo {
/*
* Object类是JAVA的根类,要么是一个类的父类,要么是一个类的间接父类
*
* class ABC{}等价于 class ABC extends object{}
*
*
* == 比较内存地址
*
* equals 类覆盖方法
*/
public static void main(String[] args) {
String aa = new String();
System.out.println(aa.hashCode());
String bb = new String();
System.out.println(bb.hashCode());
System.out.println(aa == bb);// false
System.out.println(aa.equals(bb));// true (类覆盖方法)
Object a = new Object();
System.out.println(a.hashCode());
Object b = new Object();
System.out.println(b.hashCode());
System.out.println(a == b);
System.out.println(a.equals(b));
// 用组类不要去创建子类对象
User uu = new User();
uu.toString();
lat ll = new lat();
ll.toString();
}
}
结果是——//基本类型包装类型:装箱和拆箱操作
public class IntegerDemo2 {
public static void main(String[] args) {
// 装箱操作:把一个基本类型的值,转换为对应的包装类对象
// 方式一
Integer num1 = new Integer(17);
// 方式二
Integer num2 = Integer.valueOf(17);
// 拆箱
int num3 = num1.intValue();
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
// 自动装箱:直接赋值给对应的包装类对象(语法糖)
Integer num4 = 13;
// 自动拆箱:直接把一个包装类对象赋给基本类型变量(语法糖)
int num5 = num4;
}
}
结果是——public class IntegerDemo {
/*
*
* 享元模式 共享一个元素 带有缓存设计
*
* int integer
*
*
* 拆箱以后再做比较
*/
public static void main(String[] args) {
Integer i1 = new Integer(123);
Integer i2 = new Integer(123);
System.out.println(i1 == 12);
System.out.println(i1.equals(i2));
int i3 = 123;
int i4 = 123;
System.out.println(i3 == i4);
Integer i5 = Integer.valueOf(123);
Integer i6 = Integer.valueOf(123);
System.out.println(i5 == i6);
Integer i7 = 123;
Integer i8 = 123;
System.out.println(i7 == i8);
Integer i11 = new Integer(250);
Integer i12 = new Integer(250);
System.out.println(i11 == i12);
Integer i31 = Integer.valueOf(250);
Integer i41 = Integer.valueOf(250);
System.out.println(i31 == i41);
Integer i51 = 250;
Integer i61 = 250;
System.out.println(i51 == i61);
}
}
结果是——