为了对基本数据类型进行更多的操作,Java就针对每一种基本数据类型提供了对应的类类型。
常用操作之一:基本数据类型与字符串之间的转换。
基本类型与其对应的包装类类型。
| 基本类型 | 对应的包装类 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。
此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
构造方法摘要
- Integer(int value) 构造一个新分配的 Integer 对象,它表示指定的 int 值。
- Integer(String s) 构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
public class Mytest3 {
public static void main(String[] args) {
int num = 100;
Integer integer = new Integer(num);
System.out.println(integer);
Integer integer1 = new Integer("123");
System.out.println(integer1+89);
}
}
public class Mytest1 {
public static void main(String[] args) {
int num=100;
//转换为二进制
String string = Integer.toBinaryString(num);
//转换为八进制
String string1 = Integer.toOctalString(num);
转换为十六进制
String string2 = Integer.toHexString(num);
//将返回的二进制字符串转换成其对应的数字
System.out.println(Integer.parseInt(string)+45);
System.out.println(string1);
System.out.println(string2);
}
}
String和int类型的相互转换
int – String
- 和""进行拼接
- public static String valueOf(int i)
- int – Integer – String
- public static String toString(int i)
String – int
- String – Integer – intValue();
- public static int parseInt(String s)
public class Mytest2 {
public static void main(String[] args) {
//int->String
int num=100;
String str=num+"";
String.valueOf(num);
String s = new Integer(num).toString();
//String->int
String str1 = "123";
Integer integer = new Integer(str1);
int i = integer.intValue();
int i1 = Integer.parseInt(str1);
}
}
JDK5的新特性,自动拆装箱。
- 自动装箱:把基本类型转换为包装类类型
- 自动拆箱:把包装类类型转换为基本类型
public class Mytest4 {
public static void main(String[] args) {
Integer num = 100;//自动装箱
int num1 = 200;
int i = num.intValue();//手动拆箱
System.out.println(num + num1);
}
}
public class Mytest5 {
public static void main(String[] args) {
Integer num = 10;//自动装箱
num += 100;//先自动拆箱,在自动装箱
Integer integer = new Integer(200);
int i = integer.intValue();//手动拆箱
Integer integer1 = Integer.valueOf(100);//手动装箱
Integer integer2 = Integer.valueOf("10000");//手动装箱
}
}
Java 的Integer、int与new Integer到底怎么回事?
- int和Integer在进行比较的时候,Integer会进行拆箱,转为int值与int值进行比较。
- Integer与Integer比较的时候,由于直接赋值的时候会进行自动的装箱,那么这里就需要注意两个问题,一个是-128<= x<=127的整数,将会直接缓存在IntegerCache中,那么当赋值在这个区间的时候,不会创建新的Integer对象,而是从缓存中获取已经创建好的Integer对象。二:当大于这个范围的时候,直接new Integer来创建Integer对象。
- new Integer(1) 和Integer a = 1不同,前者会创建对象,存储在堆中,而后者因为在-128到127的范围内,不会创建新的对象,而是从IntegerCache中获取的。那么Integer a = 128, 大于该范围的话才会直接通过new Integer(128)创建对象,进行装箱。
package org.westos.demo2;
public class Mytest6 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
System.out.println(i1.equals(i2));//true
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);//false
System.out.println(i3.equals(i4));//true
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false 因为 超过了一个字节的范围 会new 一个Integer对象
System.out.println(i5.equals(i6));//true
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true 没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128---127
System.out.println(i7.equals(i8));//true
}
}
运行结果为:
false
true
-----------
false
true
-----------
false
true
-----------
true
true
Java针对基本数据类型提供对应类类型,以进行更多操作。本文围绕Integer类展开,介绍其构造方法,阐述String和int类型相互转换的方式,还提及JDK5自动拆装箱特性。同时分析int和Integer、Integer与Integer比较时的情况,以及new Integer和直接赋值的区别。
以及其中Integer的特性&spm=1001.2101.3001.5002&articleId=89819892&d=1&t=3&u=4ec24e7943694f099977d09eb017132c)
323

被折叠的 条评论
为什么被折叠?



