{"id":1405,"date":"2016-06-24T10:17:07","date_gmt":"2016-06-24T10:17:07","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1405"},"modified":"2016-06-24T10:17:07","modified_gmt":"2016-06-24T10:17:07","slug":"boolean","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/boolean.html","title":{"rendered":"Boolean Class in JAVA with Example"},"content":{"rendered":"<p><u><\/u>Boolean Class is a wrapper class that wraps the primitive type boolean. This class provides mechanism to convert primitive type \u201cboolean\u201d to object type \u201cBoolean\u201d or vice versa.<\/p>\n<p>Object of Class Boolean can contain only single value whose type is boolean.<\/p>\n<p>This class extends Object class and implements Serializable and Comparable interfaces.<\/p>\n<hr \/>\n<h4><strong>Boolean Class Constructors\u00a0<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. Boolean (boolean value)<\/strong><\/span><\/p>\n<p>This constructor creates a Boolean object with value which is of type boolean that can be true or false<\/p>\n<p><span style=\"color: #008000;\"><strong>2. Boolean (String string)<\/strong><\/span><\/p>\n<p>This constructor creates a Boolean object with value \u201ctrue\u201d if and only if value of string passed is \u201ctrue\u201d ignoring case or else object is initialized with \u201cfalse\u201d.<\/p>\n<hr \/>\n<h4><strong>Boolean Methods<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. boolean booleanValue()<\/strong><\/span><\/p>\n<p>This method returns primitive value of the Boolean object.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\r\n\r\n\/\/ Step 1: Create an object of Boolean class and assigning value to it\r\nBoolean booleanobj=new Boolean(true);\r\n\r\n\/\/ Step 2: Create a primitive type of boolean\r\nboolean booleanPrimitive;\r\n\r\n\/\/ Step 3: Assigning primitive value of booleanobj to booleanPrimitive value using booleanValue() method\r\nbooleanPrimitive = booleanobj.booleanValue();\r\n\r\nString str = \"Primitive value of booleanobj : \" + booleanobj + \" is \" + booleanPrimitive;\r\nSystem.<em>out<\/em>.println( str );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Primitive value of booleanobj : true is true\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean class object is created and value \u201ctrue\u201d is assigned to it<\/li>\n<li>In Step 2, primitive type of boolean is created<\/li>\n<li>In Step 3, value of Boolean object is assigned to primitive type boolean using booleanValue() method.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>2. int compareTo(Boolean b)<\/strong><\/span><\/p>\n<p>This method as the name suggests compares one Boolean object with other. This method returns int value which can be negative, positive or zero.<\/p>\n<p><strong>Zero<\/strong> is returned when the two objects have same values.<\/p>\n<p><strong>Positive<\/strong> value is returned when calling object has value \u201ctrue\u201d and argument object has value \u201cfalse\u201d.<\/p>\n<p><strong>Negative<\/strong> value is returned when calling object has value \u201cfalse\u201d and argument object has value \u201ctrue\u201d.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\r\n\/\/ Step 1: create Boolean objects\r\nBoolean boolean1, boolean2 ;\r\n\r\n\/\/ Step 2: assign values to two objects created in step 1\r\nboolean1 = new Boolean(true);\r\nboolean2 = new Boolean(false);\r\n\r\nint flag1 ;\u00a0\r\n\r\n\/\/ Step 3: compare boolean1 with boolean2 using compareTo() method\r\nflag1 = boolean1.compareTo(boolean2);\r\n\r\nif( flag1 == 0 ){\r\nSystem.<em>out<\/em>.println( \" Both values are equal \");\r\n}\r\nelse if( flag1 &gt; 0 ){\r\nSystem.<em>out<\/em>.println( \" Object value is true \" );\r\n}\r\nelse if( flag1 &lt; 0 ){\r\nSystem.<em>out<\/em>.println( \" Argument value is true \" );\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Object value is true\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean class objects are created.<\/li>\n<li>In Step 2, Values are assigned to the objects created in step 1.<\/li>\n<li>In Step 3, Boolean objects are compared using method compareTo()<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>3. boolean equals(Object obj)<\/strong><\/span><\/p>\n<p>This method returns boolean value true or false, true when the both the Boolean objects have same value, or else false.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\r\n\r\n\/\/ Step 1: create Boolean objects and assign values to them\r\nBoolean boolean1, boolean2 ,boolean3;\r\n\r\nboolean1 = new Boolean(true);\r\nboolean2 = new Boolean(false);\r\nboolean3 = new Boolean(false);\r\n\u00a0\r\n\/\/ Step 2: create a boolean primitive flag\r\nboolean flag1, flag2;\u00a0\r\n\r\n\/\/ Step 3: Assign the result of equals method to flag\r\nflag1 = boolean1.equals(boolean2);\r\nflag2 = boolean3.equals(boolean2);\u00a0\r\n\r\nSystem.<em>out<\/em>.println( \"Are boolean1 and boolean2 equal? : \" +flag1\u00a0 );\r\nSystem.<em>out<\/em>.println( \"Are boolean2 and boolean3 equal? : \" +flag2\u00a0 );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Are boolean1 and boolean2 equal? : false\r\nAre boolean2 and boolean3 equal? : true<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean class objects are created and values are assigned to them.<\/li>\n<li>In Step 2, boolean flag1 and flag2 are created.<\/li>\n<li>In Step 3, objects are compared using equals() method and boolean values are stored in the flag.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>4. static boolean getBoolean(String name)<\/strong><\/span><\/p>\n<p>This method is to check boolean value of System property. This method will return true if the system property named by the argument exists and has value \u201dtrue\u201d assigned to it or else this method will return false.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: create boolean primitives.\r\nboolean boolean1, boolean2;\u00a0\r\n\r\n\/\/ Step 2: Set values for system properties flag1 and flag2\r\nSystem.<em>setProperty<\/em>(\"flag1\",\"true\");\r\nSystem.<em>setProperty<\/em>(\"flag2\",\"xyz\");\r\n\r\n\/\/ Step 3: Assign system properties to boolean primitive types using getBoolean() method\r\nboolean1 = Boolean.<em>getBoolean<\/em>(\"flag1\");\r\nboolean2 = Boolean.<em>getBoolean<\/em>(\"flag2\");\r\n\r\nSystem.<em>out<\/em>.println( \"Boolean value of System property flag1 is : \" +boolean1\u00a0 );\r\nSystem.<em>out<\/em>.println( \"Boolean value of System property flag2 is : \" +boolean2\u00a0\u00a0 );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Boolean value of System property flag1 is : true\r\nBoolean value of System property flag2 is : false<strong style=\"font-family: 'Noto Serif', serif; line-height: 1.6471; background-color: #ffffff;\">\u00a0<\/strong><\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, boolean primitives are created.<\/li>\n<li>In Step 2, Values for two system properties are assigned using <em>setProperty<\/em> () method.<\/li>\n<li>In Step 3, System properties are assigned to boolean primitive types using getBoolean() method.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>5. int hashCode()<\/strong><\/span><\/p>\n<p>This method returns a hash code for the calling Boolean object.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: create 2 Boolean objects.\r\nBoolean boolean1, boolean2;\r\nboolean1= new Boolean(true);\r\nboolean2= new Boolean(false);\u00a0\r\n\r\n\/\/ Step 2: Hash Code values are obtained using hashCode() method.\u00a0\r\nSystem.<em>out<\/em>.println( \" Hashcode value of boolean1 is : \" + boolean1.hashCode() );\r\nSystem.<em>out<\/em>.println( \" Hashcode value of boolean2 is : \" + boolean2.hashCode() );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Hashcode value of boolean1 is : 1231\r\nHashcode value of boolean2 is : 1237<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 2 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean objects are created and values are assigned to them.<\/li>\n<li>In Step 2, Hash codes are obtained using hashCode() method.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #008000;\">6. static boolean parseBoolean(String s)<\/span><\/strong><\/p>\n<p>This method parses the value of string arguments, and will return true if and only if string value is \u201ctrue\u201d (ignoring case) or else it will return false.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: Create two strings and assign values to them\r\nString string1 = \"tRuE\";\r\nString string2 = \"xyz\";\u00a0\r\n\r\n\/\/ Step 2: Create 2 boolean primitives\r\nboolean boolean1, boolean2;\u00a0\r\n\r\n\/\/Step 3: Assigning values to primitive types using parseBoolean() method\r\nboolean1 = Boolean.<em>parseBoolean<\/em>(string1);\r\nboolean2 = Boolean.<em>parseBoolean<\/em>(string2);\r\n\r\nSystem.<em>out<\/em>.println( \" Value of boolean1 is : \" + boolean1 );\r\nSystem.<em>out<\/em>.println( \" Value of boolean1 is : \" + boolean2 );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Value of boolean1 is : true\r\nValue of boolean1 is : false<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, two strings are created and values are assigned to them.<\/li>\n<li>In Step 2, boolean types are created.<\/li>\n<li>In Step 3, Values are assigned to primitive types using parseBoolean() method.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>7. String toString()<\/strong><\/span><\/p>\n<p>This method returns a String object which represents the value of calling Boolean object. If the value of Boolean object is null then this method will return false.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\r\n\r\n\/\/ Step 1: create Boolean objects.\r\nBoolean boolean1, boolean2, boolean3;\r\n\r\n\/\/ Step 2: Assign values to objects created in Step 1\r\nboolean1= new Boolean(true);\r\nboolean2= new Boolean(false);\r\nboolean3= new Boolean(null);\u00a0\r\n\r\n\/\/ Step 3: Assign String values of Boolean objects using toString() method\r\nString string1= boolean1.toString();\r\nString string2= boolean2.toString();\r\nString string3= boolean3.toString();\u00a0\r\n\r\nSystem.<em>out<\/em>.println( \" String value of Boolean object boolean1 is\u00a0 \" + string1 );\r\nSystem.<em>out<\/em>.println( \" String value of Boolean object boolean2 is\u00a0 \" + string2 );\r\nSystem.<em>out<\/em>.println( \" String value of Boolean object boolean3 is\u00a0 \" + string3 );\r\n}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>String value of Boolean object boolean1 is\u00a0 true\r\nString value of Boolean object boolean2 is\u00a0 false\r\nString value of Boolean object boolean3 is\u00a0 false<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean class objects are created<\/li>\n<li>In Step 2, Values are assigned to objects created in Step 1.<\/li>\n<li>In Step 3, String values of Boolean objects are assigned to String objects using toString() method<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>8. static String toString(boolean b)<\/strong><\/span><\/p>\n<p>This method returns a String object which represents the value of calling boolean primitive type. This method is called directly from the Boolean class as this method is static.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: create boolean primitives.\r\nboolean boolean1, boolean2;\u00a0\r\n\r\n\/\/ Step 2: Assign values to boolean primitives created in Step 1\r\nboolean1= true;\r\nboolean2= false;\u00a0\r\n\r\n\/\/ Step 3: Assign String values of boolean primitives using toString() method\r\nString string1= Boolean.<em>toString<\/em>(boolean1);\r\nString string2= Boolean.<em>toString<\/em>(boolean2);\r\n\u00a0\r\nSystem.<em>out<\/em>.println( \" String value of boolean primitive boolean1 is\u00a0 \" +string1 );\r\n\r\nSystem.<em>out<\/em>.println( \" String value of boolean primitive boolean2 is\u00a0 \" +string2 );\r\n}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>String value of boolean primitive boolean1 is\u00a0 true\r\nString value of boolean primitive boolean2 is\u00a0 false<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, boolean primitive types are created.<\/li>\n<li>In Step 2, Values are assigned to primitive types created in Step 1.<\/li>\n<li>In Step 3, String values of boolean primitives are assigned to String objects using toString() method.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>9. static Boolean valueOf(boolean b)<\/strong><\/span><\/p>\n<p>This method returns an object of Boolean class that represents the specified boolean value.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: create Boolean objects\r\nBoolean boolean1, boolean2;\u00a0\r\n\r\n\/\/ Step 2: create boolean primitives and assign values to them\r\nboolean boolPrimitive1 = true;\r\nboolean boolPrimitive2 = false;\u00a0\r\n\r\n\/\/ Step 3: Assigning values of primitive types to Boolean objects\r\nboolean1 = Boolean.<em>valueOf<\/em>(boolPrimitive1);\r\nboolean2 = Boolean.<em>valueOf<\/em>(boolPrimitive2);\u00a0\r\n\r\nSystem.<em>out<\/em>.println( \" Boolean instance of primitive type boolPrimitive1 is : \" + boolean1 );\r\nSystem.<em>out<\/em>.println( \" Boolean instance of primitive type boolPrimitive2 is : \" + boolean2\u00a0 );\r\n}\r\n}\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">Output:<\/span><\/strong><\/p>\n<pre>Boolean instance of primitive type boolPrimitive1 is : true\r\nBoolean instance of primitive type boolPrimitive2 is : false<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean objects are created.<\/li>\n<li>In Step 2, boolean primitives are created and values are assigned to them<\/li>\n<li>In Step 3, Values of primitive types are assigned to Boolean objects<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>10. static Boolean valueOf(String s)<\/strong><\/span><\/p>\n<p>This method returns Boolean object with a value true or false<\/p>\n<p><strong>true<\/strong> when the value of string is \u201ctrue\u201d (ignoring case)<\/p>\n<p><strong>false<\/strong> when the value is any string other than \u201ctrue\u201d.<\/p>\n<p>Let us discuss this method with the help of example as shown below<\/p>\n<pre>public class BooleanMethods {\r\npublic static void main(String[] args) {\u00a0\r\n\r\n\/\/ Step 1: Create Boolean objects\r\nBoolean boolean1, boolean2, boolean3, boolean4;\u00a0\r\n\r\n\/\/ Step 2: Create\u00a0 String's and assign values to them\r\nString string1 = \"abc\";\r\nString string2 = \"false\";\r\nString string3= null;\r\nString string4= \"TrUe\";\r\n\r\n\/\/Step 3: Assigning boolean value of Strings to Boolean objects\r\nboolean1 = Boolean.<em>valueOf<\/em>(string1);\r\nboolean2 = Boolean.<em>valueOf<\/em>(string2);\r\nboolean3 = Boolean.<em>valueOf<\/em>(string3);\r\nboolean4 = Boolean.<em>valueOf<\/em>(string4);\u00a0\r\n\r\nSystem.<em>out<\/em>.println( \" Boolean value of String1 is : \"\u00a0 + boolean1 );\r\nSystem.<em>out<\/em>.println( \" Boolean value of String2 is : \"\u00a0 + boolean2 );\r\nSystem.<em>out<\/em>.println( \" Boolean value of String3 is : \"\u00a0 + boolean3 );\r\nSystem.<em>out<\/em>.println( \" Boolean value of String4 is : \"\u00a0 + boolean4 );\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Boolean value of String1 is : false\r\nBoolean value of String2 is : false\r\nBoolean value of String3 is : false\r\nBoolean value of String4 is : true\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>Above program has been divided into 3 steps that we will discuss one by one<\/p>\n<ul>\n<li>In Step 1, Boolean objects are created.<\/li>\n<li>In Step 2, String objects are created and values are assigned to them<\/li>\n<li>In Step 3, boolean value of Strings are assigned to Boolean objects<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Boolean Class is a wrapper class that wraps the primitive type boolean. This class provides mechanism to convert primitive type \u201cboolean\u201d to object type \u201cBoolean\u201d or vice versa. Object of Class Boolean can contain only single value whose type is boolean. This class extends Object class and implements Serializable and Comparable interfaces. Boolean Class Constructors\u00a0 &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/boolean.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Boolean Class in JAVA with Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79],"tags":[],"class_list":["post-1405","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-basics"],"psp_head":"<title>Boolean Class in JAVA with Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Boolean Class is a wrapper class that wraps the primitive type boolean. This class provides mechanism to convert primitive type \u201cboolean\u201d to object type \u201cBoolean\u201d or vice versa.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/boolean.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=1405"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1405\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}