基本类型包装类概述
基本类型包装类应用场景
通过文本框获取用户输入的数字数据,得到的都是字符串。 如果想要对字符串中的数字进行运算,必须要将字符串转成数字。
解决方法
Java中提供了相应的解决的对象 :基本数据类型对象包装类
java将基本数据类型值封装成了对象,封装成对象的好处就在于可以提供更多的操作基本数值的功能。
以下为8大基本数据类型所对应的对象:
byte —— Byte
short —— Short
int —— Integer
long —— Long
float —— Float
double —— Double
boolean —— Boolean
char —— Character
其中需要注意int对应的是Integer,char对应的Character,其他6个都是基本类型首字母大写即可。
基本数据类型对象包装类特点
用于在基本数据和字符串之间进行转换。
代码示例
public static void main(String[] args) {
//将该数字默认以十进制转为字符串形式
System.out.println(Integer.toString(100));
//将该数字以三进制转为字符串形式
System.out.println(Integer.toString(100,3));
System.out.println(Integer.toBinaryString(100));
System.out.println(Integer.toOctalString(100));
System.out.println(Integer.toHexString(100));
//将数字拼接一个空串转为字符串形式
System.out.println(10010 + "");
System.out.println(String.valueOf(100));
}
将字符串转成基本类型
基本定义
parseXXX(String s);
其中XXX表示基本类型,参数为可以转成基本类型的字符串,如果字符串无法转成基本类型,将会发生 NumberFormatException
System.out.println(Integer.parseInt("123") + 2);
System.out.println(Integer.parseInt("a1", 16));// 可以将其他进制转成十进制。
将基本数值转成字符串有3种方式
- 基本类型直接与"“相连接即可;34+”"
- 调用String的valueOf方法:String.valueOf(34);
- 调用包装类中的toString方法:Integer.toString(34);
代码示例
public static void main(String[] args){
//将字符串s默认以十进制转出来十进制整数
System.out.println(Integer.parseInt("1123"));
//将字符串s以radix进制转出来十进制整数
System.out.println(Integer.parseInt("1001",2));
}
基本类型和对象转换
//基本数值---->包装对象
Integer i = **new** Integer(4);//使用构造函数函数
Integer ii = **new** Integer("4");//构造函数中可以传递一个数字字符串
Integer iii = Integer.*valueOf*(4);//使用包装类中的valueOf方法
//包装对象---->基本数值
int num = i.intValue();
自动装箱拆箱
基本定义
Integer i = 4; //自动装箱 —— Integer i = Integer.valueOf(4);
i = i + 5; //自动拆箱 —— 将i对象转成基本数值 i.intValue() + 5;
//加法运算后,再次装箱
自动装箱细节
Integer a = new Integer(3);
Integer b = new Integer(3);
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("---------------------");
Integer x = 128;
Integer y = 128; //在jdk1.5自动装箱时,如果数值在byte范围之内,不会新创建对象空间而是使用原来已有的空间。
System.out.println(x==y);
System.out.println(x.equals(y))
代码示例
import java.math.BigInteger;
public class IntegerDemo {
public static void main(String[] args) {
int i1 = 3;
int i2 = 4;
Integer i3 = Integer.valueOf(5); //new Integer()
Integer i4 = new Integer(6);
Integer i5 = new Integer("7");
//自动装箱 自动将8封装为一个Integer对象
Integer i6 = 8; //Integer.valueOf(8) -> new Integer(8)
//自动拆箱 基本数据类型的包装类对象 一旦和基本数据类型或基包进行基本四则运算
//自动会使用 intValue();
System.out.println(i1 + i3.intValue());
System.out.println(i2 + i4.intValue());
System.out.println(i3.intValue() + i4.intValue());
System.out.println(i4.intValue() + i5.intValue());
System.out.println("+++++++++++++++++++++++++");
Integer a = 1; //new Integer(1)
Integer b = 1; //new Integer(1)
System.out.println(a == b);
//不是你理解a.intValue() == b.intValue()
//a == b 确实比较的是两个对象的地址
System.out.println(a.equals(b));
Integer x = 200; //new Integer(200)
Integer y = 200; //new Integer(200)
System.out.println(x == y);
//x == y 确实比较的是两个对象的地址
System.out.println(x.equals(y));
/*
* if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
*
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
*
*
* */
}
}