1.1 概述

Java提供了两个类型零碎,根本类型与援用类型,应用根本类型在于效率,然而很多状况,会创建对象应用,因为对象能够做更多的性能,如果想要咱们的根本类型像对象一样操作,就能够应用根本类型对应的包装类,如下:

根本类型对应的包装类(位于java.lang包中)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

1.2 Integer类

  • Integer类概述

    包装一个对象中的原始类型 int 的值

  • Integer类构造方法及静态方法
办法名阐明
public Integer(int value)依据 int 值创立 Integer 对象(过期)
public Integer(String s)依据 String 值创立 Integer 对象(过期)
public static Integer valueOf(int i)返回示意指定的 int 值的 Integer 实例
public static Integer valueOf(String s)返回保留指定String值的 Integer 对象
static string tobinarystring(int i)失去二进制
static string tooctalstring(int i)失去八进制
static string toHexstring(int i)失去十六进制
static int parseInt(string s)将字符串类型的整数转成int类型的整数
  • 示例代码
//public Integer(int value):依据 int 值创立 Integer 对象(过期)Integer i1 = new Integer(100);System.out.println(i1);//public Integer(String s):依据 String 值创立 Integer 对象(过期)Integer i2 = new Integer("100");//Integer i2 = new Integer("abc"); //NumberFormatExceptionSystem.out.println(i2);System.out.println("--------");//public static Integer valueOf(int i):返回示意指定的 int 值的 Integer 实例Integer i3 = Integer.valueOf(100);System.out.println(i3);//public static Integer valueOf(String s):返回保留指定String值的Integer对象 Integer i4 = Integer.valueOf("100");System.out.println(i4);
/*            public static string tobinarystring(int i) 失去二进制            public static string tooctalstring(int i) 失去八进制            public static string toHexstring(int i) 失去十六进制            public static int parseInt(string s) 将字符串类型的整数转成int类型的整数 *///1.把整数转成二进制,十六进制String str1 = Integer.toBinaryString(100);System.out.println(str1);//1100100//2.把整数转成八进制String str2 = Integer.toOctalString(100);System.out.println(str2);//144//3.把整数转成十六进制String str3 = Integer.toHexString(100);System.out.println(str3);//64//4.将字符串类型的整数转成int类型的整数//强类型语言:每种数据在java中都有各自的数据类型//在计算的时候,如果不是同一种数据类型,是无奈间接计算的。int i = Integer.parseInt("123");System.out.println(i);System.out.println(i + 1);//124//细节1://在类型转换的时候,括号中的参数只能是数字不能是其余,否则代码会报错//细节2://8种包装类当中,除了Character都有对应的parseXxx的办法,进行类型转换String str = "true";boolean b = Boolean.parseBoolean(str);System.out.println(b);

1.3 装箱与拆箱

根本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:

  • 装箱:从根本类型转换为对应的包装类对象。
  • 拆箱:从包装类对象转换为对应的根本类型。

用Integer与 int为例:(看懂代码即可)

根本数值---->包装对象

Integer i = new Integer(4);//应用构造函数函数Integer iii = Integer.valueOf(4);//应用包装类中的valueOf办法

包装对象---->根本数值

int num = i.intValue();

1.4 主动装箱与主动拆箱

因为咱们常常要做根本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,根本类型与包装类的装箱、拆箱动作能够主动实现。例如:

Integer i = 4;//主动装箱。相当于Integer i = Integer.valueOf(4);i = i + 5;//等号左边:将i对象转成根本数值(主动拆箱) i.intValue() + 5;//加法运算实现后,再次装箱,把根本数值转成对象。

1.5 根本类型与字符串之间的转换

根本类型转换为String

  • 转换形式
  • 形式一:间接在数字后加一个空字符串
  • 形式二:通过String类静态方法valueOf()
  • 示例代码
public class IntegerDemo {    public static void main(String[] args) {        //int --- String        int number = 100;        //形式1        String s1 = number + "";        System.out.println(s1);        //形式2        //public static String valueOf(int i)        String s2 = String.valueOf(number);        System.out.println(s2);        System.out.println("--------");    }}

String转换成根本类型

除了Character类之外,其余所有包装类都具备parseXxx静态方法能够将字符串参数转换为对应的根本类型:

  • public static byte parseByte(String s):将字符串参数转换为对应的byte根本类型。
  • public static short parseShort(String s):将字符串参数转换为对应的short根本类型。
  • public static int parseInt(String s):将字符串参数转换为对应的int根本类型。
  • public static long parseLong(String s):将字符串参数转换为对应的long根本类型。
  • public static float parseFloat(String s):将字符串参数转换为对应的float根本类型。
  • public static double parseDouble(String s):将字符串参数转换为对应的double根本类型。
  • public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean根本类型。

代码应用(仅以Integer类的静态方法parseXxx为例)如:

  • 转换形式

    • 形式一:先将字符串数字转成Integer,再调用valueOf()办法
    • 形式二:通过Integer静态方法parseInt()进行转换
  • 示例代码
public class IntegerDemo {    public static void main(String[] args) {        //String --- int        String s = "100";        //形式1:String --- Integer --- int        Integer i = Integer.valueOf(s);        //public int intValue()        int x = i.intValue();        System.out.println(x);        //形式2        //public static int parseInt(String s)        int y = Integer.parseInt(s);        System.out.println(y);    }}
留神:如果字符串参数的内容无奈正确转换为对应的根本类型,则会抛出java.lang.NumberFormatException异样。

5.6 底层原理

倡议:获取Integer对象的时候不要本人new,而是采取间接赋值或者静态方法valueOf的形式

因为在理论开发中,-128~127之间的数据,用的比拟多。如果每次应用都是new对象,那么太节约内存了。

所以,提前把这个范畴之内的每一个数据都创立好对象,如果要用到了不会创立新的,而是返回曾经创立好的对象。

//1.利用构造方法获取Integer的对象(JDK5以前的形式)/*Integer i1 = new Integer(1);        Integer i2 = new Integer("1");        System.out.println(i1);        System.out.println(i2);*///2.利用静态方法获取Integer的对象(JDK5以前的形式)Integer i3 = Integer.valueOf(123);Integer i4 = Integer.valueOf("123");Integer i5 = Integer.valueOf("123", 8);System.out.println(i3);System.out.println(i4);System.out.println(i5);//3.这两种形式获取对象的区别(把握)//底层原理://因为在理论开发中,-128~127之间的数据,用的比拟多。//如果每次应用都是new对象,那么太节约内存了//所以,提前把这个范畴之内的每一个数据都创立好对象//如果要用到了不会创立新的,而是返回曾经创立好的对象。Integer i6 = Integer.valueOf(127);Integer i7 = Integer.valueOf(127);System.out.println(i6 == i7);//trueInteger i8 = Integer.valueOf(128);Integer i9 = Integer.valueOf(128);System.out.println(i8 == i9);//false//因为看到了new关键字,在Java中,每一次new都是创立了一个新的对象//所以上面的两个对象都是new进去,地址值不一样。/*Integer i10 = new Integer(127);        Integer i11 = newsubtitle Integer(127);        System.out.println(i10 == i11);        Integer i12 = new Integer(128);        Integer i13 = new Integer(128);        System.out.println(i12 == i13);*/
本文参加了SegmentFault 思否面试闯关挑战赛,欢送正在浏览的你也退出。