Showing posts with label Boolean Class. Show all posts
Showing posts with label Boolean Class. Show all posts

Boolean class Method in Java

java.lang.Boolean

The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean. 

Some Methods of Boolean class.

Boolean.compare() : It compares two boolean values.

Boolean.FALSE: The Boolean object corresponding to the primitive value false.

Boolean.getBoolean(): It returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string "true".

Boolean.hashCode(): It returns a hash code for a boolean value.

Boolean.logicalAnd(): It returns the result of applying the logical AND operator to the specified boolean operands.

Boolean.logicalOr(): It returns the result of applying the logical OR operator to the specified boolean operands.

Boolean.logicalXor(): It returns the result of applying the logical XOR operator to the specified boolean operands.

Boolean.parseBoolean(): It parses the string argument as a boolean. 

Boolean.toString(): It returns a String object representing the specified boolean.

Boolean.TRUE: The Boolean object corresponding to the primitive value true.

Boolean.valueOf(): It returns a Boolean instance representing the specified boolean value.

Boolean.valueOf(String s): It returns a Boolean with a value represented by the specified string.


Boolean.valueOf() String in Java

Boolean.valueOf(): This method is available in the java.lang.Boolean class of Java.

Syntax:

Boolean java.lang.Boolean.valueOf(String s)

This method takes one argument of type String as its parameter. This method returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".Otherwise, a false value is returned, including for a null argument.

Parameters: One parameter is required for this method.

s: a string.

Returns: the Boolean value represented by the string.

For Example:

Boolean.valueOf("Hello") = > It returns false.

Approach

Java

public class BooleanvalueOfString {
    public static void main(String[] args) {

        String s = "Hello";
        System.out.println(Boolean.valueOf(s));

    }
}

Output:

false

Boolean.valueOf() in Java

Boolean.valueOf(): This method is available in the java.lang.Boolean class of Java.

Syntax:

Boolean java.lang.Boolean.valueOf(boolean b)

This method takes one argument of type boolean as its parameter. This method returns a Boolean instance representing the specified boolean value.

Parameters: One parameter is required for this method.

b: a boolean value.

Returns: a Boolean instance representing b.

For Example:

Boolean.valueOf(true) = > It returns true. 

Approach

Java

public class BooleanvalueOfBoolean {
    public static void main(String[] args) {

        boolean b = true;
        System.out.println(Boolean.valueOf(b));

    }
}

Output:

true

Boolean.TRUE in Java

Boolean.TRUE: This method is available in the java.lang.Boolean class of Java.

Syntax:

Boolean java.lang.Boolean.TRUE

The Boolean object corresponding to the primitive value true.

Approach

Java

public class BooleanTrue {
    public static void main(String[] args) {

        System.out.println(Boolean.TRUE);

    }
}

Output:

true

Boolean.toString() in Java

Boolean.toString(): This method is available in the java.lang.Boolean class of Java.

Syntax:

String java.lang.Boolean.toString(boolean b)

This method takes one argument of type boolean as its parameter. This method returns a String object representing the specified boolean. If the specified boolean is true, then the string "true" will be returned, otherwise the string "false" will be returned. 

Parameters: One parameter is required for this method.

b: the boolean to be converted.

Returns: the string representation of the specified boolean.

For Example:

Boolean.toString(true) = > It returns true.

Approach

Java

public class BooleantoString {
    public static void main(String[] args) {

        boolean b = true;
        System.out.println(Boolean.toString(b));

    }
}

Output:

true

Boolean.parseBoolean() in Java

Boolean.parseBoolean(): This method is available in the java.lang.Boolean class of Java.

Syntax:

boolean java.lang.Boolean.parseBoolean(String s)

This method takes one argument of type String as its parameter. This method parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".Otherwise, a false value is returned, including for a null argument. 

Example: 

1. Boolean.parseBoolean("True") returns true.

2. Boolean.parseBoolean("yes") returns false. 

Parameters: One parameter is required for this method.

s: the String containing the boolean representation to be parsed.

Returns: the boolean represented by the string argument.

For Example:

Boolean.parseBoolean("True") = > It returns true.

Approach

Java

public class BooleanparseBoolean {
    public static void main(String[] args) {

        String s = "True";
        System.out.println(Boolean.parseBoolean(s));

    }
}

Output:

true

Boolean.logicalXor() in Java

Boolean.logicalXor(): This method is available in the java.lang.Boolean class of Java.

Syntax:

boolean java.lang.Boolean.logicalXor(boolean a, boolean b)

This method takes two arguments of type boolean as its parameters. This method returns the result of applying the logical XOR operator to the specified boolean operands.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the logical XOR of a and b.

For Example:

Boolean.logicalXor(true,false) = > It returns true. 

Approach

Java

public class BooleanlogicalXor {
    public static void main(String[] args) {

        boolean a = true, b = false;
        System.out.println(Boolean.logicalXor(a, b));

    }
}

Output:

true

Boolean.logicalOr() in Java

Boolean.logicalOr(): This method is available in the java.lang.Boolean class of Java.

Syntax:

boolean java.lang.Boolean.logicalOr(boolean a, boolean b)

This method takes two arguments of type boolean as its parameters. This method returns the result of applying the logical OR operator to the specified boolean operands.

Parameters: Two parameters are required for this method.

 a: the first operand.

b: the second operand.

Returns: the logical OR of a and b.

For Example:

Boolean.logicalOr(true,false) = > It returns true. 

Approach

Java

public class BooleanlogicalOr {
    public static void main(String[] args) {

        boolean a = true, b = false;
        System.out.println(Boolean.logicalOr(a, b));

    }
}

Output:

true

Boolean.logicalAnd() in Java

Boolean.logicalAnd(): This method is available in the java.lang.Boolean class of Java.

Syntax:

boolean java.lang.Boolean.logicalAnd(boolean a, boolean b)

This method takes two arguments of type boolean as its parameters. This method returns the result of applying the logical AND operator to the specified boolean operands.

Parameters: Two parameters are required for this method.

a: the first operand.

b: the second operand.

Returns: the logical AND of a and b.

For Example:

Boolean.logicalAnd(true,true) = > It returns true.

Approach

Java

public class BooleanlogicalAnd {
    public static void main(String[] args) {

        boolean a = true, b = true;
        System.out.println(Boolean.logicalAnd(a, b));

    }
}

Output:

true

Boolean.hashCode() in Java

Boolean.hashCode(): This method is available in the java.lang.Boolean class of Java.

Syntax:

int java.lang.Boolean.hashCode(boolean value)

This method takes one argument of type boolean as its parameter. This method returns a hash code for a boolean value.

Parameters: One parameter is required for this method.

value: the value to the hash. 

Returns: a hash code value for a boolean value.

For Example:

Boolean.hashCode(true) = > It returns 1231.

Approach

Java

public class BooleanhashCode {
    public static void main(String[] args) {

        boolean value = true;
        System.out.println(Boolean.hashCode(value));

    }
}

Output:

1231

Boolean.getBoolean() in Java

Boolean.getBoolean(): This is available in the java.lang.Boolean class of Java.

Syntax:

boolean java.lang.Boolean.getBoolean(String name)

This method takes two arguments of type String as its parameters. This method returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string "true".

Parameters: One parameter is required for this method.

name: the system property name.

Returns: the boolean value of the system property.

Throws: SecurityException - for the same reasons as System.getProperty

For Example:

Boolean.getBoolean("Aryan") = > It returns false.

Approach

Java

public class BooleangetBoolean {
    public static void main(String[] args) {

        String name = "Aryan";

        System.out.println(Boolean.getBoolean(name));

    }
}

Output:

false

Boolean.FALSE in Java

Boolean.FALSE: This is available in the java.lang.Boolean class of Java.

Syntax:

Boolean java.lang.Boolean.FALSE

The Boolean object corresponding to the primitive value false.

Approach

Java

public class BooleanFalse {
    public static void main(String[] args) {

        System.out.println(Boolean.FALSE);

    }
}

Output:

false

Boolean.compare() in Java

Boolean.compare(): This method is available in the java.lang.Boolean class of Java.

Syntax:

int java.lang.Boolean.compare(boolean x, boolean y)

This method takes two arguments of type boolean as its parameters. This method compares two boolean values.

Parameters: Two parameters are required for this method.

x: the first boolean to compare.

y: the second boolean to compare.

Returns: 

1. The value 0 if x == y.

2. A value less than 0 if !x && y.

3. A value greater than 0 if x && !y.

For Example:

Boolean.compare(true,false) = > It returns 1.

Approach

Java

public class BooleanCompare {
    public static void main(String[] args) {

        boolean x = true, y = false;
        System.out.println(Boolean.compare(x, y));

    }
}

Output:

1