{"id":415,"date":"2021-01-04T17:57:24","date_gmt":"2021-01-04T17:57:24","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=415"},"modified":"2021-01-04T18:14:25","modified_gmt":"2021-01-04T18:14:25","slug":"wrapper-classes","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/wrapper-classes\/","title":{"rendered":"Wrapper Classes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ed402a667\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ed402a667\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/wrapper-classes\/#Passing_Arguments\" >Passing Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/wrapper-classes\/#Returning_Values\" >Returning Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/wrapper-classes\/#Autoboxing_Unboxing\" >Autoboxing &amp; Unboxing<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Sometimes it is necessary to pass a primitive type when a (reference to an) object is expected.<\/p>\n\n\n\n<p>For example, assume that you have a method that specifies that one of its arguments is of type&nbsp;<code><strong>Object<\/strong><\/code>, and you need to pass it an&nbsp;<code><strong>int<\/strong><\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class PassIntTest\n{\n  public static void handleValue(Object value)\n  {\n    \/\/ Do something with value...\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = 56;\n    handleValue(intValue); \/\/ Error\n  }\n}\n<\/pre><\/div>\n\n\n<p>Prior to JDK Version 5.0, the above code will produce a compile-time error:<\/p>\n\n\n\n<p><code><strong>\"PassIntTest.java\": cannot find symbol; symbol : method valueOf(int), location:<br>&nbsp;&nbsp;&nbsp; class java.lang.Integer at line 13, column 17<br>\"PassIntTest.java\": Fatal Error: Unable to find method valueOf<\/strong><\/code><\/p>\n\n\n\n<p>The solution is to use a&nbsp;<em>Wrapper class<\/em>.&nbsp; For every primitive data type, there is a corresponding wrapper class:<\/p>\n\n\n\n<figure id=\"table2\" class=\"wp-block-table\"><table><thead><tr><th>Primitive Type<\/th><th>Wrapper Class<\/th><\/tr><\/thead><tbody><tr><td><code>boolean<\/code><\/td><td><code>Boolean<\/code><\/td><\/tr><tr><td><code>byte<\/code><\/td><td><code>Byte<\/code><\/td><\/tr><tr><td><code>char<\/code><\/td><td><code>Character<\/code><\/td><\/tr><tr><td><code>short<\/code><\/td><td><code>Short<\/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>float<\/code><\/td><td><code>Float<\/code><\/td><\/tr><tr><td><code>double<\/code><\/td><td><code>Double<\/code><\/td><\/tr><tr><td><code>void<\/code><\/td><td><code>Void<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Each wrapper class has a constructor which accepts a primitive type value.&nbsp; For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Integer intWrapper = new Integer(56);<\/strong><\/pre>\n\n\n\n<p>The resulting instance &#8220;wraps&#8221; the supplied value.<\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&nbsp;Each wrapper class is&nbsp;<em>immutable<\/em>;&nbsp; that is, once the value has been wrapped, it may not be changed.<\/li><li>&nbsp;Each wrapper class is&nbsp;<em>final<\/em>;&nbsp; that is, it may not be extended.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Passing_Arguments\"><\/span>Passing Arguments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you wish to pass an argument of a primitive type to a method that expects a reference to an object, then you wrap the value in its corresponding wrapper class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [13]; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class PassIntTest\n{\n  public static void handleValue(Object value)\n  {\n    \/\/ Do something with value...\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = 56;\n    handleValue( new Integer(intValue) );\n  }\n}\n<\/pre><\/div>\n\n\n<p>The above code will compile, and the instance of class&nbsp;<code>Integer<\/code>&nbsp;is passed in as the object instance&nbsp;<code><strong>value<\/strong><\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em><strong>Note:<\/strong><\/em>&nbsp;All the wrapper classes live in the&nbsp;<code><strong>java.lang<\/strong><\/code>&nbsp;package.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Returning_Values\"><\/span>Returning Values<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another challenge is sometimes encountered when a method returns a reference to an object, and you would like the returned value to be a primitive type.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class ReturnIntTest\n{\n  public static Integer returnValue()\n  {\n    return new Integer(42);\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = returnValue();\n    System.out.println(intValue);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which will result in a compile-time error:<\/p>\n\n\n\n<p><code><strong>\"ReturnIntTest.java\": incompatible types; found :<br>&nbsp; java.lang.Integer, required: int at line 12, column 20<\/strong><\/code><\/p>\n\n\n\n<p>In this case, you can make use of convenience methods provided by the wrapper classes:<\/p>\n\n\n\n<figure id=\"table2\" class=\"wp-block-table\"><table><thead><tr><th>Wrapper Class<\/th><th>Method<\/th><\/tr><\/thead><tbody><tr><td><code>Boolean<\/code><\/td><td><kbd>public boolean&nbsp;<strong>booleanValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Byte<\/code><\/td><td><kbd>public byte&nbsp;<strong>byteValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Character<\/code><\/td><td><kbd>public char&nbsp;<strong>charValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Short<\/code><\/td><td><kbd>public short&nbsp;<strong>shortValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Integer<\/code><\/td><td><kbd>public int&nbsp;<strong>intValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Long<\/code><\/td><td><kbd>public long&nbsp;<strong>longValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Float<\/code><\/td><td><kbd>public float&nbsp;<strong>floatValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Double<\/code><\/td><td><kbd>public double&nbsp;<strong>doubleValue<\/strong>()<\/kbd><\/td><\/tr><tr><td><code>Void<\/code><\/td><td>(not applicable)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [12]; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class ReturnIntTest\n{\n  public static Integer returnValue()\n  {\n    return new Integer(42);\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = returnValue().intValue();\n    System.out.println(intValue);\n  }\n}\n<\/pre><\/div>\n\n\n<p>The above code will compile and run correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Autoboxing_Unboxing\"><\/span>Autoboxing &amp; Unboxing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In Java 5.0, this need for an explicit conversion between a primitive type and its wrapper class has been automated.\u00a0 The Java 5.0 compiler does automatic &#8220;<em>autoboxing<\/em>&#8221; and &#8220;<em>unboxing<\/em>&#8221; without you having to do anything explicitly.<\/p>\n\n\n\n<p>For example, the Java 5.0 compiler will &#8220;<em>autobox<\/em>&#8221; the integer value in the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class PassIntTest\n{\n  public static void handleValue(Object value)\n  {\n    \/\/ Do something with value...\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = 56;\n    handleValue(intValue);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which will compile and run in Java 5.0 and beyond (only).<\/p>\n\n\n\n<p>Similarly, the Java 5.0 compiler will &#8220;unbox&#8221; the integer value in the following code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage example;\n\npublic class ReturnIntTest\n{\n  public static Integer returnValue()\n  {\n    return new Integer(42);\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    int intValue = returnValue();\n    System.out.println(intValue);\n  }\n}\n<\/pre><\/div>\n\n\n<p>It too, will compile and run, but only in Java 5.0 and beyond.<\/p>\n\n\n\n<p>The &#8220;<em>autoboxing<\/em>&#8221; and &#8220;<em>unboxing<\/em>&#8221; that the compiler performs automatically is entirely equivalent to the manual equivalent that you had to do yourself prior to Java 5.0.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is necessary to pass a primitive type when a (reference to an) object is expected. For example, assume that you have a method that specifies that one of its arguments is of type&nbsp;Object, and you need to pass it an&nbsp;int: Prior to JDK Version 5.0, the above code will produce a compile-time error: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":57,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-415","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Sometimes it is necessary to pass a primitive type when a (reference to an) object is expected. For example, assume that you have a method that specifies that one of its arguments is of type&nbsp;Object, and you need to pass it an&nbsp;int: Prior to JDK Version 5.0, the above code will produce a compile-time error:&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/415\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/415\/revisions\/425"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}