【转载】复制网友关于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;
缺心眼的公牛
我每天都在成长和进步
JAVA中toString方法的作用
因为它是Object里面已经有了的方法,而所有类都是继承Object,所以"所有对象都有这个方法"。
它通常只是为了方便输出,比如System.out.println(xx),括号里面的"xx"如果不是String类型的话,就自动调用xx的toString()方法
总而言之,它只是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 }
- 【转载】复制网友关于tostring的看法
- 关于敏捷开发的网友看法--转载CSDN讨论区
- 关于toString()方法的一些看法
- 【转载】关于该不该上JAVA培训班的一点看法
- 关于创新的看法
- 关于中国文化的看法
- 关于hibernate的看法
- 关于楼市的看法
- 关于出国的看法
- 关于“鸡脚神”的看法
- 关于IT的看法
- 关于营销的看法
- 关于加班的看法
- 关于IT的看法
- 关于freertos的看法
- 关于红配绿的看法
- 摄像机标定学习笔记(12)关于相机标定的问题答复网友(转载)
- 关于转载“一种新的穿透防火墙的数据传输技术”的一点看法
- Opencv学习资料
- windows socket编程中调用recvfrom返回-1(windows error 10014)错误的问题
- FE
- 使用数据库连接池的好处
- bzoj1832: [AHOI2008]聚会(倍增lca)
- 【转载】复制网友关于tostring的看法
- pc端网页自适应各版本移动端屏幕大小
- 一个很有趣的访问,超范围。
- rn尺寸处理
- 今天rpm装glibc和glibc-common版本,出现二个包相互依赖,解决办法
- 1125 POJ#1363 Rails
- [blender]图片模糊不清楚怎么变清晰
- 序
- 西门子plc与modbus设备进485通信详解(modbus多机轮询)