字符串tostring
对象类字符串toString() (Object Class String toString())
This method is available in java.lang.Object.toString().
此方法在java.lang.Object.toString()中可用。
This method is used to return the string representation of the object.
此方法用于返回对象的字符串表示形式。
This method returns a string of the object that is easy to understand for the end user.
此方法返回最终用户易于理解的对象字符串。
This method does not return an exception.
此方法不返回异常。
Syntax:
句法:
String toString(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Object class.
我们没有在Object类的方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is String that means this method returns String representation of the object.
该方法的返回类型为String ,这意味着该方法返回对象的String表示形式。
Java程序演示对象类toString()方法的示例 (Java program to demonstrate example of Object Class toString() method)
import java.lang.Object;
import java.util.LinkedList;
public class ObjectClass {
public static void main(String[] args) {
// Create an object of Float type
Float fl = new Float(10.f);
// Create an object of linked list type
LinkedList ll = new LinkedList();
// Add few elements in Linked List
ll.add(10);
ll.add(20);
// Display String representation of the objects
System.out.println("The String representation of the Object fl is: " + fl.toString());
System.out.println("The String representation of the Object ll is: " + ll.toString());
}
}
Output
输出量
D:\Programs>javac ObjectClass.java
D:\Programs>java ObjectClass
The String representation of the Object fl is :10.0
The String representation of the Object ll is :[10, 20]
翻译自: https://www.includehelp.com/java/object-class-string-tostring-method-with-example.aspx
字符串tostring