Object类
- Object类是所有Java类的根父类。
- 如果在类的声明中未使用extends关键字指明其父类,则其默认父类为Object类。
Object类中的主要方法:
1、public boolean equals(Object obj)方法
比较两个对象是否“同一”,即比较两个引用是否指向同一个对象,与==含义相同。
2、public int hashCode()方法
得到用以标志对象的哈希码。
3、public String toString()方法
以字符串形式返回对象相关信息。
4、public final Class getClass()方法
获取当前对象所属类的信息,返回Class对象。
package test;
public class Test {
public static void main(String[] args) {
Test test1=new Test();
System.out.println("test1对象所属于的类为:"+test1.getClass());
System.out.println("test1对象的哈希码值为:"+test1.hashCode());
System.out.println("test1对象的toString()方法返回值为:"+test1.toString());
Test test2=new Test();
System.out.println("利用equals方法判断两个对象是否相等的结果:"+test1.equals(test2));
System.out.println("利用==判断两个对象是否相等的结果:"+test1.equals(test2));
String s1=new String("abc");
String s2=new String("abc");
String s3="123";
String s4="123";
System.out.println("s1==s2结果为"+(s1==s2));
System.out.println("s1.equals(s2)结果为:"+(s1.equals(s2)));//String类中改写了equals方法
System.out.println("s3==s4结果为"+(s3==s4));//字符串常量生成的变量,其所存放的地址是相同的
System.out.println("s3.equals(s4)结果为:"+(s3.equals(s4)));
}
}
其运行结果为:
注意:
判断两个字符串内容是否相等,要使用equals方法,而不能用==。
对象类型转换
设Person是Student的父类
- 从子类到父类的类型转换可以自动进行
package test;
public class Test {
public static void main(String[] args) {
Student s = new Student();
Person p = s;
}
}
- 从父类到子类的类型转换必须通过强制类型转换实现
package test;
public class Test {
public static void main(String[] args) {
Person p = new Person();
Student s = (Student) p;
}
}
- 无继承关系的引用类型间的转换是非法的
例子:
package test;
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.method(new Person());
t.method(new Student());
}
public void method(Person e) {
if(e instanceof Student) {
Student s = (Student) e;
s.getSchool();
}else {
e.test();
}
}
}
package test;
public class Person {
public void test() {
System.out.println("这是Person的test()方法");
}
}
package test;
public class Student extends Person {
public void getSchool() {
System.out.println("这是Student的getSchool()方法");
}
}
其运行结果为:
==操作符和equals()方法
==:
(1)基本类型比较值:只要两个变量的值想等,即为true。
package test;
public class Test {
public static void main(String[] args) {
int i = 1;
System.out.println(i == 1);
}
}
其运行结果为:
(2)引用类型比较引用(是否指向同一个对象)
只有指向同一个对象时,==才返回true。
package test;
public class Test {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
System.out.println(p1 == p2);
}
}
下面这种情况才是true:
- 用“==”进行比较时,符号两边的数据类型必须兼容(可自动转换的基本数据类型除外),否则编译出错。
equals()
- 只能比较引用类型,其作用与“==”相同,比较是否指向同一个对象。
- 格式:
obj1.equals(obj2)
package test;
public class Test {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
System.out.println(p1.equals(p2));
}
}
- 特例:当用equals()方法进行比较时,对类File、String、Date及包装类(Wrapper Class)来说,是比较类型及内容而不考虑引用的是否是同一个对象。
- 原因:在这些类中重写了Object类的equals()方法。
package test;
public class Test {
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2));
}
}
String对象的创建
字面量创建对象的时候,只在常量池创建—个对象;
new的时候,常量池有对象,堆中也要有对象;
字面量方法要比new省内存
==比较的是对象的内存地址
包装类
(1)基本数据类型包装成包装类实例——装箱
- 通过包装类的构造器实现:
int i = 500;
Integer t = new Integer(i);
- 还可以通过字符串参数构造包装类对象:
Float f = new Float("4.56");
Long l = new Long("asdf");//会报错
(2)获得包装类对象中包装的基本类型变量——拆箱
- 调用包装类的
.xxxValue()
方法:
package test;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(112);
int i0 = i.intValue();
System.out.println(i0);
}
}
(3)jdk1.5版本之后,可进行自动装箱和拆箱
(4)字符串转换成基本数据类型
(5)基本数据类型转换成字符串
package test;
public class Test {
public static void main(String[] args) {
//字符串转换成基本数据类型
int i = Integer.parseInt("123");
float f = Float.parseFloat("0.40");
boolean b = Boolean.parseBoolean("false");
//基本数据类型转换成字符串
String istr = String.valueOf(i);
String fstr = String.valueOf(f);
String bstr = String.valueOf(true);
}
}
toString
父类Object的toString方法是输出当前对象的内存地址。
如果要输出其他的信息,则需要重写toString方法。
文章评论