Syntax:
句法:
public String toString();
public static String toString(boolean value);
布爾類toString()方法 (Boolean class toString() method)
toString() method is available in java.lang package.
toString()方法在java.lang包中可用。
toString() method is used to represent String denoted by this Boolean object.
toString()方法用于表示此布爾對象表示的String。
toString(boolean value) method is used to represent String denoted by the given argument of boolean type.
toString(boolean value)方法用于表示由給定的boolean類型的參數表示的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(boolean value) is a static method, it is accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.
toString(boolean value)是一個靜態方法,也可以使用類名進行訪問,并且,如果我們嘗試使用類對象訪問這些方法,那么我們也不會收到錯誤。
Parameter(s):
參數:
In the first case toString(), we don't pass any parameter or value.
在第一種情況下, toString() ,我們不傳遞任何參數或值。
In the second case toString(boolean value), we pass only one parameter of the boolean type it represents the boolean value to be converted.
在第二種情況下toString(boolean value) ,我們僅傳遞一個布爾類型的參數,它表示要轉換的布爾值。
Return value:
返回值:
In the first case, the return type of this method is String - it returns the String representation of this Boolean object.
在第一種情況下,此方法的返回類型為String-它返回此布爾對象的String表示形式。
In the second case, the return type of this method is String - it returns the String representation of the given argument is of boolean type.
在第二種情況下,此方法的返回類型為String-它返回給定參數的String表示形式為布爾類型。
Note: If this Boolean object value is true, it returns the desired string true. Else, if this Boolean object value is not equal to true, it returns the desired string false.
注意:如果此布爾對象的值為true ,則返回所需的字符串true 。 否則,如果此布爾對象值不等于true ,它將返回所需的字符串false 。
Example:
例:
// Java program to demonstrate the example
// of toString() method of Boolean class
public class ToStringOfBooleanClass {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
// Object initialization
Boolean ob1 = new Boolean(b1);
Boolean ob2 = new Boolean(b2);
// Display ob1,ob2 values
System.out.println("ob1:" + ob1);
System.out.println("ob2:" + ob2);
// It represents the string of this Boolean object
String value1 = ob1.toString();
// It represents the string of the given boolean parameter
String value2 = Boolean.toString(ob2);
// Display result values
System.out.println("ob1.toString(): " + value1);
System.out.println("Boolean.toString(ob2): " + value2);
}
}
Output
輸出量
ob1:true
ob2:false
ob1.toString(): true
Boolean.toString(ob2): false
翻譯自: https://www.includehelp.com/java/boolean-class-tostring-method-with-example.aspx