【转载】复制网友关于tostring的看法

来源:互联网 发布:网络美女排行榜2016 编辑:程序博客网 时间:2024/06/04 23:24

Description

The method is used to get a String object representing the value of the Number Object.

If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.

If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned.

Syntax

Following are all the variants of this method −

String toString()

static String toString(int i)

Parameters

Here is the detail of parameters −

  • i − An int for which string representation would be returned.

Return Value

  • toString() − This returns a String object representing the value of thisInteger.
  • toString(int i) − This returns a String object representing the specified integer.

Example

 Live Demo

publicclassTest{

 

publicstaticvoid main(String args[]){

Integer x =5;

 

System.out.println(x.toString());

System.out.println(Integer.toString(12));

}

}

This will produce the following result −

Output

5

12

 

 

用System.out.println()输出一个对象的时候,java默认调用对象的toString()方法

一般你要覆盖这个方法,这样根据覆盖逻辑你就可以输出自己的对象

比如你定义一个类User,有id,name属性,你直接输出一个user对象的话

System.out.println(user),得到的只是

全限定名@地址首地址

如果你在User类里面覆盖这个toString方法的话就能输出你要的

比如

public String toString(){

return "user name is:"+this.name+";"+"user id is:"+this.id;

}

这样你输出一个user对象就是

User user = new User(1,"zhangshan");

System.out.println(user);

得到:user name is :zhangshan;user id is: 1;

 

 

 

缺心眼的公牛

我每天都在成长和进步

JAVAtoString方法的作用

  

因为它是Object里面已经有了的方法,而所有类都是继承Object,所以"所有对象都有这个方法"

  

它通常只是为了方便输出,比如System.out.println(xx),括号里面的"xx"如果不是String类型的话,就自动调用xxtoString()方法

  

总而言之,它只是sun公司开发java的时候为了方便所有类的字符串操作而特意加入的一个方法

  

回答补充:

  

写这个方法的用途就是为了方便操作,所以在文件操作里面可用可不用

  

例子1

publicclass Orc

{

publicstaticclass A

{

public String toString()

{

return "this is A";

}

}

publicstaticvoid main(String[] args)

{

A obj = new A();

System.out.println(obj);

}

}

如果某个方法里面有如下句子: 

A obj=new A();

  

System.out.println(obj);

  

会得到输出:this is A

  

例子2

publicclass Orc

{

publicstaticclass A

{

public String getString()

{

return "this is A";

}

}

publicstaticvoid main(String[] args)

{

A obj = new A();

System.out.println(obj);

System.out.println(obj.getString());

}

}

会得到输出:xxxx@xxxxxxx的类名加地址形式

System.out.println(obj.getString());

  

会得到输出:this is A

  

看出区别了吗,toString的好处是在碰到"println"之类的输出方法时会自动调用,不用显式打出来。

  

  

  

  

1publicclass Zhang
2
3 {
4
5publicstaticvoid main(String[] args)
6
7 {
8
9 StringBuffer MyStrBuff1 = new StringBuffer();
10
11 MyStrBuff1.append("Hello, Guys!");
12
13 System.out.println(MyStrBuff1.toString());
14
15 MyStrBuff1.insert(6, 30);
16
17 System.out.println(MyStrBuff1.toString());
18
19 }
20
21 }

  

值得注意的是, 若希望将StringBuffer在屏幕上显示出来, 则必须首先调用toString方法把它变成字符串常量, 因为PrintStream的方法println()不接受StringBuffer类型的参数.

  

  

  

  

1publicclass Zhang
2 {
3publicstaticvoid main(String[] args)
4 {
5 String MyStr = new StringBuffer();
6 MyStr = new StringBuffer().append(MyStr).append(" Guys!").toString();
7 System.out.println(MyStr);
8 }
9 }

toString()方法在此的作用是将StringBuffer类型转换为String类型.

  

  

  

  

1publicclass Zhang
2 {
3publicstaticvoid main(String[] args)
4 {
5 String MyStr = new StringBuffer().append("hello").toString();
6 MyStr = new StringBuffer().append(MyStr).append(" Guys!").toString();
7 System.out.println(MyStr);
8 }
9 }

 

   

  

原创粉丝点击