本帖最后由 玥夜 于 2014-8-1 23:35 编辑
Java所有的对象都可以使用toString()方法,因为它是定义在Object类中的方法,
输出语句System.out.println()中所有得输出对象都是调用此方法,
但是有的类中把Object的toString()方法给覆盖了 ,所以看到的输出结果会不一样
我这有个代码希望对你有帮助
- import java.util.Arrays;
- public class 字符转字符串 {
- public static void main(String[] args)
- {
- byte[] b=new byte[]{'b','y','t','e'};
- char[] ch=new char[]{'c','h','a','r'};
- int[] i= new int[]{'i','n','t'};
- String str1= new String("String");
- String[] strr= new String[]{"s","t","r","i","n","g"};
-
-
- System.out.println("直接输出");
- System.out.println(b);
- System.out.println(ch);
- System.out.println(i);
- System.out.println(str1);
- System.out.println(strr);
- System.out.println();
-
- System.out.println("通过+"+"\"\"变成字符串输出");
- System.out.println("byte[]===="+b);
- System.out.println("char[]===="+ch);
- System.out.println("int[]====="+i);
- System.out.println("String[]====="+strr);
- System.out.println("str1===="+str1);
-
- System.out.println();
- System.out.println();
-
- System.out.println("Object中toString()方法变成字符串输出");
- System.out.println("byte[].toString()===="+b.toString());
- System.out.println("char[].toString()===="+ch.toString());
- System.out.println("int[].toString()====="+i.toString());
- System.out.println("String[].toString()====="+strr.toString());
- System.out.println("String.toString()====="+str1.toString());
-
- System.out.println();
- System.out.println();
-
- System.out.println("Array.toString(arr)方法变成字符串输出");
- System.out.println("Arrays.toString(byte[])===="+Arrays.toString(b));
- System.out.println("Arrays.toString(char[])===="+Arrays.toString(ch));
- System.out.println("Arrays.toString(int[])===="+Arrays.toString(i));
- System.out.println("Arrays.toString(String[])===="+Arrays.toString(strr));
-
- System.out.println();
- System.out.println();
-
- System.out.println("String.valueOf(arr)方法变成字符串输出");
- System.out.println("String.valueOf(byte[])===="+String.valueOf(b));
- System.out.println("String.valueOf(char[])===="+String.valueOf(ch));
- System.out.println("String.valueOf(int[])===="+String.valueOf(i));
- System.out.println("String.valueOf(int)====="+String.valueOf(6));
- //valueOf(Object obj) 返回 Object 参数的字符串表示形式。除了char类型和基本类型
-
- System.out.println();
- System.out.println(new String(b));
-
-
- }
- }
复制代码
|