java基础数据类型的包装类

java基础数据类型的包装类

基本数据类型 ------------->  对应的包装类

byte                                     Byte

short                                   Short

int                                       Integer

long                                    Long 

float                                    Float

double                                Double

char                                    Character

boolean                              Boolean

TestWrapperClass.java

//基本数据类型的包装类测试
/*
	byte 		Byte
	short 		Short
	int 		Integer
	long		Long 
	float 		Float
	double 		Double
	char 		Character
	boolean		Boolean
*/
public class TestWrapperClass {
	public static void main(String[] args) {
		/*
		Double(double value)
			Constructs a newly allocated Double object that represents the primitive double argument.
		Double(String s)
			Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
		*/
		Integer i = new Integer(100);
		Double d = new Double("123.456");		
		int j = i.intValue() + d.intValue();
		float f = i.floatValue() + d.floatValue();		
		System.out.println(j);//223
		System.out.println(f);//223.456
		
		double pi = Double.parseDouble("3.1415926");
		double r = Double.valueOf("2.0").doubleValue();		
		double s = pi * r * r;
		System.out.println(s);//12.5663704
		
		int k;
		try{
			k = Integer.parseInt("1.25");//只能是整数字符串
		}catch(NumberFormatException e){
			k = -1;
			System.out.println("数据格式不正确: " + k);//数据格式不正确: -1
		}
				
		/*
		static String	toBinaryString(int i) 
			Returns a string representation of the integer argument as an unsigned integer in base 2.
		static String	toHexString(int i)
			Returns a string representation of the integer argument as an unsigned integer in base 16.
		static String	toOctalString(int i)
			Returns a string representation of the integer argument as an unsigned integer in base 8.
		*/
		System.out.println(Integer.toBinaryString(123) + "B");//1111011B 二进制
		System.out.println(Integer.toHexString(123) + "H");//7bH 十六进制
		System.out.println(Integer.toOctalString(123) + "O");//173O 八进制
		
	}
}
F:\java>javac TestWrapperClass.java

F:\java>java TestWrapperClass
223
223.456
12.5663704
数据格式不正确: -1
1111011B
7bH
173O

F:\java>

猜你喜欢

转载自mfcfine.iteye.com/blog/2384543