Java.lang - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Boolean

Java.lang Boolean

shape Description

The functionality of java.lang.Boolean class is that to wrap a value of primitive type boolean in an object. This class will hire the methods from java.lang.Object class. Object class is the root class for all Java classes, following is the syntax declaration of java.lang.Boolean class. [java] public final class Boolean extends Object implements Serializable, Comparable<Boolean> [/java] The functionality of java.lang.Byte class is that to wrap a value of primitive type byte in an object. This class will hire the methods from java.lang.Object class. Following is the syntax declaration for this class. [java] public final class Byte extends Number implements Comparable<Byte>[/java]

java.lang Boolean Class

shape Methods

Following are the regularly using methods of java.lang Boolean Class.
Method Description
booleanValue() To make the Boolean object as boolean primitive
compareTo() To compare Boolean object with another.
getBoolean(String) Returns is valid if and just if the framework property named by the parameter exists and is equivalent to the string "true".
hashCode() To return a unique number to the Boolean.
valueOf(String) To return the Boolean value that indicated by the given String.

shape Constructors

Following are the constructors of java.lang Boolean Class.
Constructor Description
Boolean(String) Assigns a Boolean object indicating the value true if the string contention is not invalid and is equivalent, disregarding case, to the string true.
Boolean(boolean) Assigns a Boolean object indicating the value parameter.

shape Example

Following is an example to understand the functionality of java.lang.Boolean.booleanValue() method. DemoBoolean.java [java] package com.SPlessons; public class DemoBoolean { public static void main(String[] args) { // b is object Boolean b; // assign value to b b = new Boolean(true); // create a boolean primitive type bool boolean bool; // assign primitive value of b to bool bool = b.booleanValue(); String str = "The Primitive value of Boolean object " + b + " is " + bool; // print bool value System.out.println( str ); } } [/java] Where created the object to the Boolean that is b and also created the primitive to the Boolean that is bool. Output: The functionality of booleanValue() is that to make the Boolean object as boolean primitive. [java]The Primitive value of Boolean object true is true[/java] Following is the code for java.lang.Boolean.compareTo(Boolean b) method. [java] package com.SPlessons; public class DemoBoolean { public static void main(String[] args) { // b1, b2 are objects Boolean b1, b2; // values to b1, b2 b1 = new Boolean(true); b2 = new Boolean(false); // create an int res int res; // compare b1 with b2 res = b1.compareTo(b2); String str1 = "Both values are equal "; String str2 = "Object value is true"; if( res == 0 ){ System.out.println( str1 ); } else if( res > 0 ){ System.out.println( str2 ); } } } [/java] Where the developer is comparing the two objects by using the method compareTo() and written some logic by using two strings. Output: The functionality of compareTo() is to compare the Boolean instance with another. [java]Object value is true[/java]

java.lang Byte Class

shape Methods

Following are the regularly using methods of java.lang Byte Class.
Method Description
byteValue() The value of Byte will be returned as byte.
decode(String) The string will be decoded as byte.
parseByte(String) To parse the String argument as a signed byte.
valueOf(String) To give back a Byte object value given by the String.
hashCode() To return the hash code for a byte.

shape Constructors

Following are the constructors of java.lang Boolean Class.
Constructor Description
Byte(byte value) Develops a Byte object instated to the predetermined byte esteem.
Byte(String s) Builds a Byte object instated to the value indicated by the String argument.

shape Example

Following is an example for java.lang.Byte.byteValue() method. DemoByte.java [java] package com.SPlessons; public class DemoByte { public static void main(String[] args) { // Byte object b Byte b; // value to b b = new Byte("50"); // create a byte primitive bte byte bte; // assign primitive value of b to bt bte = b.byteValue(); String string = "The Primitive byte value of Byte object " + b + " is " + bte; // print bt value System.out.println( string ); } } [/java] The byteValue() method return Byte value as the byte. Where the developer created the primitive byte that is bte. Output: Following is the result. [java] The Primitive byte value of Byte object 50 is 50 [/java] Following is an example for the Java.lang.Byte.decode() Method. [java] package com.SPlessons; public class DemoByte { public static void main(String[] args) { // create 4 Byte objects Byte b1, b2, b3, b4; b1 = Byte.decode("50"); // hexadecimal values are decoded and assigned to Byte objects b2, b3 b2 = Byte.decode("0x6b"); b3 = Byte.decode("-#4c"); // octal value is decoded and assigned to Byte object b4 b4 = Byte.decode("0127"); String string1 = "Byte value of decimal 50 is " + b1; String string2 = "Byte value of hexadecimal 6b is " + b2; String string3 = "Byte value of hexadecimal -4c is " + b3; String string4 = "Byte value of octal 127 is " + b4; // print b1, b2, b3, b4 values System.out.println( string1 ); System.out.println( string2 ); System.out.println( string3 ); System.out.println( string4 ); } } [/java] In the above code, the developer used hexadecimal values to decode them as a byte. Output:Now compile the code result will be as follows. [java] Byte value of decimal 50 is 50 Byte value of hexadecimal 6b is 107 Byte value of hexadecimal -4c is -76 Byte value of octal 127 is 87 [/java] Following is an example to Java.lang.Byte.parseByte() method. [java] package com.SPlessons; public class DemoByte { public static void main(String[] args) { byte bt1, bt2; String s1 = "+121"; String s2 = "-123"; bt1 = Byte.parseByte(s1); bt2 = Byte.parseByte(s2); String str1 = "Parse byte value of " + s1 + " is " + bt1; String str2 = "Parse byte value of " + s2 + " is " + bt2; // print bt1, bt2 values System.out.println( str1 ); System.out.println( str2 ); System.out.println("parse string to byte:"+Byte.parseByte("10")); } } [/java] Where the developer has taken two primitives for the bytes that bt1, bt2. Values are assigned to them and performed parseByte() method. Output:Now check the result as follows. [java] Parse byte value of +121 is 121 Parse byte value of -123 is -123 parse string to byte:10 [/java]

Summary

shape Key Points

  • The byte method of intValue() is used to return Byte value as int.
  • The MAX_VALUE is the field of byte to have the maximum value of byte.
  • Boolean class and Byte classes will hire the methods from java.lang.Object.