针对八种基本定义相应的引用类型—包装类(封装类)
有了类的特点,就可以调用类中的方法。
基本数据类型 | 包装类 |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
double | Double |
(1) 通过包装类的构造器实现:
int i = 500; Integer t = new Integer(i);
(2) 还可以通过字符串参数构造包装类对象:
Float f = new Float(“4.56”);
Long l = new Long(“asdf”); //NumberFormatException
调用包装类的 .xxxValue() 方法:
boolean b = bObj.booleanValue();
注
:JDK1.5 之后,支持自动装箱,自动拆箱。但类型必须匹配。
package day08;
public class Test {
public static void main(String[] args) {
Integer i = new Integer(112);
Integer i1 = 112;//自动装箱
int i0 = i.intValue();
int i2 = i1;//自动拆箱
System.out.println(i0);
// boolean b = new Boolean("false").booleanValue();
boolean b = new Boolean("false");//自动拆箱
Boolean b1 = true;//自动装箱
System.out.println(b);
}
}
(1) 通过包装类的构造器实现:
int i = new Integer("12");
(2) 通过包装类的 parseXxx(String s) 静态方法:
Float f = Float.parseFloat("12.1");
例:
package day08;
public class Test {
public static void main(String[] args) {
int i = Integer.parseInt("123");
float f = Float .parseFloat("0.40");
boolean b = Boolean.parseBoolean("flase");
}
}
(1) 调用字符串重载的 valueOf() 方法:
String fstr = String.valueOf(2.34f);
例:
package day08;
public class Test {
public static void main(String[] args) {
//字符串转换成基本数据类型
int i = Integer.parseInt("123");
float f = Float .parseFloat("0.40");
boolean b = Boolean.parseBoolean("flase");
//基本数据类型转换成字符串
String istr = String.valueOf(i);
String fstr = String.valueOf(f);
String bstr = String.valueOf(b);
}
}
重点:基本数据类型的包装类:主要作用就是基本数据类型与字符串直接的转化
(2) 更直接的方式:
String intStr = 5 + ""
父类Object的toString方法就是输出当前对象的内存地址(如果想要输出类的其他信息,重写toString方法)
举例:(MyDate.java和Test.java在包day08中)
MyDate类:
package day08;
public class MyDate {
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
int year;
int month;
int day;
@Override
public boolean equals(Object obj) {
int flag = 1;
if(obj instanceof MyDate) {
MyDate md = (MyDate) obj;
flag = 0;
if(this.year != md.year) {
flag += 1;
}
if(this.month != md.month) {
flag += 1;
}
if(this.day != md.day) {
flag += 1;
}
}
if(flag == 0) {
return true;
}else {
return false;
}
}
}
Test.java:
package day08;
public class Test {
public static void main(String[] args) {
MyDate m = new MyDate(2023,12,12);
System.out.println(m.toString());//输出: day08.MyDate@2133c8f8
System.out.println(m); //输出: day08.MyDate@2133c8f8
}
}
运行结果:
MyDate.java:
package day08;
public class MyDate {
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
int year;
int month;
int day;
@Override
public boolean equals(Object obj) {
int flag = 1;
if(obj instanceof MyDate) {
MyDate md = (MyDate) obj;
flag = 0;
if(this.year != md.year) {
flag += 1;
}
if(this.month != md.month) {
flag += 1;
}
if(this.day != md.day) {
flag += 1;
}
}
if(flag == 0) {
return true;
}else {
return false;
}
}
@Override
public String toString() {
String str = this.year + "-" + this.month + "-" + this.day;
return str;
}
}
Test.java:
package day08;
public class Test {
public static void main(String[] args) {
MyDate m = new MyDate(2023,12,12);
System.out.println(m.toString());
System.out.println(m);//打印 m 对象相当于执行打印 m.toString()
}
}
运行结果: