1整型数据——》字符型
1)如果是按ASCII码形式进行转换的,且转化结果只是单个字符,
int d = 97;
char e = (char)d;
2)如果是按ASCII码形式进行转换的,且转化结果是多个字符字符串,前提应将整型数据保存在byte型的数组中,即
byte[] b ={97, 98, 99};
System.out.println(new String(b));
3)如果是按整型数据本身进行转化,即123——》"123",
String num = Integer.toString(int n);
2浮点型——>字符串
String num = Float.toString(Float n);
3数组——》字符串
1)整型字符数组转换为字符串,
//int型数组转字符串 使用StringBuffer
int ints = {1,2,3,4,5,6};
StringBuffer str = new StringBuffer();
for (int i = 0; i <ints.length ; i++) {
str = str.append(ints[i]);
}
String str1 = str.toString();
作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作
自己想了一下也可以只使用String实现转化,
int[] arr = {1, 2, 3, 4, 5};
String str = new String();
for(int i = 0; i < arr.length; i++){
str = str + arr[i];
}
System.out.println(str);
2)字符串数组转换为字符串,
//字符数组转字符串
//使用String.copyValueOf()方法
char[] ch = new char[10];
String string =String.copyValueOf(ch);
作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作
4字符串——》数组
String str= "abc";
//转换为char[] 注意取char[]中元素应该用 单引号 '' 表示char
char[] ch = str.toCharArray();
作者:Bye白夜
链接:https://www.imooc.com/article/43299
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作
也可以使用逐个替换的方法
char[] cha= new char[str.length()];
for(int i =0;i<str.length();i++) {
cha[i] = str.charAt(i);
}
5字符型——》整型
单个字符转换为整型
char c = 'a';
int a = (int)c;
多个的字符串转化为一个整型数字
int/Integer num = Integer.parseInt(String str);
多个字符串转化为多个整型数字(整型数组),可以使用字符串str——》字符数组stra(参照4),字符数组stra——》整型数组(参考本节上述方法)。
6包装类对象——》基本数字类型
Integer a = new Integer(3);
//int b = a;
//int b = a.intValue();
7基本数据类型——》包装类对象
Integer a = new Integer(3);
Integer a = Integer.valueOf(30);
8字符串——》包装类对象
Integer a = new Integer("124");
Integer a = Integer.parseInt("123");
9包装类对象——》字符串
Integer f = new Integer(123);
String str = f.toString();
//String str = f+"";
10StringBuffer——》String
String str= sb.toString();
11String——》StringBuffer
StringBuffer sb = new StringBuffer(str);