包装类

包装类有哪些

八种基本数据类型:byte short int long float double char boolea
对应的包装类: Byte Short Integer Long Float Double Character Boolean

基本数据类型Value
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

基本数据类型和包装类的相互转换

将八种数据类型转换成对应的包装类

使用构造器进行转换

int i  = 10;
Integer integer = new Integer(i);
float f = 1.3f;
Float f1 = new Float(f);
char a = 'A';
Character character = new Character(a);

将包装类转换成八种基本数据类型

jdk 1.5的时候提供了自动拆箱和装箱操作,八种基本数据类型和包装类可以直接进行转换

 Integer i = new Integer(1);
 int i1  = i;

将八种数据类型转换成String

用String提供的重载valueOf

int i = 15;
String str = String.valueOf(i);

将String类转换成基本数据类型

例如:String–>int

String str ="132";
int i = Integar.perseInt(str);

包装类面试题

Object o1 =  true ? new Integer(1) : new Double(2.0);
System.out.println(o1);

问:o1是多少?
错误答案:1,地址
正确答案: 1.0

 
  Integer i  = new Integer(1);
  Integer j =  new Integer(2);
  System.out.println(i == j);
  

正确答案: false

Integer m  =1;
  Integer n = 1;
  System.out.println(m ==n);

正确答案:true

 Integer x = 128;
  Integer y = 128;
  System.out.println(x == y);

正确答案:false

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值