基本类型转换成包装类/包装类转换成基本类型/包装类转换成String/String转换成基本类型?

public class TestWrapper{
  
	@Test
   public void test(){
	    int i=10;
	    System.out.println(i);//10
	    //基本的数据类型-------》对应的包装类的,调用的是包装类的构造器
	    Integer i1=new Integer(i);
	    System.out.println(i1);//10
	    i1=new Integer("123");
	    System.out.println(i1);//123
	    //对于Boolean 来讲,
	    boolean ii=true;
	    Boolean b1=new Boolean("truesgag");
	    System.out.println(b1);//false
	    
	    //包装类-----》基本数据类型:调用的是包装类的xxx的xxxValue()
	    
	    int i2=i1.intValue();
	    System.out.println(i2);//123
	    
	    boolean bb=b1.booleanValue();
	    System.out.println(bb);//false
	    
	    //jdk5.0,可以自动的转换
	    
	    int  i4=12;
	    Integer i3=i4;  //自动装箱
	    System.out.println(i3);//12
	    
	    int i5=i3;
	    System.out.println(i5);//12
	    
	    
   }
	  //基本数据类型,包装类----》String: 调用String类的重载的valueOf
	  @Test
	    public void test2(){
		  int i1=10;
		  Integer i2=i1;
		  String str=String.valueOf(i2);
		  
		  String str2=String.valueOf(true);
		  str=i2.toString();
		  System.out.println(str2);//true
		  
		  
		//String------->  基本数据类型,包装类,调用的方法parsexxx(String str)
          int i3=Integer.parseInt(str);
          System.out.println(i3);//10
          
          boolean b=Boolean.parseBoolean(str2);
          System.out.println(b);//true
          
          
	  }
	    
}

猜你喜欢

转载自blog.csdn.net/Java_stud/article/details/82320035