JavaSE中字符串与基本数据类型、包装类型转

包装类简介

基本数据类型不是对象层次结构的组成部分。有时需要像处理对象一样处理这些基本数据类型,可通过相应的“包装类”来将其“包装”后使用。

基本数据类型和对应包装类:

数据类型包装类
booleanBoolean
byteByte
charCharacter
doubleDouble
floatFloat
intInteger
longLong
shortShort

字符串与基本数据类型、包装类型转换图

在这里插入图片描述

基本数据类型转换为包装类

int型通过构造器的器转换为Integer包装类
通过构造器转换为

int pInt = 500;
Integer wInt = new Integer(pInt);

字符串转换为包装类

字符串通过构造方法转换为包装类

String sInt =500;
Integer wInt = new Integer(sInt);

字符串通过包装类的valueOf(String s)转换为包装类

String sInt =500;
Integer wInt = Integer.valueOf(sInt);

字符串不能通过以上两种方式转换为Character

包装类通过xxxValue()方法转换为基本数据类型

Integer wInt = new Integer(500);
int pInt = wInt.intValue();

包装类转换为字符串

包装类通过toString()方法转换为字符串

Integer wInt = new Integer(500);
String sInt = wInt.toString();

字符串转换为基本数据类型

字符串通过parseXXX(String s)方法转换为基本数据类型

String sInt =500;
int pInt = Integer.parseInt (sInt);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值