一: 基本类型包装类:
为什么会有基本类型包装类?
为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.
常用操作: 常用的操作之一:用于基本数据类型与字符串之间的转换。
基本类型和包装类的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Integer类的概述和构造方法:
Integer类概述:
通过JDK提供的API,查看Integer类的说明Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个
方法能 在 int 类型和 String 类型之间互相转换, 还提供了处理 int 类型时非常有用的其他一些常量和方法
构造方法
public Integer(int value);
public Integer(String s);
举例:
package org.westos.Demo1;
public class MyTest {
public static void main(String[] args) {
// Java为了我们方便的去操作这些基本数据类型,给我们提供了与之对应的包装类型
// byte Byte
// short Short
// int Integer
// long Long
// float Float
// double Double
// char Character
// boolean Boolean
// Integer
// static int MAX_VALUE
// 值为 231-1 的常量,它表示 int 类型能够表示的最大值。
// static int MIN_VALUE
// 值为 -231 的常量,它表示 int 类型能够表示的最小值。
// Integer中的静态常量
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
System.out.println(minValue);
System.out.println(maxValue);
// Integer常用的构造方法
// B:构造方法
// public Integer(int value)
// public Integer(String s)
int num = 100;
Integer integer = new Integer(num);
String string = "50";
Integer integer2 = new Integer(string);
// A:int -- String
int a = 100;
// 方式1
String s1 = a + "";
// 方式2
String of = String.valueOf(a);//将数字转成字符串的数字
// String------int
String string2 = "100";
// 方式1
Integer integer3 = new Integer(string2);
int intValue = integer3.intValue();//将一个包装类型,转成他所对应的基本数据类型
System.out.println(intValue + 1);
//方式2 常用
int parseInt = Integer.parseInt("50");//将一个字符串的数字,转成一个数字
}
}package org.westos.Demo1;
public class MyTest {
public static void main(String[] args) {
// Java为了我们方便的去操作这些基本数据类型,给我们提供了与之对应的包装类型
// byte Byte
// short Short
// int Integer
// long Long
// float Float
// double Double
// char Character
// boolean Boolean
// Integer
// static int MAX_VALUE
// 值为 231-1 的常量,它表示 int 类型能够表示的最大值。
// static int MIN_VALUE
// 值为 -231 的常量,它表示 int 类型能够表示的最小值。
// Integer中的静态常量
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
System.out.println(minValue);
System.out.println(maxValue);
// Integer常用的构造方法
// B:构造方法
// public Integer(int value)
// public Integer(String s)
int num = 100;
Integer integer = new Integer(num);
String string = "50";
Integer integer2 = new Integer(string);
// A:int -- String
int a = 100;
// 方式1
String s1 = a + "";
// 方式2
String of = String.valueOf(a);//将数字转成字符串的数字
// String------int
String string2 = "100";
// 方式1
Integer integer3 = new Integer(string2);
int intValue = integer3.intValue();//将一个包装类型,转成他所对应的基本数据类型
System.out.println(intValue + 1);
//方式2 常用
int parseInt = Integer.parseInt("50");//将一个字符串的数字,转成一个数字
}
}
二: String和int类型的相互转换:
String -----int
1.String-----Integer----int
String s = “123456”;
Integer i = new Integer(s);
Int a = i.intValue(); //intValue() 以int类型返回该Integer的值
2.通过Integer中的parseInt()方法
String s = “123456”;
Int a = Integer.parseInt(s);
Int-----String
1 通过与空字符串拼接
Int i =123456;
String s= “”;
String s1 = i+s;
2 通过String类中的valueOf()方法
Int i =123456;
String s =new String() ;
String s1 = s.valueOf(i);
3 通过Integer中的toString(int i)方法
toString
(int i)
: 返回一个表示指定整数的 String
对象。
Int i = 123456;
String s = Integer.toString(i);
4 先将基本类型转换为包装类型在通过包装类型调用toString()方法
Int a = 123456;
Integer i = new Integer(a);
String s =i.toString();
三: JDK5的新特性自动装箱和拆箱:
JDK5的新特性:
自动装箱:把基本类型转换为包装类类型 Integer ii = 100;
自动拆箱:把包装类类型转换为基本类型 ii += 200;
注意事项:
在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。
自动装箱和拆箱举例:
package org.westos.demo;
public class MyTest2 {
public static void main(String[] args) {
// 在JDK1.5 有一个新特性叫自动拆装箱
// 自动装箱:把基本数据类型自动转成对应的包装类型
// 自动拆箱:把包装类型自动转成他对应的基本数据类型
Integer ii = 100;// 自动装箱
ii += 200;// 自动拆箱,自动装箱
System.out.println(ii);
// 手动拆装箱
int a = 10;
Integer valueOf = Integer.valueOf(a);// 手动装箱
int intValue = valueOf.intValue();// 手动拆箱
int b = 30 + intValue;
Integer num = 500;
Integer[] arr = { 2, 6, 8 };
int sum = arr[0] + 10;
Integer[] arr2 = { Integer.valueOf(2), Integer.valueOf(20) };
}
}