{"id":77,"date":"2020-12-31T22:07:07","date_gmt":"2020-12-31T22:07:07","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=77"},"modified":"2021-01-14T15:10:49","modified_gmt":"2021-01-14T15:10:49","slug":"generic-programming","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/","title":{"rendered":"Generic Programming"},"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-6a06ed5980cc2\" 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-6a06ed5980cc2\"  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\/generic-programming\/#Enter_Generic_Programming\" >Enter Generic Programming!<\/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\/generic-programming\/#Concept\" >Concept<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>How many times have you seen code that does something like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n    ArrayList list = new ArrayList();\n    \n    ...\n    for (int i = 0; i &lt; list.size(); i++)\n    {\n      String name = (String) list.get(i); \/\/ Note required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n    for (Iterator iter = list.iterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n<\/pre><\/div>\n\n\n<p>where we are forced to do an explicit <em>typecast<\/em>, because we &#8220;know&#8221; the type that is being returned from some method (in this case, from a Collections class) ?<\/p>\n\n\n\n<p>This kind of coding is annoying, and error-prone.&nbsp; If the type of object that is actually returned does not match what you thought, then the JVM will throw a&nbsp;<code><strong>ClassCastException<\/strong><\/code>.<\/p>\n\n\n\n<p>It would be nice to have some mechanism where this kind of thing could be specified and checked at compile time, thus avoiding the error-prone practices exemplified above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Enter_Generic_Programming\"><\/span>Enter Generic Programming!<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>JDK 1.5 (5.0) introduced&nbsp;<em><strong>Generic Programming<\/strong><\/em>&nbsp;(a.k.a.&nbsp;<em><strong>Generics<\/strong><\/em>) to solve this problem.<\/p>\n\n\n\n<p>Generics allow you to write code that is safer and easier to read than code that includes lots of explicit typecasting, and the like.<\/p>\n\n\n\n<p>They provide you with a great deal of power to write generic classes &#8212; classes that basically behave the same, but use different types.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Concept\"><\/span>Concept<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>What&#8217;s a Generic Class?<\/p>\n\n\n\n<p>It&#8217;s a class with one or more&nbsp;<strong>type variables<\/strong>.&nbsp; Here&#8217;s a simple example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [8,13,19,24,29,34,39,40]; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\n\/**\n * A simple class to represent a pair of values\n * of potentially different types.\n *\n *\/\npublic class Pair&lt;T1, T2&gt;\n{ \n  \/** \n   * Creates a new instance of Pair \n   *\/\n  public Pair(T1 first, T2 second)\n  {\n    m_first = first;\n    m_second = second;\n  }\n  \n  public T1 getFirst() \n  { \n    return m_first; \n  }\n  \n  public void setFirst(T1 value)\n  {\n    m_first = value;\n  }\n  \n  public T2 getSecond() \n  { \n    return m_second; \n  }\n  \n  public void setSecond(T2 value)\n  {\n    m_second = value;\n  }\n  \n  private T1 m_first;\n  private T2 m_second;\n}\n<\/pre><\/div>\n\n\n<p>and here&#8217;s a very simple class to test it out:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [7,8]; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\npublic class PairTest\n{\n  public static void main(String&#x5B;] args)\n  {\n    Pair&lt;String, Integer&gt; pair1 = \n      new Pair&lt;String, Integer&gt;(&quot;Fred Bloggs&quot;, 5);\n    \n    System.out.println( pair1.getFirst() + &quot; owns &quot; + pair1.getSecond() );\n  }\n}\n<\/pre><\/div>\n\n\n<p>This outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Fred Bloggs owns 5<\/strong><\/pre>\n\n\n\n<p>I could have chosen any combination of types (other than primitive types) for my purposes.<\/p>\n\n\n\n<p>In other words, I am able to write a generally useful class and parameterize it with types.&nbsp; Then, I can specify the types that I need when I come to use that class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times have you seen code that does something like: where we are forced to do an explicit typecast, because we &#8220;know&#8221; the type that is being returned from some method (in this case, from a Collections class) ? This kind of coding is annoying, and error-prone.&nbsp; If the type of object that is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"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-77","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":"How many times have you seen code that does something like: where we are forced to do an explicit typecast, because we &#8220;know&#8221; the type that is being returned from some method (in this case, from a Collections class) ? This kind of coding is annoying, and error-prone.&nbsp; If the type of object that is&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/77","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=77"}],"version-history":[{"count":5,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/77\/revisions"}],"predecessor-version":[{"id":1264,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/77\/revisions\/1264"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}