一滴水里观沧海,一粒沙中看世界

              ——一带一路欢迎宴致辞

上代码:

 package cn.stringtoobj;

 public class TypeConversion {

     public static void main(String[] args) {
//将String转int
String str = "";
int[] ints = new int[];
ints[] = Integer.parseInt(str);
ints[] = Integer.valueOf(str);
ints[] = new Integer(str);
print(ints);
//String转byte
byte[] bytes = new byte[];
bytes[] = Byte.parseByte(str);
bytes[] = Byte.valueOf(str);
bytes[] = new Byte(str);
print(bytes);
//String转short
short[] shorts = new short[];
shorts[] = Short.parseShort(str);
shorts[] = Short.valueOf(str);
shorts[] = new Short(str);
print(shorts);
//String转long
long[] longs = new long[];
longs[] = Long.parseLong(str);
longs[] = Long.valueOf(str);
longs[] = new Long(str);
print(longs);
//String转double
double[] doubles = new double[];
doubles[] = Double.parseDouble(str);
doubles[] = Double.valueOf(str);
doubles[] = new Double(str);
print(doubles);
//String转float
float[] floats = new float[];
floats[] = Float.parseFloat(str);
floats[] = Float.valueOf(str);
floats[] = new Float(str);
print(floats);
//String转boolean
str = "true";
boolean[] booleans = new boolean[];
booleans[] = Boolean.parseBoolean(str);
booleans[] = Boolean.valueOf(str);
booleans[] = new Boolean(str);
print(booleans);
//String转byte[]
byte[] bytes2 = str.getBytes();
print(bytes2);
//String转char[]
char[] dstchars =new char[str.length()];
str.getChars(, str.length(), dstchars, );
print(dstchars);
//Object转String
Object[] obj = new Object[];
str = String.valueOf(obj);
System.out.println(str);
str = String.valueOf(dstchars);
System.out.println(str);
}
private static void print(char[] dstchars) {
for (char i : dstchars) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(boolean[] booleans) {
for (boolean i : booleans) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(float[] floats) {
for (float i : floats) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(double[] doubles) {
for (double i : doubles) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(long[] longs) {
for (long i : longs) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(short[] shorts) {
for (short i : shorts) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(int[]ints){
for (int i : ints) {
System.out.print(i +" ");
}
System.out.println();
}
private static void print(byte[]bytes){
for (byte i : bytes) {
System.out.print(i +" ");
}
System.out.println();
}
}
05-28 22:26