Java 包装类的使用 + 小练习
1 package com.bytezreo.ut; 2 3 import org.junit.Test; 4 5 /** 6 * 7 * @Description 包装类的使用 8 * @author Bytezero·zhenglei! Email:420498246@qq.com 9 * @version 10 * @date 下午3:59:43 11 * @ 1.Java提供了8种数据类型对应的包装类,使得基本数据类型的变量具有类的特征。 12 * 13 * 2.基本数据类型,包装类,String三者之间的转换 14 * 15 */ 16 public class WrapperTest 17 { 18 //String类型 ---->基本数据类型,包装类:调用包装类的parseXxx(String s) 19 @Test 20 public void test5() 21 { 22 String str1 = "123"; 23 24 //错误情况 25 // int num1 = (int)str1; 26 27 // Integer in1 = (Integer)str1; 28 29 //可能会报 NumberFormatException 30 int num2 = Integer.parseInt(str1); 31 System.out.println(num2 + 3); 32 33 String str2 = "true"; 34 boolean b1 = Boolean.parseBoolean(str2); 35 System.out.println(b1); 36 37 38 } 39 40 41 42 43 //基本数据类型,包装类------>String类型:调用String重载的valueOf(Xxx xxx) 44 @Test 45 public void test4() 46 { 47 int num1 = 10; 48 //方式一:连接运算 49 String str1 = num1+""; 50 51 //方式二:调用String重载的valueOf(Xxx xxx) 52 53 float f1 = 12.3f; 54 String str2 = String.valueOf(f1);//"12.3" 55 System.out.println(str2.toString()); 56 57 58 Double d1 = new Double(12.4); 59 String str3 = String.valueOf(d1); 60 System.out.println(str3); //"12.4" 61 62 63 64 65 } 66 67 68 69 70 71 /* 72 * JDK 5.0新特性:自动装箱与自动拆箱 73 * 74 */ 75 76 @Test 77 public void test3() 78 { 79 // int num1 = 10; 80 // 81 // //基本数据类型----->包装类的对象 82 // method(num1); 83 84 //自动装箱: 基本数据类型----->包装类的对象 85 int num2 = 10; 86 Integer in1 = num2; //自动装箱 87 88 boolean b1 = true; 89 Boolean b2 = b1; //自动装箱 90 91 92 93 //自动拆箱:包装类 -----> 基本数据类型 94 System.out.println(in1.toString()); 95 96 int num3 = in1; //自动拆箱 97 System.out.println(num3); 98 99 100 101 } 102 public void method(Object obj) 103 { 104 System.out.println(obj); 105 } 106 107 108 109 110 111 112 //包装类转 ------> 化为基本数据类型:调用包装类的xxxValie() 113 @Test 114 public void test2() 115 { 116 Integer in1 = new Integer(12); 117 118 int i1 = in1.intValue(); 119 System.out.println(i1 + 2); 120 121 122 123 Float f1 = new Float(12.3); 124 float f2 = f1.floatValue(); 125 System.out.println(f2+5); 126 127 } 128 129 130 131 132 133 //基本数据类型----->包装类:调用包装类的构造器 134 @Test 135 public void test1() 136 { 137 int num1 = 10; 138 139 Integer in1 = new Integer(num1); 140 141 142 System.out.println(in1.toString()); 143 144 Integer in2 = new Integer("123"); 145 System.out.println(in2.toString()); 146 147 148 //异常 149 // Integer in3 = new Integer("123abc"); 150 // System.out.println(in3.toString()); 151 152 153 Float f1 = new Float(12.3f); 154 155 Float f2 = new Float("12.3"); 156 157 System.out.println(f1.toString()); 158 System.out.println(f2); 159 160 161 Boolean b1 = new Boolean(true); 162 Boolean b2 = new Boolean("true"); 163 Boolean b3 = new Boolean("true123"); 164 165 System.out.println(b1); 166 System.out.println(b2); 167 System.out.println(b3); //false 168 169 170 Order order = new Order(); 171 System.out.println(order.isMale); //false 172 System.out.println(order.isFemale); //null 173 174 175 } 176 177 /* 178 * 小练习 179 * 180 */ 181 @Test 182 public void test6() 183 { 184 Object o1 = true ? new Integer(1):new Double(2.0); 185 System.out.println(o1); //1.0 类型同一 自动类型提升 186 } 187 188 @Test 189 public void test7() 190 { 191 Object o2; 192 if(true) 193 o2 = new Integer(1); 194 else 195 o2 = new Double(2.0); 196 System.out.println(o2); //1 197 } 198 199 @Test 200 public void method1() 201 { 202 Integer i = new Integer(1); 203 Integer j = new Integer(1); 204 205 System.out.println(i == j); //false 地址不相等 206 207 // Integer内部定义了IntegerCache结构, IntegerCache中定义了Integer[], 208 //保存了-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围 209 //在-128~127范围内时,可以直接使用数组中的元素,不用在new了。 210 // 目的提高效率 211 Integer m = 1; 212 Integer n = 1; 213 214 System.out.println(m == n); // true 215 216 217 Integer x = 128; //不在int 的取值范围内 同时new了两个 128 所以不相同 218 219 Integer y = 128; 220 System.out.println(x == y); //false 221 } 222 223 224 225 } 226 227 228 class Order{ 229 boolean isMale; //这是基本数据类型 230 231 Boolean isFemale; //这是类 232 233 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15334934.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)