AppDividend
Latest Code Tutorials

Java.lang.Boolean Class In Java Example From Scratch

0

Java.lang.Boolean Class In Java Example From Scratch is today’s topic. Java.lang.Boolean class wraps are used for wrapping the value of primitive data type, i.e. boolean in an object. This boolean class contains two values, i.e. true or false. Java provides a wrapper class Boolean in java.lang package. 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.

Java.lang.Boolean Class In Java

The Boolean class simply a wrapper class for the primitive type boolean. It wraps the boolean primitive value to an object. An object of type Boolean contains a single field whose type is boolean.

The syntax for creating a boolean object.

Boolean y = new Boolean(boolean arg);

In the above syntax, we have created a boolean object which contains arg as a value.

Boolean y = new Boolean(String str);

The above syntax is true if the string is not null and is equal, and in this, we can ignore the case otherwise false.

Field:

  1. static Boolean FALSE-  This boolean object contains the value of primitive data as false.
  2. Static Boolean TRUE- This boolean object contains the value of primitive data as true.
  3. Static Class<Boolean> TYPE- It is the class object of primitive type boolean.

Methods of Java.lang.Boolean:

static boolean parseBoolean(String str):

This method returns the string argument as a boolean. The method will return true if the string is not null and is equal, and in this, we can ignore the case; otherwise, it returns false if it is not matched.

See the following syntax.

public static boolean parseBoolean(String str) {}

See the program.

public class demo {
    public static void main(String args[]) {
        boolean a = Boolean.parseBoolean("False");
        boolean b = Boolean.parseBoolean("True");
        boolean c = Boolean.parseBoolean("faLsE");
        boolean d = Boolean.parseBoolean("AppDividend");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

See the output.

 

Java.lang.Boolean Class In Java

boolean booleanValue():

This method only returns the value of the boolean object as boolean.

Syntax:

public boolean booleanValue()

See the following Java program.

public class demo {
    public static void main(String args[]) {
        Boolean a = new Boolean("false");
        boolean a1 = a.booleanValue(); // getting the value
        Boolean b = new Boolean("True");
        boolean b1 = b.booleanValue(); // getting the value
        Boolean c = new Boolean("Appdividend");
        boolean c1 = c.booleanValue(); // getting the value

        System.out.println(a1);
        System.out.println(b1);
        System.out.println(c1);
    }
}

See the following output.

 

boolean booleanValue()

static Boolean valueOf(boolean a):

This method returns a boolean object containing a boolean value.

Syntax:

public static boolean valueOf(boolean a);

See the following program.

public class demo {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        Boolean a1 = Boolean.valueOf(a);
        Boolean b1 = Boolean.valueOf(b);
        System.out.println(a1);
        System.out.println(b1);
    }
}

See the following output.

 

static Boolean valueOf(boolean a)

Static Boolean valueOf(String s):

This method returns a Boolean output with a value represented by the string ‘s’. It returns true only if the string argument is not null and is equal, ignoring the case. It returns false when the string is not equal.

See the following syntax.

public static boolean valueOf(String s)

See the following program.

public class demo {
    public static void main(String args[]) {
        Boolean a = Boolean.valueOf("tRue");
        Boolean b = Boolean.valueOf("false");
        Boolean c = Boolean.valueOf("null");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

See the output.

 

static Boolean valueOf(String s)

Static String toString(boolean b):

It returns the string representing a specific boolean. In this, if the string is true, it will return true otherwise false. See the following syntax.

public static String toString(boolean b)

See the following program.

public class demo {
    public static void main(String args[]) {
        boolean a = true;
        boolean b = false;
        String a1 = Boolean.toString(a);
        String b2 = Boolean.toString(b);
        System.out.println(a1);
        System.out.println(b2);
    }
}

See the output.

 

static String toString(boolean b)

String toString()

It returns a string object representing a boolean value. It returns true if the string is equal to “true,” otherwise false is returned.

See the following program.

public class demo {
    public static void main(String args[]) {
        Boolean a = new Boolean("True");
        Boolean b = new Boolean("False");
        String str1 = a.toString();
        String str2 = b.toString();
        System.out.println(str1);
        System.out.println(str2);
    }
}

See the output.

 

String toString()

int hashCode

This method returns a hash code for the boolean instance. The hash code is fixed here, for true it’s 1231, and for false it’s 1237.

See the following syntax.

public int hashCode()

See the following program.

public class demo {
    public static void main(String[] args) {
        Boolean a = new Boolean("True");
        Boolean b = new Boolean("false");
        Boolean c = new Boolean("TRue");

        System.out.println(a.hashCode());
        System.out.println(b.hashCode());
        System.out.println(c.hashCode());
    }
}

See the output.

 

int hashCode

boolean equals(Object ob):

It returns true only if the boolean object represents the same value; otherwise false.

See the following syntax.

public boolean equals(Object ob)

See the following program.

public class demo {
    public static void main(String args[]) {
        Boolean a = new Boolean("True");
        Boolean b = new Boolean("False");
        Boolean c = new Boolean(null);
        System.out.println(a.equals(b));
        System.out.println(b.equals(b));
        System.out.println(c.equals(a));
    }
}

See the output.

 

boolean equals(Object ob)

int compareTo(Boolean b):

It compares the present boolean object with the passed argument b.

See the following syntax.

public int compareTo(Boolean b)

The function returns the following output:

  1. Zero when object matches with the argument value.
  2. A positive value when the object represents true, whereas the argument represents false value.
  3. A negative value when the object represents false, whereas the argument represents true.

See the following program.

public class demo {
    public static void main(String args[]) {
        Boolean a = new Boolean("True");
        Boolean b = new Boolean("False");
        Boolean c = new Boolean("TRue");

        System.out.println(b1.compareTo(b));
        System.out.println(b1.compareTo(c));
        System.out.println(b2.compareTo(a));
    }
}

See the output.

 

int compareTo(Boolean b)

int compare(boolean x, boolean y):

It is used to compare the primitive boolean variables like if x is true and y is false, the output is positive.

See the following syntax.

public static int compare(boolean x, boolean y)

It returns zero when x and y have the same value as true-true or false-false and positive when x->true and y->false.

See the following program.

public class demo {
    public static void main(String args[]) {
        boolean a = true;
        boolean b = false;
        boolean c = true;
        System.out.println(Boolean.compare(a, b));
        System.out.println(Boolean.compare(a, c));
        System.out.println(Boolean.compare(b, c));
    }
}

See the output.

int compare(boolean x, boolean y)

 

Finally, Java.lang.Boolean Class In Java Example From Scratch is over.

Recommended Posts

Java StringBuilder Class

Java Comparable Interface Tutorial

Java Packages Tutorial With Example

Method Overriding in Java Tutorial

Java Assertion Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.