常用类

常用类

Object类

java中所有的类都直接或间接的继承于Object类,自动的拥有Object类的所有方法. toString方法用于输出一个对象的内容.

package day15;
class Animal {
private String name;
private String type;
public Animal(String name, String type) {
super();
this.name = name;
this.type = type;
}
}

public class Day1501 {
public static void main(String[] args) {
Animal a1 = new Animal("旺财", "中华田园犬");
// Animal没有toString方法,因此会调用Object类的toString方法
// Object类的toString方法将返回对象的地址
String str1 = a1.toString();
System.out.println(str1);// day15.Animal@15db9742
// println方法内部会调用对象a1的toString方法
System.out.println(a1);// day15.Animal@15db9742
}
}

为了输出对象的内容,可以重写toString方法。

package day15;
class Animal {
private String name;
private String type;
public Animal(String name, String type) {
super();
this.name = name;
this.type = type;
}
 @Override
public String toString() {
return "Animal [name=" + name + ", type=" + type + "]";
}
}
public class Day1501 {
public static void main(String[] args) {
Animal a1 = new Animal("旺财", "中华田园犬");
//可以重写toString方法,来输出对象的内容
String str1 = a1.toString();
System.out.println(str1);//
// println方法内部会调用对象a1的toString方法
System.out.println(a1);//
}
}

Object类的equals方法用于比较两个对象是否相等. Object类的equals方法是使用==来比较.==比较的是对象的引用是否相等.

package day15;
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
}
} 
public class Day1502 {
public static void main(String[] args) {
Client c1 = new Client(25);
Client c2 = new Client(25);
//比较两个对象的地址是否相等
System.out.println(c1 == c2);//false
//Object类的equals方法内部使用==比较两个对象,比较的是地址是否相等
System.out.println(c1.equals(c2));//false
}
}

可以重写euqals方法来比较对象的属性。

package day15;
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
} @
Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
} /
/重写equals方法,这里认为只要id相等,对象就相等
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
 return false;
if (getClass() != obj.getClass())
return false;
Client other = (Client) obj;
if (id != other.id)
return false;
return true;
}
}
public class Day1502 {
public static void main(String[] args) {
Client c1 = new Client(25);
Client c2 = new Client(25);
//比较两个对象的地址是否相等
System.out.println(c1 == c2);//false
//可以重写Object类的equals方法,来提供自定义的实现
System.out.println(c1.equals(c2));//true
Client c3 = null;
c3.equals(c2);//java.lang.NullPointerException
}
}

hashCode方法 Object类的hashCode方法将返回对象的哈希码,哈希码是一个整数值。 Object类的hashCode方法
简单的使用对象的内存地址作为哈希码,但是并不符合hashCode方法的意义。
哈希码是对于对象的属性使用hash算法得到的一个整数值。这个整数值相当于对象的摘要。相同对象的哈希码总是相同的,不同对象的哈希码也可能相同。
可以重写hashCode方法,以保证hashCode方法的意义。
如果重写了equlas方法,那么也应该重写hashCode方法,从而保证相同的对象的哈希码也相同。 hashCode方法应用于集合中的数据结构:哈希表。
如果要将对象放入哈希表中,那么必须重写equals和hashCode方法。

package day15;
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
} /
/重写equals方法,这里认为只要id相等,对象就相等
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())return false;
Client other = (Client) obj;
if (id != other.id)
return false;
return true;
}
} 
public class Day1503 {
public static void main(String[] args) {
Animal a1 = new Animal("旺财", "中华田园犬");
Animal a2 = new Animal("小花", "咖啡猫");
//Object类的hashCode方法将返回对象的哈希码,哈希码是一个整数值。
int h1 = a1.hashCode();
int h2 = a2.hashCode();
System.out.println(h1);//366712642
System.out.println(h2);//1829164700
Client c1 = new Client(25);
Client c2 = new Client(25);
//如果重写了equals而没有重写hashCode,那么会导致相同对象的哈希码不同
//这样不符合hashCode的意义
//因此,如果重写了equals方法,也应该重写hashCode方法
//这样才能保证相同对象的哈希码总是相同的
System.out.println(c1.equals(c2));//true
System.out.println(c1.hashCode());//2018699554
System.out.println(c2.hashCode());//1311053135
}
}

package day15;
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
} /
/如果重写了equals方法,也应该重写hashCode方法
//这样才能保证相同对象的哈希码总是相同的
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
} /
/重写equals方法,这里认为只要id相等,对象就相等
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Client other = (Client) obj;
if (id != other.id)
return false;
return true;
}
}
 public class Day1503 {
public static void main(String[] args) {
Animal a1 = new Animal("旺财", "中华田园犬");
Animal a2 = new Animal("小花", "咖啡猫");
//Object类的hashCode方法将返回对象的哈希码,哈希码是一个整数值。
int h1 = a1.hashCode();
int h2 = a2.hashCode();
System.out.println(h1);//366712642
System.out.println(h2);//1829164700
Client c1 = new Client(25);
Client c2 = new Client(25);
System.out.println(c1.equals(c2));//true
System.out.println(c1.hashCode());//56
System.out.println(c2.hashCode());//56
}
}

包装类

包装类将基本类型的值包装成一个对象。 基本数据类型:boolean,byte,char ,double, float, int , long,short 包装 类:Boolean,Byte,Character,Double,Float,Integer,Long,Short 包装类对象有一个基本类型的属性value用于保存基本类型的数据。
使用构造方法创建包装类对象:

package day15;
public class Day1504 {
public static void main(String[] args) {
// 创建了一个Integer类的对象,它所保存的数据是10
// Integer类的对象的值是不可变的
Integer i = new Integer(10);
Boolean b = new Boolean(true);
Byte by = new Byte((byte) 10);
Short s = new Short((short) 12);
Character c = new Character('C');
Long l = new Long(100L);
Float f = new Float(55.0f);
Double d = new Double(55.0);
Integer in = null;
}
}

使用包装类的原因: 1,基本数据类型不允许null值,使用包装类可以。 2,集合中不允许使用基本数据类型,可以
使用包装类。 3,类型转换。

package day15;
public class Day1505 {
public static void main(String[] args) {
String s1 = "123";
// Integer.parseInt可以将String转换为int
int i1 = Integer.parseInt(s1);
System.out.println(i1);
// 对象变量i为空,说明i不引用任何对象
Integer i = null;
//强制类型转换
int a = 10;
short s = (short) a;
//使用包装类进行类型转换
i = new Integer(10);
// 获取short值
short ss = i.shortValue();
}
}

使用包装类的方法: valueOf方法,替代构造器,提供了另外一种方式创建包装类对象

   package day15;
    public class Day1506 {
    public static void main(String[] args) {
    Integer i1 = new Integer(10);
    Integer i2 = Integer.valueOf(10);
    Integer i3 = Integer.valueOf("10");
    //第一个参数是字符串,第二个参数是进制
    Integer i4 = Integer.valueOf("111", 2);// 7
    System.out.println(i1);// 10
    System.out.println(i2);// 10
   System.out.println(i3);// 10
   System.out.println(i4);// 7
}
}

xxxValue方法,可以将包装类对象转换为基本类型

package day15;
public class Day1507 {
public static void main(String[] args) {
Integer i1 = Integer.valueOf(10);
int i2 = i1.intValue();
float i3 = i1.floatValue();
System.out.println(i1);// 10
System.out.println(i2);// 10
System.out.println(i3);// 10.0
Long.valueOf(10L).intValue();
}
}

parseXxx方法,类似valueOf方法,但是此方法将返回基本数据类型

package day15;
public class Day1508 {
public static void main(String[] args) {
int i1 = Integer.parseInt("10");
System.out.println(i1);//10
int i2 = Integer.parseInt("11101", 2);
System.out.println(i2);//29
}
}
静态的toString方法,将基本数据类型转换为字符串

package day15;
public class Day1509 {public static void main(String[] args) {
String str1 = Integer.toString(10);
System.out.println(str1);//10
String str2 = Integer.toString(7, 2);
System.out.println(str2);//111
//0123456789abcdef
System.out.println(Integer.toHexString(12));//十六进制,c
System.out.println(Integer.toBinaryString(10));//二进制,1010
//01234567
System.out.println(Integer.toOctalString(10));//八进制,12
//1+8+64
System.out.println(Integer.valueOf("111", 8));//73
}
}

自动装箱和自动拆箱

package day15;
public class Day1510 {
public static void main(String[] args) {
// 自动装箱,将1包装成一个Integer对象,然后将地址赋值给i1
// 编译后,调用类方法Integer.valueOf(int i);
// 相当于Integer i1 = Integer.valueOf(1);
Integer i1 = 1;//自动装箱
// 自动拆箱,取出i1所引用对象的值,然后赋值给i2
// 编译后,调用实例方法i1.intValue();
// 相当于int i2 = i1.intValue();
int i2 = i1;//自动拆箱
System.out.println(i1);//1
System.out.println(i2);//1
Integer ten = new Integer(10);
ten++;// 先自动拆箱,再自动装箱
}
}

package day15;
public class Day1511 {
public static void f1(Integer i){
System.out.println(i);
} p
ublic static void f2(int i){
System.out.println(i);} p
ublic static void main(String[] args) {
//赋值和传参是一样的
f1(1);//自动装箱
f2(new Integer(2));//自动拆箱
}
}

包装类的缓存

public class Day1513 {
public static void main(String[] args) {
Integer nineA = new Integer(9);
Integer nineB = new Integer(9);
System.out.println(nineA == nineB);//false
System.out.println(nineA.equals(nineB));//true
Integer i1 = 9;//自动装箱,Integer i1 = Integer.valueOf(9);
Integer i2 = 9;//自动装箱,Integer i2 = Integer.valueOf(9);
//使用Integer.valueOf(int i)方法,如果字面值是-128-127,就会从缓存中获取一个对象
System.out.println(i1 == i2);//true
System.out.println(i1.equals(i2));//true
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值