Syntax:
句法:
public String toString();
public static String toString(short value);
短类toString()方法 (Short class toString() method)
toString() method is available in java.lang package.
toString()方法在java.lang包中可用。
toString() method is used to represent String denoted by this Short object.
toString()方法用于表示此Short对象表示的String。
toString(short value) method is used to represent String denoted by the given argument of short type.
toString(short value)方法用于表示由short类型的给定参数表示的String。
These methods don't throw an exception at the time of String representation.
这些方法在String表示时不会引发异常。
toString() is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
toString()是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
toString(int value) is a static method, it is accessible with the class name too and, if we try to access this method with the class object then also we will not get an error.
toString(int value)是一个静态方法,也可以使用类名进行访问,并且,如果我们尝试使用类对象访问此方法,那么也不会收到错误。
Parameter(s):
参数:
In the first case toString(), we don't pass any parameter or value.
在第一种情况下,toString(),我们不传递任何参数或值。
In the second case toString(short value), we pass only one parameter of short type it represents the short value to be converted.
在第二种情况下,toString(short value),我们仅传递一个short类型的参数,它表示要转换的short值。
Return value:
返回值:
In the first case, the return type of this method is String - it returns the String representation of this Short object.
在第一种情况下,此方法的返回类型为String-它返回此Short对象的String表示形式。
In the second case, the return type of this method is String - it returns the String representation of the given argument is of short type.
在第二种情况下,此方法的返回类型为String-它返回给定参数的String表示形式为short类型。
Example:
例:
// Java program to demonstrate the example
// of toString () method of Short class
public class ToStringOfShortClass {
public static void main(String[] args) {
short s1 = 100;
short s2 = 200;
// Object initialization
Short ob1 = new Short(s1);
Short ob2 = new Short(s2);
// Display ob1,ob2 values
System.out.println("ob1: " + ob1);
System.out.println("ob2: " + ob2);
// It represents the string of this Short object
String value1 = ob1.toString();
// It represents the string of the given short parameter
String value2 = Short.toString(ob2);
// Display result values
System.out.println("ob1.toString(): " + value1);
System.out.println("Short.toString(ob2): " + value2);
}
}
Output
输出量
ob1: 100
ob2: 200
ob1.toString(): 100
Short.toString(ob2): 200
翻译自: https://www.includehelp.com/java/short-class-tostring-method-with-example.aspx