Object类
1. Object类是所有Java类的根父类
2. 如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object类
3. Object类中的功能(属性、方法)就具有通用性。
属性:无
方法:equals() / toString() / getClass() /hashCode() / clone() / finalize()/wait() 、 notify()、notifyAll()
4. Object类只声明了一个空参的构造器
equals()
==和equals()的区别
1. 可以使用在基本数据类型变量和引用数据类型变量中
2. 如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等。
(不一定类型要相同)-->有自动类型提升
3. 如果比较的是引用数据类型变量:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体
4. 补充: == 符号使用时,必须保证符号左右两边的变量类型一致。
5. 是一个方法,而非运算符
6. 只能适用于引用数据类型
7. Object类中equals()的定义:
public boolean equals(Object obj) {
return (this == obj);
}
8. 说明:Object类中定义的equals()和==的作用是相同的:
比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体
9. 像String、Date、File、包装类等都重写了Object类中的equals()方法。重写以后,
比较的不是两个引用的地址是否相同,而是比较两个对象的"实体内容"是否相同。
10. [重点] 通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的"实体
内容"是否相同。那么,我们就需要对Object类中的equals()进行重写. 重写的原则:比较两
个对象的实体内容是否相同.
public class EqualsTest {
public static void main ( String[ ] args) {
int i = 10 ;
int j = 10 ;
double d = 10.0 ;
System. out. println ( i == j) ;
System. out. println ( i == d) ;
boolean b = true ;
char c = 10 ;
System. out. println ( i == c) ;
char c1 = 'A' ;
char c2 = 65 ;
System. out. println ( c1 == c2) ;
Customer cust1 = new Customer ( "Hwang" , 21 ) ;
Customer cust2 = new Customer ( "Hwang" , 21 ) ;
System. out. println ( cust1 == cust2) ;
String str1 = new String ( "Hwang" ) ;
String str2 = new String ( "Hwang" ) ;
System. out. println ( str1 == str2) ;
System. out. println ( "****************************" ) ;
System. out. println ( cust1. equals ( cust2) ) ;
System. out. println ( str1. equals ( str2) ) ;
Date date1 = new Date ( 32432525324 L) ;
Date date2 = new Date ( 32432525324 L) ;
System. out. println ( date1. equals ( date2) ) ;
}
}
public class Customer {
private String name;
private int age;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public Customer ( ) {
super ( ) ;
}
public Customer ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null)
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
Customer other = ( Customer) obj;
if ( age != other. age)
return false ;
if ( name == null) {
if ( other. name != null)
return false ;
} else if ( ! name. equals ( other. name) )
return false ;
return true ;
}
int it = 65 ;
float fl = 65.0f ;
System. out. println ( “65 和65.0f 是否相等?” + ( it == fl) ) ;
char ch1 = 'A' ; char ch2 = 12 ;
System. out. println ( "65和'A'是否相等?" + ( it == ch1) ) ;
System. out. println ( “12 和ch2是否相等?" + ( 12 == ch2) ) ;
String str1 = new String ( "hello" ) ;
String str2 = new String ( "hello" ) ;
System. out. println ( "str1和str2是否相等?" + ( str1 == str2) ) ;
System. out. println ( "str1是否equals str2?" + ( str1. equals ( str2) ) ) ;
System. out. println ( “hello” == new java. util. Date ( ) ) ;
toString()
1. 当我们输出一个对象的引用时,实际上就是调用当前对象的toString()
2. Object类中toString()的定义:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());//16进制的hash码值。
}
3. 像String、Date、File、包装类等都重写了Object类中的toString()方法。使得在调用对象的toString()时,返回"实体内容"信息。
4. 自定义类也可以重写toString()方法,当调用此方法时,返回对象的"实体内容"
@Override
public String toString ( ) {
return "Customer [name=" + name + ", age=" + age + "]" ;
}
面试题
final、finally、finalize的区别?
public void test ( ) {
char [ ] arr = new char [ ] { 'a' , 'b' , 'c' } ;
System. out. println ( arr) ;
int [ ] arr1 = new int [ ] { 1 , 2 , 3 } ;
System. out. println ( arr1) ;
double [ ] arr2 = new double [ ] { 1.1 , 2.2 , 3.3 } ;
System. out. println ( arr2) ;
}
包装类
1. 针对八种基本数据类型定义相应的引用类型—包装类(封装类)
2. 有了类的特点,就可以调用类中的方法,Java才是真正的面向对象
转换
JDK1.5之后,支持自动装箱,自动拆箱。但类型必须匹配。
1.通过包装类的构造器实现:
int i = 500; Integer t = new Integer(i);
2.还可以通过字符串参数构造包装类对象:
Float f = new Float(“4.56”);
Long l = new Long(“asdf”); //NumberFormatException
public class WrapperTest {
@Test
public void test1 ( ) {
int num1 = 10 ;
Integer in1 = new Integer ( num1) ;
System. out. println ( in1. toString ( ) ) ;
Integer in2 = new Integer ( "123" ) ;
System. out. println ( in2. toString ( ) ) ;
Float f1 = new Float ( 12.3f ) ;
Float f2 = new Float ( "12.3" ) ;
System. out. println ( f1) ;
System. out. println ( f2) ;
Boolean b1 = new Boolean ( true ) ;
Boolean b2 = new Boolean ( "TrUe" ) ;
System. out. println ( b2) ;
Boolean b3 = new Boolean ( "true123" ) ;
System. out. println ( b3) ;
Order order = new Order ( ) ;
System. out. println ( order. isMale) ;
System. out. println ( order. isFemale) ;
}
}
class Order {
boolean isMale;
Boolean isFemale;
}
1. 调用包装类的.xxxValue()方法:
boolean b = bObj.booleanValue();
@Test
public void test2 ( ) {
Integer in1 = new Integer ( 12 ) ;
int i1 = in1. intValue ( ) ;
System. out. println ( i1 + 1 ) ;
Float f1 = new Float ( 12.3 ) ;
float f2 = f1. floatValue ( ) ;
System. out. println ( f2 + 1 ) ;
}
自动装箱与拆箱
1. 自动装箱:基本数据类型 --->包装类
2. 自动拆箱:包装类--->基本数据类型
@Test
public void test3 ( ) {
int num2 = 10 ;
Integer in1 = num2;
boolean b1 = true ;
Boolean b2 = b1;
System. out. println ( in1. toString ( ) ) ;
int num3 = in1;
}
public void method ( Object obj) {
System. out. println ( obj) ;
}
基本数据类型、包装类—>String类型
1. 方式1:连接运算
2. 方式2:调用String的valueOf(Xxx xxx)
总结:不管你是基本数据类型、包装类类型的,都可以调用valueOf(Xxx xxx),将基本数据类型、包装类类型作为参数传进去。
@Test
public void test4 ( ) {
int num1 = 10 ;
String str1 = num1 + "" ;
float f1 = 12.3f ;
String str2 = String. valueOf ( f1) ;
Double d1 = new Double ( 12.4 ) ;
String str3 = String. valueOf ( d1) ;
System. out. println ( str2) ;
System. out. println ( str3) ;
}
String类型 —>基本数据类型、包装类
1. String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)
总结:parseXxx(String s)为静态方法,可以使用类名去调用。
@Test
public void test5 ( ) {
String str1 = "123" ;
int num2 = Integer. parseInt ( str1) ;
System. out. println ( num2 + 1 ) ;
String str2 = "true1" ;
boolean b1 = Boolean. parseBoolean ( str2) ;
System. out. println ( b1) ;
}
包装类的面试题
Object o1 = true ? new Integer ( 1 ) : new Double ( 2.0 ) ;
System. out. println ( o1) ;
Object o2;
if ( true )
o2 = new Integer ( 1 ) ;
else
o2 = new Double ( 2.0 ) ;
System. out. println ( o2) ;
public void method1 ( ) {
Integer i = new Integer ( 1 ) ;
Integer j = new Integer ( 1 ) ;
System. out. println ( i == j) ;
Integer m = 1 ;
Integer n = 1 ;
System. out. println ( m == n) ;
Integer x = 128 ;
Integer y = 128 ;
System. out. println ( x == y) ;
}