首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
软件与程序人生
›
后端开发
›
.Net
›
Java面向对象(六)
返回列表
发新帖
Java面向对象(六)
[复制链接]
发表于 2022-8-27 06:37:47
|
显示全部楼层
|
阅读模式
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
Java面向对象(六)
目录
Java面向对象(六)
十九、包装类
19.1 八种基本类型包装类
19.2 基本类型、包装类与 String 类间的转换。
19.3 基本数据类型转换为包装类(装箱)
19.4 包装类转换为基本数据类型(拆箱)
19.5 自动装箱拆箱
19.6 基本数据类型、包装类转换为String类型
19.7 String类型转换为基本数据类型、包装类
19.8 特殊例子
十九、包装类
19.1 八种基本类型包装类
java 提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征。
19.2 基本类型、包装类与 String 类间的转换。
19.3 基本数据类型转换为包装类(装箱)
public void test1(){
public static void main(String[] args){
int num1 = 10;
// System.out.println(num1.toString()); 报错
Integer in1 = new Integer(num1); //通过构造器装箱
System.out.println(in1.toString());
Integer in2 = new Integer("123"); //通过字符串参数装箱
System.out.println(in2.toString());
//报异常
// Integer in3 = new Integer("123abc");
// System.out.println(in3.toString());
Float f1 = new Float(12.3f); //通过构造器装箱
Float f2 = new Float("12.3"); //通过字符串参数装箱
System.out.println(f1);
System.out.println(f2);
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("TrUe");
System.out.println(b2); //true
/* b2 返回 true 原因:
public Boolean(String s) {
this(parseBoolean(s));
}
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
如果给定对象与字符串相等,则返回 true,否则返回 false。
*/
Boolean b3 = new Boolean("true123");
System.out.println(b3); //false
Order order = new Order();
System.out.println(order.isMale); //false(基本数据类型默认值)
System.out.println(order.isFemale); //null(引用数据类型默认值)
}
}
class Order{
boolean isMale;
Boolean isFemale;
}
复制
代码
19.4 包装类转换为基本数据类型(拆箱)
public void test2(){
public static void main(String[] args){
Integer in1 = new Integer(12);
int i1 = in1.intValue(); //调用类方法xxxValue()
System.out.println(i1 + 1); // 13
Float f1 = new Float(12.3);
float f2 = f1.floatValue(); //调用类方法xxxValue()
System.out.println(f2 + 1); // 13.3
}
}
复制
代码
19.5 自动装箱拆箱
// JDK 5.0 新特性:自动装箱 与自动拆箱
public void test3(){
int num1 = 10;
Integer in1 = num1; //自动装箱
boolean b1 = true;
Boolean b2 = b1; //自动装箱
System.out.println(in1.toString());
int num3 = in1; //自动拆箱
}
复制
代码
19.6 基本数据类型、包装类转换为String类型
// String类型:调用String重载的valueOf(Xxx xxx)
public void test4(){
public static void main(String[] args){
int num1 = 10;
//方式1:连接运算
String str1 = num1 + "";
//方式2:调用String的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2); //"12.3"
System.out.println(str3); //"12.4"
}
}
复制代码
19.7 String类型转换为基本数据类型、包装类
// 调用包装类的parseXxx(String s)
public void test5(){
public static void main(String[] args){
String str1 = "123";
//错误的情况:
// int num1 = (int)str1;
// Integer in1 = (Integer)str1;
int num2 = Integer.parseInt(str1);
//当字符串中不是纯数字时,可能会报NumberFormatException
System.out.println(num2 + 1);
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1); //false
}
}
复制代码
19.8 特殊例子
public void test6(){
public static void main(String[] args){
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);
// 输出 1.0,三元运算符要求两边表达式数据类型一致,左边 Integer 类型自动类型转换为 Double
}
}
复制代码
public void method1() {
public static void main(String[] args){
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j); //false
Integer m = 1;
Integer n = 1;
System.out.println(m == n); //true
Integer x = 128; //相当于new了一个Integer对象
Integer y = 128; //相当于new了一个Integer对象
System.out.println(x == y); //false
}
}
/*
Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了。
目的:提高效率
详细可以看 Integer 源码
*/
复制代码
回复
使用道具
举报
返回列表
民工心事
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表