1 构造方法
image.pngpackage cn.itcast_Integer;
/*
* 构造方法
* public Integer(int value)
* public Integer(String s),字符串s必须是由数字组成的,否则编译报错
*/
public class TestInteger02 {
public static void main(String[] args) {
int i = 200;
Integer it = new Integer(i);
String s = "300";
Integer it2 = new Integer(s);
System.out.println("it = "+it);
System.out.println("it2 = "+it2);
;
}
}
运行结果
it = 200
it2 = 300
2 和int之间的转换(JDK1.5后实现自动拆装箱)
image.png/*
* 1.5后的新特性,自动装拆箱
* 自动装箱:int--->Integer
* 自动拆箱:Integer--->int
* 注意,在做自动拆装箱前需要判断Integer对象不是null,否则调用intValue方法
* 时会报错null;
*/
public class TestInteger4 {
public static void main(String[] args){
/*Integer i = 100;
i +=100;
System.out.println("i = "+i);*/
//编译的实际内容是
Integer i = Integer.valueOf(100);
if(i != null){
i = Integer.valueOf(i.intValue()+100);
}
System.out.println(new StringBuilder().append("i = ").append(i).toString());
}
}
运行结果:
i = 200
2.2 Integer直接赋值,范围[-128,127],直接从缓冲池中获取数据
package cn.itcast_Integer;
//import java.lang.Integer.IntegerCache;
/*
* 1.5后新特性,Integer直接赋值,范围[-128,127],直接从缓冲池中获取数据
* 查看Integer.valueOf(int i)的源码可解释
*/
public class TestInteger5 {
public static void main(String[] args) {
Integer i = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i == i2);//f
System.out.println(i.equals(i2));
System.out.println("------------------------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);//f
System.out.println(i3.equals(i4));
System.out.println("------------------------");
Integer i5 = 127;//Integer.valueOf(127)
Integer i6 = 127;
System.out.println(i5 == i6);//t
System.out.println(i5.equals(i6));
System.out.println("------------------------");
Integer i7 = 128;
Integer i8 = 128;
System.out.println(i7 == i8);//f
System.out.println(i7.equals(i8));
System.out.println("------------------------");
/*
* 源码内容:
* public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
从缓存池中取数据
return IntegerCache.cache[i + offset];
}
开辟新的地址空间
return new Integer(i);
}*/
}
}
3 实现String转换为int(字符串是由数字组成的)
image.png/*Integer的成员方法实现String转换为int:
*静态方法: public static int parseInt(String s)
*非静态方法:public int intValue()
*
* String -->int,public static int parseInt(String s)
* int -->String ,String.valueOf(int)(最常用)
*/
public class TestInteger3 {
public static void main(String[] args) {
int i = 100;
// int -->String
String s = "" + i;
//最常用
String s2 = String.valueOf(i);
//public String toString()
String s3 = new Integer(i).toString();
//public static String toString(int i)
String s4 = Integer.toString(i);
System.out.println(s+s2+s3);
System.out.println("-----------------");
//String -->int
int i2 = Integer.parseInt(s4);
System.out.println("i2 = "+i2);
int i3 = new Integer(s4).intValue();
}
}
4 进制字符串和Int之间的转换
public static String toString(int i, int radix)实现Int转换为各种进制的字符串,public static int parseInt(String s int radix)实现各种数字型的字符串转化为个中进制对应的Int
/*
* 1 输出100的2进制 8进制 16进制数
* 2 判断一个数是否是int范围内
* 10进制到其他进制,进制的范围是[2,36]
* public static String toString(int i, int radix):以racix的进制表示i的字符串
*
*/
public class TestInteger {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(100));//1100100
System.out.println(Integer.toOctalString(100));//144
System.out.println(Integer.toHexString(100));//64
System.out.println("MAX = "+Integer.MAX_VALUE);//MAX = 2147483647
System.out.println("Min = "+Integer.MIN_VALUE);//Min = -2147483648
System.out.println("------------------------");
System.out.println(Integer.toString(100, 10));//100
System.out.println(Integer.toString(100, 2));//1100100
System.out.println(Integer.toString(100, 8));//144
System.out.println(Integer.toString(100, 16));//64
System.out.println(Integer.toString(100, 32));//34
System.out.println("---------------------------");
System.out.println(Integer.parseInt("100", 2));//4
System.out.println(Integer.parseInt("100", 8));//64
System.out.println(Integer.parseInt("100", 10));//100
System.out.println(Integer.parseInt("100", 16));//256
System.out.println(Integer.parseInt("100", 36));//1296
}
}
网友评论