包装类
2020-09-05 本文已影响0人
有腹肌的豌豆Z
为什么需要包装类(Wrapper Class)
- java并不是纯面向对象的语言,java语言是一个面向对象的语言,但是java中的基本数据类型却不是面向对象的,但是我们在实际使用中经常将基本数据类型转换成对象,便于操作,比如,集合的操作中,这时,我们就需要将基本类型数据转化成对象!
包装类和基本数据类型的关系
- 基本数据类型 包装类
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double
包装类的继承关系

包装类的基本操作

具体操作
public class TestInteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i1=new Integer(123);
Integer i2 = new Integer("123");
System.out.println("i1=i2:"+(i1==i2));//false
System.out.println("i1.equals(i2):"+i1.equals(i2));
System.out.println(i2);
System.out.println(i2.toString());//说明重写了toString方法
Integer i3=new Integer(128);
System.out.println(i1.compareTo(i3));//-1
System.out.println(i1.compareTo(i2));//0
System.out.println(i3.compareTo(i2));//1
//(1)Integer-->int 包装对象.intValue()
int i=i1.intValue();
System.out.println(Integer.max(10, 20));//返回最大值
//(2)String -->int 包装类类名.parseInt(String s)
int ii=Integer.parseInt("234");
//(3)int -->Integer
Integer i4=Integer.valueOf(123);
//(4)int-->String
String str=ii+"";
String s=String .valueOf(ii);
//(5)String-->Integer;
Integer i5=new Integer("345");
//(6)Integer-->String
String ss=i5.toString();
System.out.println(ss);
}
}