Java自学-数字与字符串 装箱和拆箱
Java中基本类型的装箱和拆箱
步骤 1 : 封装类
所有的基本类型,都有对应的类类型
比如int对应的类是Integer
这种类就叫做封装类
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//把一个基本类型的变量,转换为Integer对象
Integer it = new Integer(i);
//把一个Integer对象,转换为一个基本类型的int
int i2 = it.intValue();
}
}
步骤 2 : Number类
数字封装类有
Byte,Short,Integer,Long,Float,Double
这些类都是抽象类Number的子类

package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
Integer it = new Integer(i);
//Integer是Number的子类,所以打印true
System.out.println(it instanceof Number);
}
}
步骤 3 : 基本类型转封装类
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本类型转换成封装类型
Integer it = new Integer(i);
}
}
步骤 4 : 封装类转基本类型
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本类型转换成封装类型
Integer it = new Integer(i);
//封装类型转换成基本类型
int i2 = it.intValue();
}
}
步骤 5 : 自动装箱
不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本类型转换成封装类型
Integer it = new Integer(i);
//自动转换就叫装箱
Integer it2 = i;
}
}
步骤 6 : 自动拆箱
不需要调用Integer的intValue方法,通过=就自动转换成int类型,就叫拆箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
Integer it = new Integer(i);
//封装类型转换成基本类型
int i2 = it.intValue();
//自动转换就叫拆箱
int i3 = it;
}
}
步骤 7 : int的最大值,最小值
int的最大值可以通过其对应的封装类Integer.MAX_VALUE获取
package digit;
public class TestNumber {
public static void main(String[] args) {
//int的最大值
System.out.println(Integer.MAX_VALUE);
//int的最小值
System.out.println(Integer.MIN_VALUE);
}
}
练习: 装箱拆箱
对byte,short,float,double进行自动拆箱和自动装箱
byte和Integer之间能否进行自动拆箱和自动装箱
通过Byte获取byte的最大值
答案:
package digit;
public class TestNumber {
public static void main(String[] args) {
// 1. 对byte,short,float,double进行自动拆箱和自动装箱
byte b = 1;
short s = 2;
float f = 3.14f;
double d = 6.18;
// 自动装箱
Byte b1 = b;
Short s1 = s;
Float f1 = f;
Double d1 = d;
// 自动拆箱
b = b1;
s = s1;
f = f1;
d = d1;
// 2. byte和Integer之间能否进行自动拆箱和自动装箱
Integer i1 = b; //不能把byte直接自动装箱成Integer
b = new Integer(1); //也不能把Integer自动拆箱成 byte
// 3. 通过Byte获取byte的最大值
System.out.println(Byte.MAX_VALUE);
}
}
Java自学-数字与字符串 装箱和拆箱的更多相关文章
- Java学习笔记之——自动装箱与拆箱
自动装箱与拆箱 基本类型与引用类型的互相转换 1. 基本类型对应的包装类 byte short char int long flaot double ...
- java和c#中的装箱和拆箱操作
c#装箱和拆箱 装箱:整体上来说,装箱是将值类型转换成引用类型,比如将Vector3转换成Object类型. 具体而言: 1)在托管堆中为值类型分配内存.除了原始的数值以外还应该有指向该数值的引用. ...
- Java包装类,基本的装箱与拆箱
我的博客 何为包装类 将原始类型和包装类分开以保持简单.当需要一个适合像面向对象编程的类型时就需要包装类.当希望数据类型变得简单时就使用原始类型. 原始类型不能为null,但包装类可以为null.包装 ...
- Java自学-数字与字符串 字符
Java中的字符 示例 1 : 保存一个字符的时候使用char package character; public class TestChar { public static void main(S ...
- Java自学-数字与字符串 字符串转换
Java中把数字转换为字符串,字符串转换为数字 步骤 1 : 数字转字符串 方法1: 使用String类的静态方法valueOf 方法2: 先把基本类型装箱为对象,然后调用对象的toString pa ...
- Java自学-数字与字符串 字符串
Java中的字符串String 示例 1 : 创建字符串 字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象 常见创建字符串手段: 每当有一个字面值出现的时候,虚拟机就会创 ...
- Java自学-数字与字符串 格式化输出
Java 使用printf或format 进行格式化输出 步骤 1 : 格式化输出 如果不使用格式化输出,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐 使用格式化输出,就可以简洁明了 %s ...
- Java自学-数字与字符串 MyStringBuffer
自己开发一个Java StringBuffer 根据接口IStringBuffer ,自己做一个MyStringBuffer 步骤 1 : IStringBuffer接口 package charac ...
- Java自学-数字与字符串 StringBuffer
Java StringBuffer常见方法 StringBuffer是可变长的字符串 示例 1 : 追加 删除 插入 反转 append追加 delete 删除 insert 插入 reverse 反 ...
随机推荐
- ActiveMQ 入门和与 Spring 整合
ActiveMQ 入门演示 activemq 依赖 <dependency> <groupId>org.apache.activemq</groupId> < ...
- python基础之三:int、bool、str
一.数据类型之整型的函数使用 i = print("该整型数字所占有效比特位的长度是:%d" % i.bit_length()) print(i.to_bytes(, " ...
- 前端js判空处理,js字符串判空,js数组判空
1.字符串 在 js 中,字符串为空会有这么几种形式,"",null,undefined,如果在已知变量为空串的情况下可以直接采用 if (string.length == 0) ...
- 10-排序5 PAT Judge (25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- es6 - spreed & rest 【... 扩展运算符】
扩展运算符:…运算符 好处:简化书写长度,提升开发效率. 具备两个功能: 1.展开功能 2.收集功能 所以…运算符又可以叫做展开收集运算符. 他的不同作用需要在不同场景中使用才会出现: a.读 - 展 ...
- 【操作系统之三】Linux下进程间通信-IPC(Inter-Process Communication)
管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信:信号(Sign ...
- Maven 教程(7)— Maven使用的最佳实践
原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79544201 1.设置MAVEN_OPTS环境变量 通常需要设置MAVEN_OP ...
- wordpress防止垃圾邮件方法
wordpress防止垃圾邮件方法 安装NoSpamNX插件然后设置Operating mode 为 Block (recommended) save
- maven-3.6.1
1.下载 cd /opt/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-m ...
- ASP.NET-------gridview 进行编辑的时候,给出提示
在使用gridview 控件的时候,控制修改人的操作行为,并给出合理的提示, 比如 在执行编辑操作的时候 不允许姓名为空,并显示出提示,姓名不可以为空 操作: 前台页面,对一些字段的解释 一定要注意 ...