{"id":318,"date":"2021-01-03T21:59:45","date_gmt":"2021-01-03T21:59:45","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=318"},"modified":"2021-01-03T22:01:48","modified_gmt":"2021-01-03T22:01:48","slug":"objects-arrays","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/","title":{"rendered":"Objects &#038; Arrays"},"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-6a06ee19ec03e\" 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-6a06ee19ec03e\"  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\/objects-arrays\/#Objects\" >Objects<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Creating_an_Object\" >Creating an Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Accessing_an_Object\" >Accessing an Object<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Destroying_an_Object\" >Destroying an Object<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Arrays\" >Arrays<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Creating_an_Array\" >Creating an Array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Accessing_an_Array\" >Accessing an Array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/#Destroying_an_Array\" >Destroying an Array<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>Objects and Arrays are the two basic kinds of\u00a0<em>Reference Types<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Objects\"><\/span>Objects<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_an_Object\"><\/span>Creating an Object<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Two stages:<ul><li>Declare the variable to hold the reference to an object<\/li><li>Use&nbsp;<strong>new<\/strong>&nbsp;to create an object and associate it with the reference variable:<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Grommet g;\nGrommet h = new Grommet();\ng = new Grommet();\nh = new Grommet();\t\/\/ What does this do?<\/strong><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Accessing_an_Object\"><\/span>Accessing an Object<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the dot (<strong>.<\/strong>) operator to access fields of an object:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>ComplexNumber c = new ComplexNumber();\nc.x = 1.0;\nc.y = -1.414;\ndouble m = c.magnitude();<\/strong><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Destroying_an_Object\"><\/span>Destroying an Object<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>There is&nbsp;<em><strong>no<\/strong><\/em>&nbsp;operator in Java to do the opposite of&nbsp;<strong>new&nbsp;<\/strong>(i.e. no equivalent of&nbsp;<strong>delete&nbsp;<\/strong>in C++)<\/li><li>Java detects when an object is no longer being used (has no references)<\/li><li>Java automatically deletes such objects<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Arrays\"><\/span>Arrays<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_an_Array\"><\/span>Creating an Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>There are two ways of creating an array:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use&nbsp;<code><strong>new<\/strong><\/code>&nbsp;and specify the size of the array:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>char buffer[] = new char[1024];\nGrommet[] grommets = new Grommet[10];<\/strong><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note where you can place the&nbsp;<strong>[]<\/strong>&nbsp;! (Unlike in C or C++)<\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>Use an <em>initializer<\/em>:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>int ages[] = { 10, 35, 16, 92, 53 };<\/strong><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Each initializer value may be an arbitrary expression (unlike C).<\/p><\/blockquote>\n\n\n\n<p><strong>Notes:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>You&nbsp;<em><strong>cannot<\/strong><\/em>&nbsp;say&nbsp;<strong>int&nbsp;ages[5]&nbsp;<\/strong>because it is a declaration of a&nbsp;<em>reference to an array.<\/em><\/li><li>What it refers to dynamically keeps track of its own&nbsp;<strong>length&nbsp;<\/strong>(number of elements)<\/li><li>So, the right-hand side must define the number of elements.<\/li><li>The reference variable&nbsp;<strong>ages&nbsp;<\/strong>can refer to arrays of different sizes during its lifetime.<\/li><\/ol>\n\n\n\n<p>Here&#8217;s the result of creating the array of primitive types:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>int ages[] = { 10, 35, 16, 92, 53 };<\/strong><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"461\" height=\"226\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/array1.gif\" alt=\"\" class=\"wp-image-319\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:39px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>On the other hand, here&#8217;s the result of creating an array of reference types:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Grommet[] grommets = new Grommet[10];<\/strong><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"647\" height=\"356\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/array3.gif\" alt=\"\" class=\"wp-image-320\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Accessing_an_Array\"><\/span>Accessing an Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Indexes are 0-based, like C\/C++<\/li><li>An index is either of type&nbsp;<strong>int<\/strong>, or be convertible to&nbsp;<strong>int<\/strong><\/li><li>Index values are&nbsp;<em>bounds-checked!<\/em><ul><li>If bounds-checking fails at run time, throws&nbsp;<strong>ArrayIndexOutOfBoundsException<\/strong>.<\/li><\/ul><\/li><li>The number of elements in the array may be determined from the&nbsp;<strong>length&nbsp;<\/strong>field.<\/li><li>Example:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>int a[] = new int[100];\na[0] = 0;\nfor (int i = 1; i &lt; a.length; i++)\n    a[i] = i + a[i-1];<\/strong><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Destroying_an_Array\"><\/span>Destroying an Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Arrays are automatically garbage collected, as are objects.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Objects and Arrays are the two basic kinds of\u00a0Reference Types. Objects Creating an Object Two stages:Declare the variable to hold the reference to an objectUse&nbsp;new&nbsp;to create an object and associate it with the reference variable: Grommet g; Grommet h = new Grommet(); g = new Grommet(); h = new Grommet(); \/\/ What does this do? [&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-318","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":"Objects and Arrays are the two basic kinds of\u00a0Reference Types. Objects Creating an Object Two stages: Declare the variable to hold the reference to an object Use&nbsp;new&nbsp;to create an object and associate it with the reference variable: Grommet g; Grommet h = new Grommet(); g = new Grommet(); h = new Grommet(); \/\/ What does&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/318","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=318"}],"version-history":[{"count":1,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/318\/revisions"}],"predecessor-version":[{"id":321,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/318\/revisions\/321"}],"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=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}