{"id":1308,"date":"2022-02-27T06:31:52","date_gmt":"2022-02-27T06:31:52","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1308"},"modified":"2022-02-27T06:31:52","modified_gmt":"2022-02-27T06:31:52","slug":"auto-boxing-and-unboxing","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/auto-boxing-and-unboxing\/","title":{"rendered":"Auto boxing and unboxing"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java autoboxing and unboxing with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"autoboxing\">Java Autoboxing &#8211; Primitive Type to Wrapper Object<\/h2>\n\n\n\n<p>In&nbsp;<strong>autoboxing<\/strong>, the Java compiler automatically converts primitive types into their corresponding wrapper class objects. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 56;\n\n\/\/ autoboxing\nInteger aObj = a;\n<\/code><\/pre>\n\n\n\n<p><strong>Autoboxing<\/strong>&nbsp;has a great advantage while working with Java collections.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\">Example 1: Java Autoboxing<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n   public static void main(String&#91;] args) {\n\n      ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n\n      \/\/autoboxing\n      list.add(5);\n      list.add(6);\n\n      System.out.println(\"ArrayList: \" + list);\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>ArrayList: &#91;5, 6]\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an array list of Integer type. Hence the array list can only hold objects of&nbsp;<code>Integer<\/code>&nbsp;type.<\/p>\n\n\n\n<p>Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.add(5);\n<\/code><\/pre>\n\n\n\n<p>Here, we are passing primitive type value. However, due to&nbsp;<strong>autoboxing<\/strong>, the primitive value is automatically converted into an&nbsp;<code>Integer<\/code>&nbsp;object and stored in the array list.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"unboxing\">Java Unboxing &#8211; Wrapper Objects to Primitive Types<\/h2>\n\n\n\n<p>In&nbsp;<strong>unboxing<\/strong>, the Java compiler automatically converts wrapper class objects into their corresponding primitive types. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ autoboxing\nInteger aObj = 56;\n\n\/\/ unboxing\nint a = aObj;\n<\/code><\/pre>\n\n\n\n<p>Like&nbsp;<strong>autoboxing<\/strong>,&nbsp;<strong>unboxing<\/strong>&nbsp;can also be used with Java collections.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\">Example 2: Java Unboxing<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n   public static void main(String&#91;] args) {\n\n      ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n\n      \/\/autoboxing\n      list.add(5);\n      list.add(6);\n\n      System.out.println(\"ArrayList: \" + list);\n\n      \/\/ unboxing\n      int a = list.get(0);\n      System.out.println(\"Value at index 0: \" + a);\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>ArrayList: &#91;5, 6]\nValue at index 0: 5\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = list.get(0);\n<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>get()<\/code>&nbsp;method returns the object at index&nbsp;<var>0<\/var>. However, due to&nbsp;<strong>unboxing<\/strong>, the object is automatically converted into the primitive type&nbsp;<code>int<\/code>&nbsp;and assigned to the variable&nbsp;<var>a<\/var>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java autoboxing and unboxing with the help of examples. Java Autoboxing &#8211; Primitive Type to Wrapper Object In&nbsp;autoboxing, the Java compiler automatically converts primitive types into their corresponding wrapper class objects. For example, Autoboxing&nbsp;has a great advantage while working with Java collections. Example 1: Java Autoboxing Output In [&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\/1308"}],"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=1308"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1308\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}