java数据类型转化大全实例代码

(更多精彩内容,请访问我的网站:http://www.xuediit.cn,谢谢支持)

 


public class IntZhuangHua{
public static void main(String [] args){

   // 1、整型数、字符串类型数、字符串对象转化为整型对象
   int a = 512;
   String str = new String("512");
      Integer t11 = new Integer(a);
   Integer t22 = new Integer("512");  
   Integer t33 = new Integer(str);
   System.out.println("1------" + t11.hashCode());
   System.out.println("2------" + t22.hashCode());
   System.out.println("3------" + t33.hashCode());
   Integer k = Integer.valueOf(a); //另外一种写法
   System.out.println("-----------------------------------------------------------");
  
     
   //2、 字符串类型数、字符串对象转化为整型数
   String str2 = "555";
   String str3 = new String("666");
   Integer aa = new Integer(777);
   if(Integer.parseInt(str2) == 555 && Integer.parseInt(str3) == 666){
      System.out.println("true");
   }else{
      System.out.println("false");
   }
   System.out.println("-----------------------------------------------------------");
  
  
   // 3、基础数据类型转化为包装类对象
    int e = 22;
    Integer ee = new Integer(e);
    long lo = 33;
    Long lloo = new Long(lo);
    char cr = 'h';
    Character ccrr = new Character(cr);
  
  
   // 4、包装类对象转化为基础数据类型
   int d = 99;
   Integer aw = new Integer(d);
   if(99 == Integer.valueOf(aa).intValue()){
    System.out.println("13------"+"true");
   }else{
    System.out.println("13------"+"false");
   }
  
   
   // 5、其他数据类型数(对象)转化为字符串类型数
   int c = 888;
   Integer cc = new Integer(c);
   String str4 = String.valueOf(c);
   String str5 = String.valueOf(cc);
      String str6 = Integer.toString(c); //另外一种写法
   System.out.println("11------"+str4.hashCode());
   System.out.println("12------"+str6.hashCode());
   System.out.println("-----------------------------------------------------------");
  
  
      // 6、十进制转化为二进制、八进制、十六进制
   int z = 222;
   System.out.println("5------" + Integer.toBinaryString(z));   //转化为二进制
   System.out.println("6------" + Integer.toOctalString(z));    //转化为八进制
   System.out.println("7------" + Integer.toHexString(z));      //转化为十六进制
   System.out.println(3);
   System.out.println("-----------------------------------------------------------");
  
  
   //超级悖论
   String ssss = "888";
   String sss = new String("888");
   System.out.println(ssss.hashCode());
   System.out.println(sss.hashCode());
}
}

 

 

 

最后四行我没有理解,哪位高人有懂怎么运行原理的话请赐教!QQ:632801102

(更多精彩内容,请访问我的网站:http://www.xuediit.cn,谢谢支持)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值