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); }