廖雪峰Java2面向对象编程-6Java核心类-3包装类型
Java的数据类型:
- 基本类型:int boolean float
- 引用类型:所有class类型

为一个基本类型int赋值为null,会提示“incompatible types”错误。
错误原始:基本类型是不能视为对象(引用类型)的.。
可以定义一个Integer类,包含一个实例字段int。这样可以把Integer视为int的包装类型wrapper。
包装类型本身是引用类型,只是其内部持有一个基本类型的变量。
JDK为每种基本类型都创建了对应的包装类型:
| 基本类型 | 对应的引用类型 |
|---|---|
| boolean | Boolean |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
使用JDK的Integer类传入null,语句不会再报错,只是执行时会抛出java.lang.NumberFormatException
Integer n = new Integer(null);
int i = n;
System.out.println(i);

类型转换
int i = 100;
//把int、String转换为Integer
Integer n1 = new Integer(i);
Integer n2 = Integer.valueOf(i);
Integer n3 = Integer.valueOf("100");
//把Integer、String转换为int类型
int x1 = n1.intValue();
int x3 = Integer.parseInt("100");
//把Integer、int转换为String类型
String s1 = n1.toString();
String s2 = String.valueOf(i);
自动拆箱和自动装箱
编译器何以自动在int和Integer之间转型。
- 自动装箱auto boxing int -> Integer
Integer n = 99;//自动调用Integer.valueOf(99)
- 自动拆箱auto unboxing Integer -> int
int i = n;//n.intValue()
注意:
* 自动装箱和自动拆箱只发生在编译阶段
* 装箱和拆箱会影响执行效率
* 编译后的class代码是严格区分基本类型和引用类型的,没有自动装箱和拆箱
* Integer的null->int执行时会抛出<font color=#FF0000>java.lang.NumberFormatException</font>
包装类型定义了一些有用的静态变量
```#java
Boolean t = Boolean.TRUE;
Boolean f = Boolean.FALSE;
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
int sizeOfLong = Long.SIZE;
int bytesOfLong = Long.BYTES;
Java的包装类型全部继承自Number这个class。可以利用向上转型,把任意一种包装类型先变为Number对象,利用Number提供的Byte、Integer、Float变为任意的类型。
Integer n = 5 + Integer.valueOf(10);
Number m = n;
System.out.println(m.floatValue());//15.0
总结:
JDK的包装类型可以把基本类型包装为class
自动装箱和自动拆箱是编译器完成的 (JDK>= 1.5)
装箱和拆箱会影响执行效率
注意拆箱时kennel发生NullPointException
廖雪峰Java2面向对象编程-6Java核心类-3包装类型的更多相关文章
- 廖雪峰Java2面向对象编程-6Java核心类-2StringBuilder
字符串操作过程中,每次用 + 拼接字符串,有以下问题: 每次循环都会创建新的对象 绝大部分都是临时对象.浪费内存 影响GC效率 String s = ""; for(int i=0 ...
- 廖雪峰Java2面向对象编程-6Java核心类-1字符串和编码
Java的字符串用String表示 1.String特点: 可以直接使用"..."表示一个字符串,不强制使用new String 内容不可变 String s1 = "h ...
- 廖雪峰Java2面向对象编程-6Java核心类-6常用工具类
1.Math Math提供了数学计算的静态方法 序号 方法 描述 1 abs() 返回参数的绝对值.Math.abs(-9)//9 2 ceil() 返回大于等于( >= )给定参数的的最小整数 ...
- 廖雪峰Java2面向对象编程-6Java核心类-5枚举类
直接定义常量 public class Weekday { //定义int常量 public static final int SUN = 0; public static final int MON ...
- 廖雪峰Java2面向对象编程-6Java核心类-4JavaBean
1.JavaBean定义 符合以下命名规范的class被成为JavaBean private 类型的field 针对这个field的get和set方法 public class Person { pr ...
- 廖雪峰Java2面向对象编程-3继承和多态-1继承
1.继承 继承是一种代码复用的方式. Student与Person有相同部分的代码. Student可以从Person继承,这样Student获得了Person的所有功能,只需要编写新增的功能即可.通 ...
- 廖雪峰Java2面向对象编程-1面向对象-1面向对象基础
1.对象的概念 面向对象编程:Object-Oriented Programming 对现实世界建立计算机模型的一种编程方法. 现实世界 计算机模型 Java代码 人 类/class class Pe ...
- 廖雪峰Java2面向对象编程-5包和classpath-4classpath和jar
1.classpath 1.1classpath定义 classpath是一个环境变量 classpath指示JVM如何搜索class classpath设置的搜索路径与操作系统相关 * window ...
- 廖雪峰Java2面向对象编程-4抽象类和接口-2接口
1.接口的定义 抽象方法本质上是定义接口规范. 在抽象类中定义了一个抽象方法,子类中必须实现这个抽象方法. public abstract class Person{ public abstract ...
随机推荐
- 基于BP的B/S架构破解
思路历程: 1.获取用户名 2.获取密码字典 3.使用BP爆破 案例: 管理人员在平时的网络生活中没有良好的个人信息保护,让不法份子有机可乘.例如WordPress等的类似网站使用PHP开发,虽然功能 ...
- hdu 1556 A - Color the ball 数状数组做法
#include<bits/stdc++.h> using namespace std; ; int n; int c[maxn]; int lowbit(int x) { return ...
- Python下载与安装配置
最近想学习Python那就开始吧 首先就是下载安装了 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:https://www.python.org/ 你 ...
- Spring——使用自定义标签
文章内容参考了<Spring源码深度解析>一书.自己照着书中内容做了一遍,不懂的地方以及采坑的地方会在文中记录. 推荐一篇post,关于Spring配置文件的命名空间: https://w ...
- 【liunx】Linux下的压缩和解压缩命令——jar
原文链接:http://blog.chinaunix.net/uid-692788-id-2681136.html JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包.当然也是 ...
- redux笔记1
1.安装redux 使用 npm install -save redux 安装redux,注意使用-save 表示安装到依赖中: 2. 创建store文件夹,下面创建 index.js 和 re ...
- 固态硬盘和机械硬盘双硬盘安装win10,提示无法找到系统
选择兼容模式,自己慢慢找,不同的主板所在的位置不同,大概是cms(兼容的意思)这个选项,选择enable就可以了
- Babelfish 基本试用
测试使用docker 部署 docker-compose文件 注意网络模型选择的host,同时配置了opentracing 服务 version: "3" services: b ...
- C# HttpClient请求Webapi帮助类
引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...
- CLR(Common Language Runtime) 公共语言运行库
.NET Core 使用 CoreCLR .NET Framework 使用CLR. 1. 将代码编译为IL (Intermediate Language) 2. CLR 把IL 编译为平台专用的本地 ...