{"id":1316,"date":"2022-02-27T18:05:48","date_gmt":"2022-02-27T18:05:48","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1316"},"modified":"2022-02-27T18:05:48","modified_gmt":"2022-02-27T18:05:48","slug":"wrapper-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/wrapper-class\/","title":{"rendered":"Wrapper Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java Wrapper class with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The wrapper classes in Java are used to convert primitive types (<code>int<\/code>,&nbsp;<code>char<\/code>,&nbsp;<code>float<\/code>, etc) into corresponding objects.<\/p>\n\n\n\n<p>Each of the 8 primitive types has corresponding wrapper classes.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Primitive Type<\/th><th>Wrapper Class<\/th><\/tr><tr><td><code>byte<\/code><\/td><td><code>Byte<\/code><\/td><\/tr><tr><td><code>boolean<\/code><\/td><td><code>Boolean<\/code><\/td><\/tr><tr><td><code>char<\/code><\/td><td><code>Character<\/code><\/td><\/tr><tr><td><code>double<\/code><\/td><td><code>Double<\/code><\/td><\/tr><tr><td><code>float<\/code><\/td><td><code>Float<\/code><\/td><\/tr><tr><td><code>int<\/code><\/td><td><code>Integer<\/code><\/td><\/tr><tr><td><code>long<\/code><\/td><td><code>Long<\/code><\/td><\/tr><tr><td><code>short<\/code><\/td><td><code>Short<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"primitive-types-to-wrapper-objects\">Convert Primitive Type to Wrapper Objects<\/h2>\n\n\n\n<p>We can also use the&nbsp;<code>valueOf()<\/code>&nbsp;method to convert primitive types into corresponding objects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Primitive Types to Wrapper Objects<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create primitive types\n    int a = 5;\n    double b = 5.65;\n\n    \/\/converts into wrapper objects\n    Integer aObj = Integer.valueOf(a);\n    Double bObj = Double.valueOf(b);\n\n    if(aObj instanceof Integer) {\n      System.out.println(\"An object of Integer is created.\");\n    }\n\n    if(bObj instanceof Double) {\n      System.out.println(\"An object of Double is created.\");\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>An object of Integer is created.\nAn object of Double is created.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<code>valueOf()<\/code>&nbsp;method to convert the primitive types into objects.<\/p>\n\n\n\n<p>Here, we have used the&nbsp;<code>instanceof<\/code>&nbsp;operator to check whether the generated objects are of&nbsp;<code>Integer<\/code>&nbsp;or&nbsp;<code>Double<\/code>&nbsp;type or not.<\/p>\n\n\n\n<p>However, the Java compiler can directly convert the primitive types into corresponding objects. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 5;\n\/\/ converts into object\nInteger aObj = a;\n\ndouble b = 5.6;\n\/\/ converts into object\nDouble bObj = b;\n<\/code><\/pre>\n\n\n\n<p>This process is known as\u00a0<strong>auto-boxing<\/strong>.<\/p>\n\n\n\n<p><strong>Note<\/strong>: We can also convert primitive types into wrapper objects using&nbsp;<code>Wrapper<\/code>&nbsp;class constructors. But the use of constructors is discarded after Java 9.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wrapper-objects-to-primitive-types\">Wrapper Objects into Primitive Types<\/h2>\n\n\n\n<p>To convert objects into the primitive types, we can use the corresponding value methods (<code>intValue()<\/code>,&nbsp;<code>doubleValue()<\/code>, etc) present in each wrapper class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Wrapper Objects into Primitive Types<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ creates objects of wrapper class\n    Integer aObj = Integer.valueOf(23);\n    Double bObj = Double.valueOf(5.55);\n\n    \/\/ converts into primitive types\n    int a = aObj.intValue();\n    double b = bObj.doubleValue();\n\n    System.out.println(\"The value of a: \" + a);\n    System.out.println(\"The value of b: \" + b);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The value of a: 23\nThe value of b: 5.55\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<code>intValue()<\/code>&nbsp;and&nbsp;<code>doubleValue()<\/code>&nbsp;method to convert the&nbsp;<code>Integer<\/code>&nbsp;and&nbsp;<code>Double<\/code>&nbsp;objects into corresponding primitive types.<\/p>\n\n\n\n<p>However, the Java compiler can automatically convert objects into corresponding primitive types. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer aObj = Integer.valueOf(2);\n\/\/ converts into int type\nint a = aObj;\n\nDouble bObj = Double.valueOf(5.55);\n\/\/ converts into double type\ndouble b = bObj;\n<\/code><\/pre>\n\n\n\n<p>This process is known as\u00a0<strong>unboxing<\/strong>. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages\">Advantages of Wrapper Classes<\/h2>\n\n\n\n<ul><li>In Java, sometimes we might need to use objects instead of primitive data types. For example, while working with collections.<code>\/\/ error ArrayList&lt;int&gt; list = new ArrayList&lt;&gt;(); \/\/ runs perfectly ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();<\/code>In such cases, wrapper classes help us to use primitive data types as objects.<\/li><li>We can store the null value in wrapper objects. For example,<code> \/\/ generates an error int a = null; \/\/ runs perfectly Integer a = null;<\/code><\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: Primitive types are more efficient than corresponding objects. Hence, when efficiency is the requirement, it is always recommended primitive types.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java Wrapper class with the help of examples. The wrapper classes in Java are used to convert primitive types (int,&nbsp;char,&nbsp;float, etc) into corresponding objects. Each of the 8 primitive types has corresponding wrapper classes. Primitive Type Wrapper Class byte Byte boolean Boolean char Character double Double float [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[597],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1316"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1316"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1316\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}