JAVA中有8个基本数据类型,每个类型都有各自的特点;特别是在JAVA.IO文件操作中需要用到字节流和字符流,,字符之间的转换问题,在java的API文档中为我们提供的相关的方法;
一,重点讲:
1,//字符床转成字符char
使用String的CharAt()来转成字符
2,//将字符转成字节byte
使用getBytes转换
3,//将字符转换成int
//将字符串转换成字符数组
根据字符串创建字符数组ToCharArray(),再遍历字符串进行强制转换出所有的
4,将int转换成字符串
使用Integer.toString();转换
5,在打印数组的时候千万不能打印对象,必须要打印数组中的数,否则打印出来的就是对象的地址
在将字符转换成字节时,我最开始是这样做的:代码如下
//将字符转成字节byte
for(int n = 0;n<name.length();n++){
byte[] b=name.getBytes();
System.out.println("byte = " + b);
}
分析:上述的代码最学者很难发现,此时打印的是对象的地址,而不是我们需要的byte,
特别注意,在打印数组的时候一定要打印数组中的数b[n];而不是打印b对象
现将个人理解的代码贴出以供参考 共同学习
package com.iteye.com;
/**
* 字符串--->字符-->字节--->整型
* @author Administrator
*
*/
public class StringAndChar {
public static void main(String[] args) {
//创建转换
//字符串--->字符-->字节--->整型
String name = "publicstaticvoidmain(String[]args)";
//字符床转成字符char
for(int i = 0;i<name.length();i++){
char names = name.charAt(i);
System.out.println("char = " + names);
}
//将字符转成字节byte
for(int n = 0;n<name.length();n++){
byte[] b=name.getBytes();
System.out.println("byte = " + b[n]);
}
//将字符转换成int
//将字符串转换成字符数组
char[] chars = name.toCharArray();
for(int j = 0;j<chars.length;j++){
System.out.println("字符转换成int ="+(int)chars[j]);
}
/*
*将int转换成字符串
int a1=123;
String aa=Integer.toString(a1);
System.out.println(aa);
//运行结果:123 此时的123是String类型
*/
}
}
运行结果: 数字都是打印的ASCII 编码
char = p
char = u
char = b
char = l
char = i
char = c
char = s
char = t
char = a
char = t
char = i
char = c
char = v
char = o
char = i
char = d
char = m
char = a
char = i
char = n
char = (
char = S
char = t
char = r
char = i
char = n
char = g
char = [
char = ]
char = a
char = r
char = g
char = s
char = )
byte = 112
byte = 117
byte = 98
byte = 108
byte = 105
byte = 99
byte = 115
byte = 116
byte = 97
byte = 116
byte = 105
byte = 99
byte = 118
byte = 111
byte = 105
byte = 100
byte = 109
byte = 97
byte = 105
byte = 110
byte = 40
byte = 83
byte = 116
byte = 114
byte = 105
byte = 110
byte = 103
byte = 91
byte = 93
byte = 97
byte = 114
byte = 103
byte = 115
byte = 41
字符转换成int =112
字符转换成int =117
字符转换成int =98
字符转换成int =108
字符转换成int =105
字符转换成int =99
字符转换成int =115
字符转换成int =116
字符转换成int =97
字符转换成int =116
字符转换成int =105
字符转换成int =99
字符转换成int =118
字符转换成int =111
字符转换成int =105
字符转换成int =100
字符转换成int =109
字符转换成int =97
字符转换成int =105
字符转换成int =110
字符转换成int =40
字符转换成int =83
字符转换成int =116
字符转换成int =114
字符转换成int =105
字符转换成int =110
字符转换成int =103
字符转换成int =91
字符转换成int =93
字符转换成int =97
字符转换成int =114
字符转换成int =103
字符转换成int =115
字符转换成int =41
二 , 字符串转成字节,字节转成字符串
分析思路:
字节转换成字符串
String name = "publicstaticvoidmain(String[]args)";
//转换成字节
byte[] b= name.getBytes();
//创建String对象将字节转换成字符串
String names = new String (b);
//打印出字符
System.out.println(names);
代码如下:
package com.iteye.com;
/**
* 字符串转成字节,字节转成字符串
*
* @author Administrator
*
*/
public class Stringtobyte {
public static void main(String[] args) {
// 字节转换成字符串
String name = "publicstaticvoidmain(String[]args)";
for (int i = 0; i < name.length(); i++) {
// 转换成字节
byte[] b = name.getBytes();
System.out.println("字节 " + b[i]);
// 创建String对象将字节转换成字符串
String names = new String(b);
// 打印出字符
System.out.println("字符串 =" + names);
}
}
}
运行结果:
字节 112
字符串 =publicstaticvoidmain(String[]args)
字节 117
字符串 =publicstaticvoidmain(String[]args)
字节 98
字符串 =publicstaticvoidmain(String[]args)
字节 108
字符串 =publicstaticvoidmain(String[]args)
字节 105
字符串 =publicstaticvoidmain(String[]args)
字节 99
字符串 =publicstaticvoidmain(String[]args)
字节 115
字符串 =publicstaticvoidmain(String[]args)
字节 116
字符串 =publicstaticvoidmain(String[]args)
字节 97
字符串 =publicstaticvoidmain(String[]args)
字节 116
字符串 =publicstaticvoidmain(String[]args)
字节 105
字符串 =publicstaticvoidmain(String[]args)
字节 99
字符串 =publicstaticvoidmain(String[]args)
字节 118
字符串 =publicstaticvoidmain(String[]args)
字节 111
字符串 =publicstaticvoidmain(String[]args)
字节 105
字符串 =publicstaticvoidmain(String[]args)
字节 100
字符串 =publicstaticvoidmain(String[]args)
字节 109
字符串 =publicstaticvoidmain(String[]args)
字节 97
字符串 =publicstaticvoidmain(String[]args)
字节 105
字符串 =publicstaticvoidmain(String[]args)
字节 110
字符串 =publicstaticvoidmain(String[]args)
字节 40
字符串 =publicstaticvoidmain(String[]args)
字节 83
字符串 =publicstaticvoidmain(String[]args)
字节 116
字符串 =publicstaticvoidmain(String[]args)
字节 114
字符串 =publicstaticvoidmain(String[]args)
字节 105
字符串 =publicstaticvoidmain(String[]args)
字节 110
字符串 =publicstaticvoidmain(String[]args)
字节 103
字符串 =publicstaticvoidmain(String[]args)
字节 91
字符串 =publicstaticvoidmain(String[]args)
字节 93
字符串 =publicstaticvoidmain(String[]args)
字节 97
字符串 =publicstaticvoidmain(String[]args)
字节 114
字符串 =publicstaticvoidmain(String[]args)
字节 103
字符串 =publicstaticvoidmain(String[]args)
字节 115
字符串 =publicstaticvoidmain(String[]args)
字节 41
字符串 =publicstaticvoidmain(String[]args)