java布尔类型方法命名_Java布尔类

java布尔类型方法命名

The Boolean class is wrapper in class that is used to wrap a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

布尔类是类中的包装器,用于将原始类型boolean的值包装在对象中。 布尔类型的对象包含一个布尔类型的字段。

This class consists of many helping methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods helpful while working with boolean.

此类包含许多用于将布尔值转换为String和将String转换为布尔值的帮助方法,以及在使用布尔值时有用的其他常量和方法。

This class is located into java.lang package and has following declaration.

此类位于java.lang包中,并具有以下声明。

宣言 (Declaration)

public final class Boolean extends Object implements Serializable, Comparable

Here we are explaining various methods of Boolean class and their example.

在这里,我们将解释布尔类的各种方法及其示例。

1. parseBoolean(String s) (1. parseBoolean(String s) )

The parseBoolean() method is used to parse the string argument as a boolean. It returns a boolean value either true or false. If the string argument is not null and is equal, ignoring case, to the string "true", it returns true.

parseBoolean()方法用于将字符串参数解析为布尔值。 它返回布尔值true或false。 如果字符串参数不为null,并且忽略大小写等于字符串“ true”,则返回true。

It takes a string argument that contains the boolean representation to be parsed.

它需要一个包含要解析的布尔表示形式的字符串参数。

句法 : (Syntax : )

public static boolean parseBoolean(String s)

例: (Example:)

In this example, we are parsing string arguments to the boolean type using the parseBoolean() method. This method returns true if the specified string can be parsed to boolean, false otherwise.

在此示例中,我们使用parseBoolean()方法将字符串参数解析为布尔类型。 如果可以将指定的字符串解析为布尔值,则此方法返回true,否则返回false。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = Boolean.parseBoolean("True"); 
    	boolean obj2 = Boolean.parseBoolean("False");
        boolean obj3 = Boolean.parseBoolean("StUdYtONiGhT"); 
        boolean obj4 = Boolean.parseBoolean("STUDYTONIGHT"); 
        boolean obj5 = Boolean.parseBoolean("studytonight"); 
        System.out.println(obj1); 
        System.out.println(obj2); 
        System.out.println(obj3); 
        System.out.println(obj4); 
        System.out.println(obj5);   
    } 
}
java布尔类型方法命名_Java布尔类_第1张图片

2. booleanValue() ( 2. booleanValue() )

The booleanValue() method is used to get boolean value of this Boolean object as a boolean primitive. It returns primitive type boolean value of the Boolean object. Declaration of this method is given below.

booleanValue()方法用于获取此布尔对象的布尔值作为布尔基元。 它返回布尔对象的原始类型布尔值。 该方法的声明在下面给出。

句法: (Syntax:)

public boolean booleanValue()

例: (Example:)

We are using booleanValue() method to get boolean value of the Boolean object. See the below example.

我们正在使用booleanValue()方法来获取布尔对象的布尔值。 请参见以下示例。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean("StUdYtONiGhT"); 
        Boolean obj3 = new Boolean("STUDYTONIGHT"); 
        Boolean obj4 = new Boolean("studytonight"); 

    	boolean obj5 = obj1.booleanValue();
    	boolean obj6 = obj2.booleanValue();
    	boolean obj7 = obj3.booleanValue();
    	boolean obj8 = obj4.booleanValue();

        System.out.println(obj5); 
        System.out.println(obj6); 
        System.out.println(obj7); 
        System.out.println(obj8);   
    } 
}
java布尔类型方法命名_Java布尔类_第2张图片

3. valueOf(boolean b) (3. valueOf(boolean b) )

The valueOf() method is used to get Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean. TRUE; if it is false, this method returns Boolean. FALSE.

valueOf()方法用于获取表示指定布尔值的布尔实例。 如果指定的布尔值是true,则此方法返回Boolean。 正确 ; 如果为false,则此方法返回Boolean。 假。

This method takes a boolean type argument and returns a Boolean object representing to the specified argument.

此方法采用布尔型参数,并返回表示指定参数的布尔对象。

句法: (Syntax:)

public static boolean valueOf(boolean b)

例: (Example:)

In this example, we are parsing primitive type boolean value to Boolean object. We used valueOf() method to get boolean object.

在此示例中,我们将原始类型的布尔值解析为布尔对象。 我们使用valueOf()方法获取布尔对象。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
    	boolean obj1 = true;
    	boolean obj2 = false;
    	
    	Boolean obj3 = Boolean.valueOf(obj1);
    	Boolean obj4 = Boolean.valueOf(obj2);

        System.out.println(obj3); 
        System.out.println(obj4);    
    } 
}
java布尔类型方法命名_Java布尔类_第3张图片

4. valueOf(String s) (4. valueOf(String s))

The valueOf() method is used to get a Boolean with a value represented by the specified string. It takes a string argument and returns a Boolean value represented by the specified argument.

valueOf()方法用于获取具有由指定字符串表示的值的布尔值。 它采用字符串参数,并返回由指定参数表示的布尔值。

句法: (Syntax:)

public static boolean valueOf(String s)

例: (Example:)

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = Boolean.valueOf("True"); 
        Boolean obj2 = Boolean.valueOf(null);
        Boolean obj3 = Boolean.valueOf("StUdYtONiGhT"); 
        Boolean obj4 = Boolean.valueOf("STUDYTONIGHT"); 
        Boolean obj5 = Boolean.valueOf("studytonight"); 
        System.out.println(obj1); 
        System.out.println(obj2); 
        System.out.println(obj3); 
        System.out.println(obj4); 
        System.out.println(obj5);   
    } 
}
java布尔类型方法命名_Java布尔类_第4张图片

5. toString(boolean b) (5. toString(boolean b) )

This method is used to get a String object representing the specified boolean. If the specified boolean is true, then the returned string will be "true", otherwise the string "false" will be returned.

此方法用于获取表示指定布尔值的String对象。 如果指定的布尔值为true,则返回的字符串将为“ true” ,否则将返回字符串“ false”

It takes a boolean argument and returns string representation of the specified boolean. Syntax of this method is given below.

它带有一个布尔参数,并返回指定布尔值的字符串表示形式。 该方法的语法如下。

句法: (Syntax:)

public static String toString(boolean b)

例: (Example:)

We can use this method to get string representation of boolean type value. In this example, we are taking two boolean variables that hold boolean value and getting their string value using the toString() method. See the below example.

我们可以使用此方法获取布尔类型值的字符串表示形式。 在此示例中,我们将使用两个持有布尔值的布尔变量,并使用toString()方法获取其字符串值。 请参见以下示例。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = true; 
        boolean obj2 = false; 

        String s1 = Boolean.toString(obj1); 
        String s2 = Boolean.toString(obj2); 

        System.out.println(s1); 
        System.out.println(s2);
    } 
}
java布尔类型方法命名_Java布尔类_第5张图片

6. toString() (6. toString() )

This method returns a String object representing this Boolean's value. If this object represents the value true, a string equal to "true" is returned. Otherwise, a string equal to "false" is returned. It does not take any argument.

此方法返回一个表示此布尔值的String对象。 如果此对象表示值true,则返回等于“ true”的字符串。 否则,返回等于“ false”的字符串。 它不需要任何参数。

句法: (Syntax:)

public String toString()

例: (Example:)

In this example, we are getting string representation of an Boolean object by using the toString() method.

在此示例中,我们使用toString()方法获取布尔对象的字符串表示形式。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 
	
    	String s1 = obj1.toString();
    	String s2 = obj2.toString();
    	String s3 = obj3.toString();
    	String s4 = obj4.toString();
    	String s5 = obj5.toString();

		System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
    } 
}
java布尔类型方法命名_Java布尔类_第6张图片

7. int hashCode() (7. int hashCode() )

This method is used to get a hash code for a boolean value. It returns a hash code value of an boolean object. It does not take any parameter but returns an int value that represents hash code of a boolean value.

此方法用于获取布尔值的哈希码。 它返回布尔对象的哈希码值。 它不带任何参数,但返回一个int值,该值表示布尔值的哈希码。

This method was included in Java 8 version. Syntax of this method is given below.

此方法包含在Java 8版本中。 该方法的语法如下。

句法: (Syntax:)

public int hashCode()

例: (Example:)

Lets create an example to get hash code of a boolean value. Here we have created several boolean variables that we used to fetch different hash code value.

让我们创建一个示例以获取布尔值的哈希码。 在这里,我们创建了几个布尔变量,用于获取不同的哈希码值。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.hashCode()); 
        System.out.println(obj2.hashCode()); 
        System.out.println(obj3.hashCode()); 
        System.out.println(obj4.hashCode()); 
        System.out.println(obj5.hashCode()); 
    } 
}
java布尔类型方法命名_Java布尔类_第7张图片

8. equals(Object obj) (8. equals(Object obj))

The equal() method is used to check if two boolean objects represent the same value. This method is used to compare two boolean values and returns true if both are equal, false otherwise.

equal()方法用于检查两个布尔对象是否表示相同的值。 此方法用于比较两个布尔值,如果两个相等,则返回true,否则返回false。

It takes an argument that should not be null and returns a boolean value either true or false. Syntax of this method is given below.

它接受一个不应该为null的参数,并返回布尔值true或false。 该方法的语法如下。

句法: (Syntax:)

boolean equals(Object obj)

例: (Example:)

Lets create an example to compare two boolean values whether they have the same value or not. It returns true if both the values are same.

让我们创建一个示例,比较两个布尔值是否具有相同的值。 如果两个值相同,则返回true。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.equals(obj2)); 
        System.out.println(obj2.equals(obj3)); 
        System.out.println(obj3.equals(obj4)); 
        System.out.println(obj4.equals(obj5)); 
        System.out.println(obj5.equals(obj1)); 
    } 
}
java布尔类型方法命名_Java布尔类_第8张图片

9. compareTo(Boolean b) (9. compareTo(Boolean b) )

The compareTo() method is used to compare a Boolean instance with another. This method is specified in the Comparable interface that takes a single argument of Boolean type.

compareTo()方法用于将布尔实例与另一个实例进行比较。 在Comparable接口中指定此方法,该接口采用布尔类型的单个参数。

It returns 0 if both the object represents the same boolean value; a positive value if this object holds true while the argument represents false; and a negative value if this object represents false and the argument represents true value.

如果两个对象都表示相同的布尔值,则返回0;否则,返回0。 如果此对象在参数表示false时为true,则为正值; 如果此对象表示false且参数表示真实值,则为负值。

句法: (Syntax:)

public int compareTo(Boolean b)

例: (Example:)

lets take an example to compare two boolean objects using compareTo(). This will return an int value based on the object comparison.

让我们举一个例子,使用compareTo()比较两个布尔对象。 这将基于对象比较返回一个int值。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.compareTo(obj2)); 
        System.out.println(obj2.compareTo(obj3)); 
        System.out.println(obj3.compareTo(obj4)); 
        System.out.println(obj4.compareTo(obj5)); 
        System.out.println(obj5.compareTo(obj1)); 
    } 
}
java布尔类型方法命名_Java布尔类_第9张图片

10. int compare(布尔x,布尔y) (10. int compare(boolean x, boolean y) )

The compare() method is used to compare two boolean values. It takes two boolean arguments and return 0 if both values are equal; either negative or positive integer. The syntax of this method is given below.

compare()方法用于比较两个布尔值。 它使用两个布尔参数,如果两个值相等,则返回0;否则,返回0。 正整数或负整数。 下面给出了此方法的语法。

句法: (Syntax:)

public static int compare(boolean x, boolean y)

例: (Example:)

Lets take an example that compare two boolean values and returns integer based on the comparison.

让我们举一个比较两个布尔值并根据比较结果返回整数的示例。

public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = true;
        boolean obj2 = false;
        boolean obj3 = true;
        boolean obj4 = false;
        boolean obj5 = true;

		System.out.println(Boolean.compare(obj1, obj2)); 
        System.out.println(Boolean.compare(obj2, obj3)); 
        System.out.println(Boolean.compare(obj3, obj4)); 
        System.out.println(Boolean.compare(obj4, obj5)); 
        System.out.println(Boolean.compare(obj5, obj1)); 
    } 
}
java布尔类型方法命名_Java布尔类_第10张图片

翻译自: https://www.studytonight.com/java/boolean-class.php

java布尔类型方法命名

你可能感兴趣的:(字符串,java,反射,object,接口)