当前位置:网站首页>Use of packaging

Use of packaging

2022-05-14 14:08:01Ignorance is a state

public class WrapperTest {
	
	//String type  ---> Basic data type 、 Packaging : Call the parseXxx(String s)
	@Test
	public void test5(){
		String str1 = "123";
		// Wrong situation :
//		int num1 = (int)str1;
//		Integer in1 = (Integer)str1;
		// May report NumberFormatException
		int num2 = Integer.parseInt(str1);
		System.out.println(num2 + 1);
		
		String str2 = "true1";
		boolean b1 = Boolean.parseBoolean(str2);
		System.out.println(b1);
	}
	
	// Basic data type 、 Packaging --->String type : call String Heavy duty valueOf(Xxx xxx)
	@Test
	public void test4(){
		
		int num1 = 10;
		// The way 1: Join operation 
		String str1 = num1 + "";
		// The way 2: call String Of valueOf(Xxx xxx)
		float f1 = 12.3f;
		String str2 = String.valueOf(f1);//"12.3"
		
		Double d1 = new Double(12.4);
		String str3 = String.valueOf(d1);
		System.out.println(str2);
		System.out.println(str3);//"12.4"
		
	}
	
	/*
	 * JDK 5.0  New characteristics : Automatic boxing   And automatic unpacking 
	 */
	@Test
	public void test3(){
//		int num1 = 10;
//		// Basic data type --> The object of the wrapper class 
//		method(num1);
		
		// Automatic boxing : Basic data type  ---> Packaging 
		int num2 = 10;
		Integer in1 = num2;// Automatic boxing 
		
		boolean b1 = true;
		Boolean b2 = b1;// Automatic boxing 
		
		// Automatic dismantling : Packaging ---> Basic data type 
		System.out.println(in1.toString());
		
		int num3 = in1;// Automatic dismantling 
		
	}
	
	public void method(Object obj){
		System.out.println(obj);
	}
	
	// Packaging ---> Basic data type : Call wrapper class Xxx Of xxxValue()
	@Test
	public void test2(){
		Integer in1 = new Integer(12);
		
		int i1 = in1.intValue();
		System.out.println(i1 + 1);
		
		
		Float f1 = new Float(12.3);
		float f2 = f1.floatValue();
		System.out.println(f2 + 1);
	}
	
	// Basic data type  ---> Packaging : Call the constructor of the wrapper class 
	@Test
	public void test1(){
		
		int num1 = 10;
//		System.out.println(num1.toString());
		Integer in1 = new Integer(num1);
		System.out.println(in1.toString());
		
		Integer in2 = new Integer("123");
		System.out.println(in2.toString());
		
		// The abnormal 
//		Integer in3 = new Integer("123abc");
//		System.out.println(in3.toString());
		
		Float f1 = new Float(12.3f);
		Float f2 = new Float("12.3");
		System.out.println(f1);
		System.out.println(f2);
		
		Boolean b1 = new Boolean(true);
		Boolean b2 = new Boolean("TrUe");
		System.out.println(b2);
		Boolean b3 = new Boolean("true123");
		System.out.println(b3);//false
		
		
		Order order = new Order();
		System.out.println(order.isMale);//false
		System.out.println(order.isFemale);//null
	}
	
}

summary :

Only String Class has a function to change the basic data type to String Type of valueof() Method , Other type transformations are carried out through wrapper class methods or wrapper class constructors .

原网站

版权声明
本文为[Ignorance is a state]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/134/202205141400438740.html

随机推荐