{"id":17037,"date":"2016-11-24T20:29:38","date_gmt":"2016-11-24T20:29:38","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=17037"},"modified":"2024-05-10T17:50:02","modified_gmt":"2024-05-10T12:20:02","slug":"ocajp-wrapper-classes","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/","title":{"rendered":"OCAJP &#8211; Wrapper Classes in Java"},"content":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;Develop code that uses wrapper classes such as Boolean, Double, and Integer&#8221;. In the exam you will be mainly tested about Autoboxing and Unboxing concepts of Wrapper classes, Using wrapper classes with their corresponding primitive types.<\/p>\n<ul>\n<li><strong><a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-free-test\/\">Try our free test for OCAJP 8 exam now!!<\/a><\/strong><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/05\/free_test.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-5780\" src=\"https:\/\/www.whizlabs.com\/blog\/wp-content\/uploads\/2024\/05\/free_test.jpg\" alt=\"OCPJP 6 Free Test\" width=\"700\" height=\"300\" \/><\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#What_is_Wrapper_Classes\" >What is Wrapper Classes?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#When_to_use_Wrapper_classes\" >When to use Wrapper classes?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Integer\" >Integer<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Creating_Integer_Object\" >Creating Integer Object<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Important_Integer_methods\" >Important Integer methods<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Autoboxing_and_unboxing\" >Autoboxing and unboxing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Double\" >Double<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Creating_Double_Object\" >Creating Double Object<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Important_Double_methods\" >Important Double methods<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Autoboxing_and_unboxing-2\" >Autoboxing and unboxing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Boolean\" >Boolean<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Creating_Boolean_Object\" >Creating Boolean Object<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-wrapper-classes\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Wrapper_Classes\"><\/span>What is Wrapper Classes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Generally when we work with numbers we use primitive data types such as byte, int, long, double, etc.<\/li>\n<li>There are certain situations to use objects in place of primitives and the Java platform provides wrapper classes for each of the primitive data types.<\/li>\n<li>These classes &#8220;wrap&#8221; the primitive data type value in an object. Hence the name &#8220;Wrapper Classes&#8221;.<\/li>\n<li>All wrapper classes are immutable classes.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"When_to_use_Wrapper_classes\"><\/span>When to use Wrapper classes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li>When you are working collections to store numeric values. Because collection objects can&#8217;t store primitive values.<\/li>\n<li>To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.<\/li>\n<li>To convert String representation of primitive value into primitive data type.<\/li>\n<\/ol>\n<ul>\n<li>In Java, For every primitive data type there is corresponding wrapper class.<\/li>\n<li>As shown in the above image, Number is the super class for every numeric classes such as Byte, Short,Integer,Long,Float,Double.<\/li>\n<li>Number contain four important abstract methods which are implemented by its sub classes.<\/li>\n<li>The below are the declarations of four abstract methods in Number class(abstract class) which are used to convert to primitive value.<\/li>\n<\/ul>\n<pre>public abstract int intValue()\r\npublic abstract long longValue()\r\npublic abstract float floatValue()\r\npublic abstract double doubleValue()\r\n<\/pre>\n<ul>\n<li>You don&#8217;t need to learn all classes for the exam.<\/li>\n<li>Integer, Double , Boolean classes are on the exam.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Integer\"><\/span>Integer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are the some important points about the Integer class:<\/p>\n<ul>\n<li>Integer class is in java.lang package since Java 1.0 version.<\/li>\n<li>Number class is the super class of Integer.<\/li>\n<li>It implements Comparable,Serializable interfaces.<\/li>\n<li>It is a immutable class.<\/li>\n<li>The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.<\/li>\n<li>In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Creating_Integer_Object\"><\/span>Creating Integer Object<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Integer class has two constructors.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Integer(int value)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It constructs a new Integer object that represents the specified int value.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Integer(String s)throws NumberFormatException<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It constructs a new Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10.<br \/>\nIt throws NumberFormatException if the String does not contain a parsable integer.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>You can also create Integer object without constructor.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class IntegerDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tInteger n1 = new Integer(\"5\");\r\n\t\tInteger n2 = new Integer(2);\r\n\t\tInteger n3 = 4;\r\n\t\t\/\/ Integer n4 = new Integer(\"A\"); this statement generates NumberFormatException.\r\n\t\tSystem.out.println(n1 + \" \" + n2 + \" \" + n3);\/\/prints 5 2 4\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>In the above program, you are storing primitive int value into Integer objects, it is called Boxing.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Important_Integer_methods\"><\/span>Important Integer methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h4>parseInt<\/h4>\n<p><strong>public static int parseInt(String s) throws NumberFormatException<\/strong><\/p>\n<ul>\n<li>This method converts String representation of primitive integer value into primitive integer value .<\/li>\n<li>It throws NumberFormatException if the String does not contain a parsable integer.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>ublic class IntegerDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tint n1 = Integer.parseInt(\"8\");\r\n\t\t\/\/ int n2 = Integer.parseInt(\"OCA\"); This generates NumberFormatException.\r\n\r\n\t\tSystem.out.println(n1);\/\/prints 8\r\n\r\n\t}\r\n}\r\n<\/pre>\n<h4>toBinaryString, toOctalString, toHexString<\/h4>\n<p><strong>public static String toHexString(int i)<\/strong><br \/>\nIt returns a string representation of the integer argument as an unsigned integer in base 16.<\/p>\n<p><strong>public static String toOctalString(int i)<\/strong><br \/>\nIt returns a string representation of the integer argument as an unsigned integer in base 8.<\/p>\n<p><strong>public static String toBinaryString(int i)<\/strong><br \/>\nIt returns a string representation of the integer argument as an unsigned integer in base 2.<br \/>\n<span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class IntegerDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tString s1 = Integer.toHexString(8);\r\n\t\tString s2 = Integer.toOctalString(8);\r\n\t\tString s3 = Integer.toBinaryString(8);\r\n\t\tSystem.out.println(s1 + \" \" + s2 + \" \" + s3); \/\/ prints 8 10 1000\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h4>valueOf,byteValue, doubleValue, floatValue,intValue,longValue<\/h4>\n<p><strong>public static Integer valueOf(int i)<\/strong><br \/>\nIt returns an Integer instance representing the specified int value.<br \/>\n<strong>public byte byteValue()<\/strong><br \/>\nIt returns the value of this Integer as a byte after a narrowing primitive conversion.<br \/>\n<strong>public double doubleValue()<\/strong><br \/>\nIt returns the value of this Integer as a double after a widening primitive conversion.<br \/>\n<strong>public float floatValue()<\/strong><br \/>\nIt returns the value of this Integer as a float after a widening primitive conversion.<br \/>\n<strong>public int intValue()<\/strong><br \/>\nIt returns the value of this Integer as an int.<br \/>\n<strong>public long longValue()<\/strong><br \/>\nIt returns the value of this Integer as a long after a widening primitive conversion.<br \/>\n<span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class IntegerDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tInteger in = Integer.valueOf(8);\r\n\t\tbyte b = in.byteValue();\r\n\t\tint i = in.intValue();\r\n\t\tlong l = in.longValue();\r\n\t\tfloat f = in.floatValue();\r\n\t\tdouble d = in.doubleValue();\r\n\t\tSystem.out.println(in+\" \"+b+\" \"+i+\" \"+l+\" \"+f+\" \"+d);\/\/It prints 8 8 8 8 8.0 8.0\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Autoboxing_and_unboxing\"><\/span>Autoboxing and unboxing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\t\tList&lt;Integer&gt; li = new ArrayList&lt;Integer&gt;();\r\n\t\tli.add(8);\/\/ line 1\r\n\t\tli.add(new Integer(5));\/\/ line 2\r\n\t\tint n = li.get(1);\/\/ line 3\r\n<\/pre>\n<ul>\n<li>At line1 you just typed 8 primitive value java will create Integer object and stores in ArrayList object, this process is called Autoboxing.<\/li>\n<li>At line2 you are creating the object. No Autoboxing here.<\/li>\n<li>At line3 get method returns Integer object and that is converted into primitive int value when it is assigning to int variable,this process is called Auto unboxing.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Double\"><\/span>Double<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are the some important points about the Double class:<\/p>\n<ul>\n<li>Double class is in java.lang package since Java 1.0 version.<\/li>\n<li>Number class is the super class of Double.<\/li>\n<li>It implements Comparable,Serializable interfaces.<\/li>\n<li>It is a immutable class.<\/li>\n<li>The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.<\/li>\n<li>In addition, this class provides several methods for converting an double to a String and a String to an double, as well as other constants and methods useful when dealing with an double.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Creating_Double_Object\"><\/span>Creating Double Object<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Double class has two constructors.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Double(double value)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It constructs a new Double object that represents the specified double value.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Double(String s)throws NumberFormatException<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It constructs a new Double object that represents the double value indicated by the String parameter. The string is converted to an double value in exactly the manner used by the parseInt method for radix 10.<br \/>\nIt throws NumberFormatException if the String does not contain a parsable number.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>You can also create Double object without constructor.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class DoubleDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tDouble n1 = new Double(\"5.0\");\r\n\t\tDouble n2 = new Double(2.0);\r\n\t\tDouble n3 = 4.0;\r\n\t\t\/\/ Double n4 = new Double(\"A\"); this statement generates NumberFormatException.\r\n\t\tSystem.out.println(n1 + \" \" + n2 + \" \" + n3);\/\/prints 5.0 2.0 4.0\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>In the above program, you are storing primitive double value into Double objects, it is called Boxing.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Important_Double_methods\"><\/span>Important Double methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h4>parseDouble<\/h4>\n<p><strong>public static double parseDouble(String s) throws NumberFormatException<\/strong><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>This method converts String representation of number value into primitive double value.<\/li>\n<li>It throws NumberFormatException if the String does not contain a parsable number.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class DoubleDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tdouble n1 = Double.parseDouble(\"8.0\");\r\n\t\tdouble n2 = Double.parseDouble(\"2\");\r\n\t\t\/\/ double n3 = Double.parseDouble(\"OCA\"); This generates NumberFormatException.\r\n\r\n\t\tSystem.out.println(n1+\" \"+n2);\/\/prints 8.0 2.0\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h4>valueOf,byteValue, doubleValue, floatValue,intValue,longValue<\/h4>\n<p><strong>public static Double valueOf(double i)<\/strong><br \/>\nIt returns an Double instance representing the specified double value.<br \/>\n<strong>public byte byteValue()<\/strong><br \/>\nIt returns the value of this Double as a byte after a narrowing primitive conversion.<br \/>\n<strong>public double doubleValue()<\/strong><br \/>\nIt returns the value of this Double as a double after a widening primitive conversion.<br \/>\n<strong>public float floatValue()<\/strong><br \/>\nIt returns the value of this Double as a float after a widening primitive conversion.<br \/>\n<strong>public int intValue()<\/strong><br \/>\nIt returns the value of this Double as an int.<br \/>\n<strong>public long longValue()<\/strong><br \/>\nIt returns the value of this Double as a long after a widening primitive conversion.<br \/>\n<span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class DoubleDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tDouble in = Double.valueOf(8);\r\n\t\tbyte b = in.byteValue();\r\n\t\tint i = in.intValue();\r\n\t\tlong l = in.longValue();\r\n\t\tfloat f = in.floatValue();\r\n\t\tdouble d = in.doubleValue();\r\n\t\tSystem.out.println(in+\" \"+b+\" \"+i+\" \"+l+\" \"+f+\" \"+d);\/\/It prints 8.0 8 8 8 8.0 8.0\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Autoboxing_and_unboxing-2\"><\/span>Autoboxing and unboxing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre>\t\tList&lt;Double&gt; li = new ArrayList&lt;Double&gt;();\r\n\t\tli.add(8.0);\/\/ line 1\r\n\t\tli.add(new Double(5));\/\/ line 2\r\n\t\tdouble n = li.get(1);\/\/ line 3\r\n<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>At line1 you just typed 8 primitive value java will create Double object and stores in ArrayList object, this process is called Autoboxing.<\/li>\n<li>At line2 you are creating the object. No Autoboxing here.<\/li>\n<li>At line3 get method returns Double object and that is converted into primitive double value when it is assigning to double variable,this process is called Auto unboxing.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Boolean\"><\/span>Boolean<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are the some important points about the Boolean class:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Boolean class is in java.lang package since Java 1.0 version.<\/li>\n<li>Object class is the super class of Boolean.<\/li>\n<li>It implements Comparable,Serializable interfaces.<\/li>\n<li>It is a immutable class.<\/li>\n<li>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.<\/li>\n<li>In addition, this class provides several methods for converting an boolean to a String and a String to an boolean, as well as other constants and methods useful when dealing with a boolean.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Creating_Boolean_Object\"><\/span>Creating Boolean Object<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Boolean class has two constructors.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Boolean(boolean value)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It creates a Boolean object representing the value argument.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>public Boolean(String s)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It creates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string &#8220;true&#8221;. Otherwise, allocate a Boolean object representing the value false.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>You can create Boolean using predefined fields in the Boolean class.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class DoubleDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tBoolean b1 = new Boolean(true);\r\n\t\tBoolean b2 = new Boolean(\"true\");\r\n\t\tBoolean b3 = new Boolean(\"false\");\r\n\t\tBoolean b4 = new Boolean(\"oca\");\r\n\t\tBoolean b5 = new Boolean(\"null\");\r\n\t\tBoolean b6 =Boolean.TRUE;\r\n\t\tBoolean b7 = Boolean.FALSE;\r\n\t\tSystem.out.println(b1+\" \"+b2+\" \"+b3+\" \"+b4+\" \"+b5+\" \"+b6+\" \"+b7);\/\/prints true true false false false true false\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h4>parseBoolean,valueOf<\/h4>\n<p><strong>public static boolean parseBoolean(String s)<\/strong><br \/>\nIt 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 &#8220;true&#8221;.<br \/>\n<strong>public static Boolean valueOf(boolean b)<\/strong><br \/>\nIt returns a Boolean instance representing the specified boolean value.<br \/>\n<strong>public boolean booleanValue()<\/strong><br \/>\nIt returns the value of this Boolean object as a boolean primitive.<br \/>\n<span style=\"text-decoration: underline;\"><strong>Example :<\/strong><\/span><\/p>\n<pre>public class DoubleDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tboolean b1 = Boolean.parseBoolean(\"true\");\r\n\t\tboolean b2 = Boolean.parseBoolean(\"oca\");\r\n\t\tBoolean b3 = Boolean.valueOf(false);\r\n\t\tboolean b4 = b3.booleanValue();\r\n\t\tSystem.out.println(b1 + \" \" + b2 + \" \" + b3 + \" \" + b4);\/\/ It prints true false false false\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For <a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/\" target=\"_blank\" rel=\"noopener\">OCA Java<\/a> Programmer exam concentrate on Autoboxing and Auto unboxing, ParseXXX methods in three classes and possible ways to create Boolean object.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-practice-tests\/\"><strong>Buy our 650+ questions for OCAJP 8 certification exam!!<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;Develop code that uses wrapper classes such as Boolean, Double, and Integer&#8221;. In the exam you will be mainly tested about Autoboxing and Unboxing concepts of Wrapper classes, Using wrapper classes with their corresponding primitive types. Try our free test for OCAJP 8 exam now!! What is Wrapper Classes? Generally when we work with numbers we use primitive data types such as byte, int, long, double, etc. There are certain situations to use objects in place of primitives and the Java platform provides wrapper classes for each of the primitive data types. [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[3971,1140],"class_list":["post-17037","post","type-post","status-publish","format-standard","hentry","category-java","tag-oca-java","tag-ocajp-8-preparation"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":6,"uagb_excerpt":"This post is about the OCAJP exam objective &#8220;Develop code that uses wrapper classes such as Boolean, Double, and Integer&#8221;. In the exam you will be mainly tested about Autoboxing and Unboxing concepts of Wrapper classes, Using wrapper classes with their corresponding primitive types. Try our free test for OCAJP 8 exam now!! What is&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/17037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=17037"}],"version-history":[{"count":2,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/17037\/revisions"}],"predecessor-version":[{"id":95610,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/17037\/revisions\/95610"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=17037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=17037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=17037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}