各数据类型之间的相互转换

1.String类型转换为int类型

         String s="23";
         int a;
         a=Integer.parseInt(s);

2.int类型转换为String类型

         int age=23;
         String Age=Integer.toString(age);
         String AGE=String.valueOf(age);

3.byte类型转换为String类型

         byte[] bt="How are you".getBytes();
         String BT=new String(bt);

4.String类型转换为byte类型

         String s="How are you";
         byte []bt=s.getBytes();

5.byte类型转换为int类型

         int age=23;
         byte bt=(byte)age;

6.int类型转换为byte类型

         byte bt=23;
         int a=bt;

byte的取值范围为-128到127,因此转换的时候int类型的取值若超过了取值范围,对结果会产生影响。

7.String类型转换为Student类型(StringBuilder)

         for(Student s:array){
             StringBuilder sb=new StringBuilder();
             sb.append(s.getSize()).append(",").append(s.getName()).append(",")
             .append(s.getAge()).append(",").append(s.getAddress());
             
             br.write(sb.toString());
             br.newLine();
             br.flush();
         }

8.Student类型转换为String类型(split)

         ArrayList<Student> array=new ArrayList<Student>();
         String line;
         while((line=br.readLine())!=null){
             String[] StrArray = line.split(",");
             Student s=new Student();
             s.setSize(StrArray[0]);
             s.setName(StrArray[1]);
 //            s.setAge(array.indexOf(StrArray[2]));
             s.setAge(Integer.parseInt(StrArray[2]));
             s.setAddress(StrArray[3]);
             array.add(s);
         }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值