

{"id":13278,"date":"2018-04-12T10:34:44","date_gmt":"2018-04-12T10:34:44","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13278"},"modified":"2026-05-22T16:57:34","modified_gmt":"2026-05-22T11:27:34","slug":"autoboxing-and-unboxing","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/","title":{"rendered":"What is Autoboxing and Unboxing in Java with Example Program"},"content":{"rendered":"<p>In this Java tutorial, we will learn about autoboxing in Java and unboxing in Java. So let&#8217;s start!!!!<\/p>\n<h3>Autoboxing in Java<\/h3>\n<p>Autoboxing is the process of converting a primitive data type to a Wrapper class object automatically by the compiler.<\/p>\n<p>Here, the compiler checks whether the method expects an object as a parameter. Then it automatically converts the required primitive data type to a Wrapper class object.<\/p>\n<p>Why do we need this at all? Well, this boils down to the fact that we need some special methods to be performed on datatypes that are primitive in nature.<\/p>\n<p>This, in turn, results in us having classes, namely wrapper classes. These, in turn, result in the need for conversion of primitive data types to and from wrapper classes.<\/p>\n<p>Now programmers do have a lot on their minds, so they do not want to be bothered by these kinds of methods. This is where the Java compiler comes in to the rescue. The Java compiler automatically converts the primitive data types to their respective Wrapper class objects.<\/p>\n<p>For example, int becomes Integer, float becomes Float, double becomes Double, and so on\u2026<\/p>\n<p>Simple right?<\/p>\n<p>Let us make some code!<\/p>\n<p><strong>Java program to illustrate the use of Autoboxing in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.autoboxunbox;\r\nimport java.util.*;\r\n\r\npublic class AutoBox\r\n{\r\npublic static void main(String[] args) {\r\n        \/\/We are going to declare a Stack which can only store Integer values in it. \r\n        Stack&lt;Integer&gt;stack = new Stack&lt;&gt;();\r\n        int a=6;\r\n        int b=12;\r\n        stack.push(a);\r\n        stack.push(b);\r\n        System.out.println(stack.pop());\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">12<\/div>\n<p>So the main focus of this is to perform a stack operation that could access only Integer elements, as we already see here.<\/p>\n<p>But why didn\u2019t the compiler throw an error even though we specifically asked it to push primitive data types in the stack?<\/p>\n<p>Well, this is because of autoboxing, the compiler automatically converts the primitive datatype to its respective wrapper class in the example above.<\/p>\n<h3>Unboxing in Java<\/h3>\n<p>Unboxing, as the name suggests, is the complete inverse of autoboxing.<\/p>\n<p>During unboxing, the compiler converts the wrapper class object to its respective primitive type. This essentially means that the compiler basically juggles objects and their respective data types and uses whichever one suits the needs.<\/p>\n<p>Let us take an example, in fact, let us take the same example as above.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.autoboxunbox;\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    public int add(Integer a, Integer b)\r\n{\r\n    return a+b;\r\n    \r\n}\r\n    public static void main(String[] args) {\r\n        \/\/We are going to declare a Stack which can only store Integer values in it. \r\n        Stack&lt;Integer&gt;stack = new Stack&lt;&gt;();\r\n        int a=6;\r\n        int b=12;\r\n        stack.push(a);\r\n        stack.push(b);\r\n        System.out.println(new Main().add(stack.pop(),stack.pop()));\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">18<\/div>\n<p>So what exactly happened there? How is it so that when we pass two Integer values to the function sum, it does not throw an error while trying to add them, but instead, it adds them and returns an int value as specified in the function as before?<\/p>\n<p>Well, this is the magic of the compiler again. It understands that the program needs to add two numbers of type Integer which is not possible.<\/p>\n<p>Hence, it converts these values to the required primitive values and returns an int value instead of an Integer object.<\/p>\n<h4>Advantages of Java Autoboxing and Unboxing<\/h4>\n<ul>\n<li>Developers have one less thing to worry about.<\/li>\n<li>Helps in writing cleaner code.<\/li>\n<li>Prevents unnecessary typecasting.<\/li>\n<li>A null operator is autoboxed\/unboxed to null itself.<\/li>\n<li>They help in transforming the primitive data into an object by performing autoboxing.<\/li>\n<li>Autoboxing breaks the restrictions of primitive data types by converting them to objects so they can use OOPs principles.<\/li>\n<li>Reduces the effort of type casting.<\/li>\n<li>Improve performance by merging the Java collections and the data type to enhance the API integration.<\/li>\n<\/ul>\n<h3>Autoboxing with Method Overloading in Java<\/h3>\n<p>When we plan to use autoboxing and unboxing in unison in Java, there are a few rules that we need to keep in mind before we decide to progress with our programs.<\/p>\n<p><strong>1. Boxing gets a lower preference when compared to Widening<\/strong><\/p>\n<p>When the function overloading gets a parameter that has upper type-casting(widening) involved, the autoboxing is prevented and the widening function gets called.<\/p>\n<p>An example would be<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.autoboxunbox;\r\npublic class Main\r\n{\r\n    public static void conv(Float f)\r\n    {\r\n        System.out.println(\"The value got converted to its wrapper class.\");\r\n    }\r\n    public static void conv(float f)\r\n    {\r\n        System.out.println(\"The value got converted to a wider primitive datatype\");\r\n    }\r\n    public static void main(String[] args) {\r\n        int a=9;\r\n        conv(a);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The value got converted to a wider primitive datatype<\/div>\n<p><strong>2. Boxing is preferred over var args<\/strong><\/p>\n<p>The compiler prefers boxing and unboxing over varargs. Hence, it binds the method call to perform boxing.<\/p>\n<p>A simple example would be<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.autoboxunbox;\r\npublic class Main\r\n{\r\n    public static void conv(Float f)\r\n    {\r\n        System.out.println(\"The function with the single parameter got executed\");\r\n    }\r\n    public static void conv(Float... f)\r\n    {\r\n        System.out.println(\"The function with variable arguments got executed\");\r\n    }\r\n    public static void main(String[] args) {\r\n        float a=9;\r\n        conv(a);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The function with the single parameter got executed<\/div>\n<p><strong>3. Widening beats varargs<\/strong><\/p>\n<p>The compiler prefers widening of variables in contrast to varargs. Hence, it performs widening instead of taking the parameters as a variable argument.<\/p>\n<p>A simple example would be<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.autoboxunbox;\r\n\r\n\r\npublic class Main\r\n{\r\n    public static void conv(float f)\r\n    {\r\n        System.out.println(\"The function with widened parameter got executed\");\r\n    }\r\n    public static void conv(Float... f)\r\n    {\r\n        System.out.println(\"The function with variable arguments got executed\");\r\n    }\r\n    public static void main(String[] args) {\r\n        int a=9;\r\n        conv(a);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The function with a widened parameter got executed<\/div>\n<p>While autoboxing and unboxing offer advantages, it&#8217;s important to be aware of some potential considerations. Autoboxing can occur implicitly, so it&#8217;s essential to understand when it happens to avoid surprises in your code&#8217;s behavior.<\/p>\n<p>Additionally, unboxing primitive data types from wrapper objects can incur a slight performance overhead compared to working directly with primitives. If performance is a critical concern, especially in tight loops or memory-constrained environments, you might consider using primitive data types explicitly. However, in most cases, the convenience and readability benefits of autoboxing and unboxing outweigh the minor performance impact.<\/p>\n<h3>Summary<\/h3>\n<p>Hence, in this article, we came to know about Autoboxing and Unboxing in Java.<\/p>\n<p>This is a very important concept to know because it can help developers in debugging special programs where the type of the variable or an object is key.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Java tutorial, we will learn about autoboxing in Java and unboxing in Java. So let&#8217;s start!!!! Autoboxing in Java Autoboxing is the process of converting a primitive data type to a Wrapper&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":85692,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[318,1254,1256,4289,7394,7731],"class_list":["post-13278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-advantages-of-autoboxing-and-unboxing-in-java","tag-autoboxing-and-unboxing-in-java-with-examples","tag-autoboxing-in-java-with-examples","tag-example-of-java-autoboxing","tag-java-autoboxing-and-unboxing","tag-java-unboxing-with-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Autoboxing and Unboxing in Java with Example Program - DataFlair<\/title>\n<meta name=\"description\" content=\"Java Autoboxing and Unboxing Tutorial- Learn what is Autoboxing in Java, What is Java Unboxing with their examples &amp; Advantages.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Autoboxing and Unboxing in Java with Example Program - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Java Autoboxing and Unboxing Tutorial- Learn what is Autoboxing in Java, What is Java Unboxing with their examples &amp; Advantages.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-12T10:34:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T11:27:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Autoboxing and Unboxing in Java with Example Program - DataFlair","description":"Java Autoboxing and Unboxing Tutorial- Learn what is Autoboxing in Java, What is Java Unboxing with their examples & Advantages.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/","og_locale":"en_US","og_type":"article","og_title":"What is Autoboxing and Unboxing in Java with Example Program - DataFlair","og_description":"Java Autoboxing and Unboxing Tutorial- Learn what is Autoboxing in Java, What is Java Unboxing with their examples & Advantages.","og_url":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-12T10:34:44+00:00","article_modified_time":"2026-05-22T11:27:34+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"What is Autoboxing and Unboxing in Java with Example Program","datePublished":"2018-04-12T10:34:44+00:00","dateModified":"2026-05-22T11:27:34+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/"},"wordCount":829,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg","keywords":["Advantages of Autoboxing and Unboxing in Java","Autoboxing and Unboxing in java with examples","Autoboxing in java with examples","example of Java AUtoboxing","Java Autoboxing and Unboxing","Java Unboxing with example"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/","url":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/","name":"What is Autoboxing and Unboxing in Java with Example Program - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg","datePublished":"2018-04-12T10:34:44+00:00","dateModified":"2026-05-22T11:27:34+00:00","description":"Java Autoboxing and Unboxing Tutorial- Learn what is Autoboxing in Java, What is Java Unboxing with their examples & Advantages.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Autoboxing-and-Unboxing-in-Java.jpg","width":1200,"height":628,"caption":"Autoboxing and Unboxing in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/autoboxing-and-unboxing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"What is Autoboxing and Unboxing in Java with Example Program"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13278"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13278\/revisions"}],"predecessor-version":[{"id":148410,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13278\/revisions\/148410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/85692"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}