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 反 ...
随机推荐
- Hibernate框架学习3
一对多|多对一 一对多 多对一 级联操作 结论: 简化操作.一定要用,save-update,不建议使用delete. 关系维护 在保存时.两方都会维护外键关系.关系维护两次,冗余了. 多余的维护关系 ...
- 【Matplotlib】使用速记
[持续更新] pyplot matplotlib.pyplot is a state-based interface to matplotlib. It provides a MATLAB-like ...
- [RN] React Native Image 实现placeholder占位图
React Native Image 实现placeholder占位图 react-native Image没有placeholder这样的props,但是业务有需要这种场景, 解决方法为: 使用Im ...
- 在Hadoop-3.1.2上安装HBase-2.2.1
目录 目录 1 1. 前言 3 2. 缩略语 3 3. 安装规划 3 3.1. 用户规划 3 3.2. 目录规划 4 4. 相关端口 4 5. 下载安装包 4 6. 修改配置文件 5 6.1. 修改策 ...
- 记一次生产kafka消息消费的事故
事故背景: 我们公司与合作方公司有个消息同步的需求,合作方是消息生产者,我们是消息消费者,他们通过kafka给我们推送消息,我们实时接收,然后进行后续业务处理.昨天上午,发现他们推送过来的广场门店信息 ...
- 基于Linux(中标麒麟)上QT的环境搭建
最近由于公司需要,需要在中标麒麟上进行QT的二次开发,但是网上的资料很少,就算是有也基本都是其他的版本的Linux上的搭建.中标麒麟本身的资料也很好,而且还只能试用60天. 下面就介绍下我对此环境的搭 ...
- JS 数组,对象常用方法 集合
数组 1.数组去重: 主要是使用的 new Set() 方法 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Referen ...
- How to convert a std::string to const char* or char*?
How to convert a std::string to const char* or char*? 1. If you just want to pass a std::string to a ...
- SpringMVC拦截器与SpringBoot自定义拦截器
首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...
- 【Activiti学习之二】Activiti API(一)
环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.Activiti数据查询准备数据: package com.wjy.act; imp ...