{"id":46,"date":"2020-12-31T21:56:05","date_gmt":"2020-12-31T21:56:05","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=46"},"modified":"2021-01-03T21:19:49","modified_gmt":"2021-01-03T21:19:49","slug":"java-language-fundamentals","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/java-language-fundamentals\/","title":{"rendered":"Java Language Fundamentals"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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-6a21c404dfb59\" 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-6a21c404dfb59\"  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\/java-language-fundamentals\/#Program_Structure\" >Program Structure<\/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\/java-language-fundamentals\/#Objects_Objects\" >Objects, Objects<\/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\/java-language-fundamentals\/#Command_Line_Arguments\" >Command Line Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/java-language-fundamentals\/#Program_Exit_Value\" >Program Exit Value<\/a><\/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\/java-language-fundamentals\/#Environment\" >Environment:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/java-language-fundamentals\/#Comments\" >Comments<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">A very simple Java&nbsp;<em>application<\/em>&nbsp;looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic class HelloWorld \n{\n    public static void main(String&#x5B;] args)\n    {\n        System.out.println(&quot;Hello World!&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program_Structure\"><\/span>Program Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every Java&nbsp;<em>program<\/em>&nbsp;contains a number of&nbsp;<em>classes<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class has:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a name (here,&nbsp;<strong><code>HelloWorld<\/code><\/strong>)<\/li><li>some number of&nbsp;<em>methods<\/em><\/li><li>some number of&nbsp;<em>class variables<\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A Java&nbsp;<em>application<\/em>&nbsp;must have at least one&nbsp;<em>main class<\/em>, which has:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a&nbsp;<em>method<\/em>&nbsp;called&nbsp;<strong>main&nbsp;<\/strong>with the following signature:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><code><\/code>public static void main(String[] args)<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This means that it:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>has a single argument of type&nbsp;<strong><em>array of&nbsp;<\/em>String<\/strong><\/li><li>returns&nbsp;<strong>void&nbsp;<\/strong>&#8212; i.e.,&nbsp;<em>nothing<\/em><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you run an application, you specify the&nbsp;<em>name of a main class<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If that class doesn&#8217;t have a&nbsp;<strong>main&nbsp;<\/strong>method with the proper signature &#8212;&nbsp;<em><strong>Error!<\/strong><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Objects_Objects\"><\/span>Objects, Objects<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Everything<\/em>&nbsp;in a Java program is an object (except for the primitive data types).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the above HelloWorld example, the&nbsp;<strong>main&nbsp;<\/strong>method&#8217;s body calls the&nbsp;<strong>println<\/strong>&nbsp;method of the&nbsp;<strong>System.out<\/strong>object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Command_Line_Arguments\"><\/span>Command Line Arguments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The single argument to&nbsp;<strong>main&nbsp;<\/strong>contains all the command line arguments<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike C and C++, the command line arguments do&nbsp;<em><strong>not<\/strong><\/em>&nbsp;include the program name.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic class EchoArguments\n{\n    public static void main(String&#x5B;] args)\n    {\n        System.out.print(args.length);\n\tSystem.out.println(&quot; arguments passed:&quot;);\n        for (int arg = 0; arg &lt; args.length; arg++)\n            System.out.print(&quot;&#039;&quot; + args&#x5B;arg] + &quot;&#039; &quot;);\n        System.out.println(&quot;&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If the above program is run as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>java [options] EchoArguments fee \"fi fo\" fum 12 Englishmen at 0.5 apiece<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">will output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>8 arguments passed:\n'fee' 'fi fo' 'fum' '12' 'Englishmen' 'at' '0.5' 'apiece'<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that, normally, command line arguments are separated by whitespace.&nbsp; However, you can enclose a single argument within quotes to override this behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program_Exit_Value\"><\/span>Program Exit Value<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you wish to return a numeric value to the system, as a normal C or C++ program would, you must call the&nbsp;<strong>System.exit&nbsp;<\/strong>method:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic static void main(String&#x5B;] args)\n{\n    System.out.println(&quot;Hello World!&quot;);\n    System.exit(0);\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Environment\"><\/span>Environment:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java does not support operating system environment variables &#8212; that&#8217;s non-portable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, Java does support a platform-independent&nbsp;<em>system properties list:<\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic class PropertiesDisplayer\n{\n  public static void main(String&#x5B;] args)\n  {\n    String home = System.getProperty(&quot;user.home&quot;);\n    System.out.println(&quot;HOME = &quot; + home);\n    String classPath = System.getProperty(&quot;java.class.path&quot;);\n    System.out.println(&quot;CLASSPATH = &quot; + classPath);\n    System.getProperties().list(System.out);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When I ran the above program on my system (from within NetBeans), it gave me the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">HOME = C:\\Documents and Settings\\Bryan Higgs\nCLASSPATH = C:\\Documents and Settings\\Bryan Higgs\\My Documents\\Java Projects\\Test\\build\\classes\n-- listing properties --\njava.runtime.name=Java(TM) 2 Runtime Environment, Stand...\nsun.boot.library.path=C:\\Program Files\\Java\\jdk1.5.0\\jre\\bin\njava.vm.version=1.5.0-b64\njava.vm.vendor=Sun Microsystems Inc.\njava.vendor.url=http:\/\/java.sun.com\/\npath.separator=;\njava.vm.name=Java HotSpot(TM) Client VM\nfile.encoding.pkg=sun.io\nuser.country=US\nsun.os.patch.level=Service Pack 2\njava.vm.specification.name=Java Virtual Machine Specification\nuser.dir=C:\\Documents and Settings\\Bryan Higgs...\njava.runtime.version=1.5.0-b64\njava.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment\njava.endorsed.dirs=C:\\Program Files\\Java\\jdk1.5.0\\jre\\li...\nos.arch=x86\njava.io.tmpdir=C:\\DOCUME~1\\BRYANH~1\\LOCALS~1\\Temp\\\nline.separator=\njava.vm.specification.vendor=Sun Microsystems Inc.\nuser.variant=\nos.name=Windows XP\nsun.jnu.encoding=Cp1252\njava.library.path=C:\\Program Files\\Java\\jdk1.5.0\\bin;.;...\njava.specification.name=Java Platform API Specification\njava.class.version=49.0\nsun.management.compiler=HotSpot Client Compiler\nos.version=5.1\nuser.home=C:\\Documents and Settings\\Bryan Higgs\nuser.timezone=\njava.security.policy=applet.policy\njava.awt.printerjob=sun.awt.windows.WPrinterJob\nfile.encoding=Cp1252\njava.specification.version=1.5\nuser.name=Bryan Higgs\njava.class.path=C:\\Documents and Settings\\Bryan Higgs...\njava.vm.specification.version=1.0\nsun.arch.data.model=32\njava.home=C:\\Program Files\\Java\\jdk1.5.0\\jre\njava.specification.vendor=Sun Microsystems Inc.\nuser.language=en\nawt.toolkit=sun.awt.windows.WToolkit\njava.vm.info=mixed mode, sharing\njava.version=1.5.0\njava.ext.dirs=C:\\Program Files\\Java\\jdk1.5.0\\jre\\li...\nsun.boot.class.path=C:\\Program Files\\Java\\jdk1.5.0\\jre\\li...\njava.vendor=Sun Microsystems Inc.\nfile.separator=\\\njava.vendor.url.bug=http:\/\/java.sun.com\/cgi-bin\/bugreport...\nsun.cpu.endian=little\nsun.io.unicode.encoding=UnicodeLittle\nsun.desktop=windows\nsun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, even the default system properties provide quite a bit of information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also pass name\/value pairs into the Java program by using the -D option on the command line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comments\"><\/span>Comments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java supports three types of comments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>C-style comments:<ul><li><strong>\/* &#8230; *\/<\/strong><\/li><li>May not be nested<\/li><\/ul><\/li><li>C++-style comments:<ul><li><strong>\/\/<\/strong>&nbsp;until end of line<\/li><\/ul><\/li><li>Special &#8220;doc comment&#8221;:<ul><li><strong>\/** &#8230; *\/<\/strong><\/li><li>May not be nested<\/li><li>Processed by&nbsp;<strong>javadoc&nbsp;<\/strong>into simple online documentation .HTML pages<\/li><\/ul><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A very simple Java&nbsp;application&nbsp;looks like this: Program Structure Every Java&nbsp;program&nbsp;contains a number of&nbsp;classes A class has: a name (here,&nbsp;HelloWorld) some number of&nbsp;methods some number of&nbsp;class variables A Java&nbsp;application&nbsp;must have at least one&nbsp;main class, which has: a&nbsp;method&nbsp;called&nbsp;main&nbsp;with the following signature: public static void main(String[] args) This means that it: has a single argument of type&nbsp;array of&nbsp;String [&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-46","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":"A very simple Java&nbsp;application&nbsp;looks like this: Program Structure Every Java&nbsp;program&nbsp;contains a number of&nbsp;classes A class has: a name (here,&nbsp;HelloWorld) some number of&nbsp;methods some number of&nbsp;class variables A Java&nbsp;application&nbsp;must have at least one&nbsp;main class, which has: a&nbsp;method&nbsp;called&nbsp;main&nbsp;with the following signature: public static void main(String[] args) This means that it: has a single argument of type&nbsp;array of&nbsp;String&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/46","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=46"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/46\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/46\/revisions\/300"}],"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=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}