Java基本数据类型的转换

/*
基本数据类型的转换
2015年3月31日08:21:20
*/


public class TestString
{
public static void main(String[] args)
{
//primitive->Wapper Class
int i = 10;
Integer i1 = i;   //利用Autoboxing和AutoUnboxing
System.out.println("i1 = " + i1);
Integer i2 = new Integer(i);//通过new WapperClass(primitive)创建 
System.out.println("i2 = " +i2);


//Wapper->primitive
int int1 = i1.intValue();
System.out.println("int1 = "+int1);//利用WapperInstance.xxxValue()方法
int int2 = i2;
System.out.println("int2 = " + int2);


//WapperClass->String
char c = 'a';
boolean b = true;
Character ch = c;
String str1 = String.valueOf(ch);//此处ch自动拆箱成primitive
System.out.println("str1 = " + str1);//利用static String valueOf(primitive)方法
String str2 = Boolean.toString(b);
System.out.println("str2 = " + str2);//利用WapperClass提供的toString()方法    public static String toString(WapperInstance)
String str3 = ch.toString();
//利用public String toString()方法
System.out.println("str3 = " + str3);
String str4 = "" + c;
System.out.println("str4 = " + str4);


//String->WapperClass
//Character ch2 = Character.valueOf(str1);//Error 无法将字符串转变成字符
Boolean b2 = Boolean.valueOf(b);
System.out.println("b2 = " + b2);


//String->primitive
boolean b3 = Boolean.parseBoolean(str2);//利用WapperClass提供的WapperClass.valueXxx()方法


//primitive->String
//通过String.valueOf(primitive)方法


//Character->String和WapperClass->一样
//char->String和Primitive->String一样
//String->char利用public char charAt()方法
//String->char[]利用public char[] toCharArray()方法
//char[]->String利用Arrays.toString()方法
}
}

你可能感兴趣的:(Java基本数据类型的转换)