java中string与其他类型数据之类的转换

//byte[]转String
byte[] myBytes = {69,70,71};
String myStr = new String(myBytes);

//String转byte[]
String myStr = "hello world";
byte[] myBytes = myStr.getBytes();

//char[]转String
//第一种方法
char[] array = new char[]{'a','b','c'}
String str1 = new String(array);
//第二种方法
String str2 = String.valueOf(array);

//String转char[]
String str3 = "ABC";
char[] cr = str3.toCharArray();

//int转String
int n = 123456;
String r = String.valueOf(h);

//String转int
int i =Integer.parseInt(r);

//double转String
double a = 1.1;
String A = String.valueOf(a);

//String转double
double m = Double.parseDouble(A);

//boolean转String
boolean b = false;
String B = String.valueOf(b);

//String转boolean
boolean n = Boolean.parseBoolean(B);

//float转String
float g = 12.3;
String G = String.valueOf(g);

//String转float
float m = Float.parseFloat(G);

//long转String
long j = 123342;
String J = String.valueOf(j);

// String转long
long n = Long.parseLong(J);

//对象类型转String
Object obj = new Integer(100);
//obj可以为null
String p1 = String.valueOf(obj);
//obj不可以为null
String p2 = obj.toString();

你可能感兴趣的:(Java)