包装类
一、在Java中,8中基本数据类型,并不符合面向对象的编程机制。他们不属于Object的类层次结构(8中基本数据类型没有父类)。也就是说8种基本数据类型,没有属性和方法。为了让基本数据类型也能像其他引用类型一样,具备面向对象的特性,所以,为8种基本数据类型定义了包装类。
– 基本数据类型与对应的包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
除了int变为Integer,char变为Character外,其他的基本类型的包装类都是将首字母大写,所以很好记。
二、关于八种包装类特点
• 1、每个包装类都可以与对应的基本数据类型互相转换;且包装类提供了操作该基本类型的属性和方法。
• 2、8个包装类都是final修饰的类。它和String一样,创建后,值(栈中地址)不可改变。
三、基本数据类型与包装类之间互相转换
1、基本类型- >包装类 如int
int i = 10;
Integer ii1 = new Integer(i);//1.通过包装类的构造方法
Integer ii2 = Integer.valueOf(i);//2.valueOf方法
2、 包装类 - > 基本数据类型
Integer ii1 = new Integer(12);
int x = ii1.intValue();
四、自动装箱和拆箱
1、装箱:基本数据类型- >包装类
• 底层就是使用Integer.valueOf将基本数据类型转换成包装类。
Integer ii = 10;
2、 拆箱:包装类- >基本数据类型
• 底层就是使用intValue方法,将Integer -> int
int i = ii;
Object obj = 1;//这样写正确么?
//正确,转化过程为 int -> Integer -> Object
五、String与基本数据类型或包装类之间互相转换
1、基本数据类型可以转换成String类型,以int为例
• String s1 = 10 + "";//1.基本数据类型->String
• String s2 = String.valueOf(10);//2.String.valueOf(基本数据类型对象);
• String s3 = Integer.toString(10);//3.8种包装类.toString(对应的基本数据类型)
package com.jcxy.demo08;
public class Test
{
public static void main(String[] args)
{
int i = 10;
String str1 = i + "";//基本数据类型->String
String str2 = String.valueOf(i);//String.valueOf(基本数据类型对象)
String str3 = Integer.toString(i);//8种包装类.toString(对应的基本数据类型)
System.out.println(str1);//10
System.out.println(str2);//10
System.out.println(str3);//10
}
}
2、对应的基本数据类型的字符串格式转换成基本数据类型。以int为例
• String str = "10";
Integer xx = Integer.valueOf(ss1);//将数值型的字符串->int的包装类对象。
• int x = Integer.parseInt(str);//包装类.parseInt()
• 此时如果str对象不是数值型的字符串会报NumberFormatException。
package com.jcxy.demo08;
public class Test1
{
public static void main(String[] args)
{
String str1 = "中国";
String str2 = "12";
Integer i1 = Integer.valueOf(str2);//包装类.valueOf(),返回值是Integer
int i2 = Integer.parseInt(str2);//包装类.parseInt(),注意返回值是int
// Integer i3 = Integer.valueOf(str1);//java.lang.NumberFormatException
System.out.println(i1);
System.out.println(i2);
}
}
可以通过下面图片形象的理解记忆
附:
• 字符串和byte[]数组转换,char数组的转换
package com.jcxy.demo08;
import java.io.UnsupportedEncodingException;
/**
*
* 我的eclipse默认编码是GBK
*
* @author Mr Wang
* @version [V1.00, 2015-12-8]
* @see [相关类/方法]
* @since V1.00
*/
public class Test3
{
public static void main(String[] args) throws UnsupportedEncodingException
{
//1-按照默认的码表对字符进行编码或解码
String str1 = "中国人";
byte[] bs1 = str1.getBytes();//String -> byte[] 编码
String str2 = new String(bs1);//byte[] -> String 解码
System.out.println(str2);
String ss1 = new String(bs1, 2, 2);//部分解码
System.out.println("============" + ss1);
//3.2-按照指定的码表对字符进行编码或解码
byte[] bs2 = str1.getBytes("utf8");//字符按照GBK码表进行编码
String str3 = new String(bs2, "utf8");//字节按照GBK码表进行解码
System.out.println(str3);
String ss2 = new String(bs2, 3, 3, "utf8");
System.out.println("************" + ss2);
//4- String与字符数组之间互相转换
String str = "abcdefg";
char[] cs1 = str.toCharArray();//String -> char[]
String str4 = new String(cs1);//char[] -> String
String ss3 = new String(cs1, 2, 4);
System.out.println(ss3);
}
}