{"id":77,"date":"2024-04-23T15:54:26","date_gmt":"2024-04-23T15:54:26","guid":{"rendered":"https:\/\/tuhintech.com\/tutorials\/?p=77"},"modified":"2024-04-23T15:58:25","modified_gmt":"2024-04-23T15:58:25","slug":"java-get-started-2","status":"publish","type":"post","link":"https:\/\/tuhintech.com\/tutorials\/java\/java-get-started-2\/","title":{"rendered":"Java Syntax"},"content":{"rendered":"<section class=\"kc-elm kc-css-617557 kc_row\"><div class=\"kc-row-container  kc-container\"><div class=\"kc-wrap-columns\"><div class=\"kc-elm kc-css-191380 kc_col-sm-3 kc_column kc_col-sm-3\"><div class=\"kc-col-container\"><div class=\"widget widget_nav_menu kc-elm kc-css-949070\"><h2 class=\"widgettitle\">Java Tutorials<\/h2><nav class=\"menu-java-menu-container\" aria-label=\"Java Tutorials\"><ul id=\"menu-java-menu\" class=\"menu\"><li id=\"menu-item-72\" class=\"menu-item menu-item-type-post_type menu-item-object-post menu-item-72\"><a href=\"https:\/\/tuhintech.com\/tutorials\/java\/java-home\/\" class=\"menu-link\">Java Home<\/a><\/li>\n<li id=\"menu-item-71\" class=\"menu-item menu-item-type-post_type menu-item-object-post menu-item-71\"><a href=\"https:\/\/tuhintech.com\/tutorials\/java\/java-intro\/\" class=\"menu-link\">Java Intro<\/a><\/li>\n<li id=\"menu-item-70\" class=\"menu-item menu-item-type-post_type menu-item-object-post menu-item-70\"><a href=\"https:\/\/tuhintech.com\/tutorials\/java\/java-get-started\/\" class=\"menu-link\">Java Get Started<\/a><\/li>\n<li id=\"menu-item-81\" class=\"menu-item menu-item-type-post_type menu-item-object-post menu-item-81\"><a href=\"https:\/\/tuhintech.com\/tutorials\/java\/java-get-started-2\/\" class=\"menu-link\">Java Syntax<\/a><\/li>\n<\/ul><\/nav><\/div><\/div><\/div><div class=\"kc-elm kc-css-529040 kc_col-sm-9 kc_column kc_col-sm-9\"><div class=\"kc-col-container\"><div class=\"kc-elm kc-css-685078 kc_text_block\"><\/p>\n<h1>Java Syntax\u00a0<\/h1>\n<div class=\"w3-clear nextprev\"><a class=\"w3-left w3-btn\" href=\"https:\/\/www.w3schools.com\/java\/java_intro.asp\">\u276e Previous<\/a><a class=\"w3-right w3-btn\" href=\"https:\/\/www.w3schools.com\/java\/java_syntax.asp\">Next \u276f<\/a><\/div>\n<hr \/>\n<h2>Java Syntax<\/h2>\n<p>In the previous chapter, we created a Java file called\u00a0<code class=\"w3-codespan\">Main.java<\/code>, and we used the following code to print &#8220;Hello World\" to the screen:<\/p>\n<div class=\"w3-example\">\n<p>Main.java<\/p>\n<pre class=\"w3-white language-java\" tabindex=\"0\"><code class=\"language-java\"><span class=\"token keyword keyword-public\">public<\/span> <span class=\"token keyword keyword-class\">class<\/span> <span class=\"token class-name\">Main<\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token keyword keyword-public\">public<\/span> <span class=\"token keyword keyword-static\">static<\/span> <span class=\"token keyword keyword-void\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Hello World\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n<\/code><\/pre>\n<p><a class=\"w3-btn\" href=\"https:\/\/www.w3schools.com\/java\/tryjava.asp?filename=demo_helloworld\" target=\"_blank\" rel=\"noopener\">Try it Yourself \u00bb<\/a><\/p>\n<\/div>\n<h3>Example explained<\/h3>\n<p>Every line of code that runs in Java must be inside a\u00a0<code class=\"w3-codespan\">class<\/code>. In our example, we named the class\u00a0<strong>Main<\/strong>. A class should always start with an uppercase first letter.<\/p>\n<p><strong>Note:<\/strong>\u00a0Java is case-sensitive: &#8220;MyClass\" and &#8220;myclass\" has different meaning.<\/p>\n<p>The name of the java file\u00a0<strong>must match<\/strong>\u00a0the class name. When saving the file, save it using the class name and add &#8220;.java\" to the end of the filename. To run the example above on your computer, make sure that Java is properly installed: Go to the\u00a0Get Started Chapter\u00a0for how to install Java. The output should be:<\/p>\n<div class=\"w3-example w3-padding-16\">\n<div class=\"notranslate w3-black w3-padding\"><code class=\"notranslate\">Hello World<\/code><\/div>\n<\/div>\n<hr \/>\n<h2>The main Method<\/h2>\n<p>The\u00a0<code class=\"w3-codespan\">main()<\/code>\u00a0method is required and you will see it in every Java program:<\/p>\n<div class=\"w3-example\">\n<pre class=\"w3-white language-java\" tabindex=\"0\"><code class=\"language-java\"><span class=\"token keyword keyword-public\">public<\/span> <span class=\"token keyword keyword-static\">static<\/span> <span class=\"token keyword keyword-void\">void<\/span> <span class=\"token function\"><strong>main<\/strong><\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span>\r\n<\/code><\/pre>\n<\/div>\n<p>Any code inside the\u00a0<code class=\"w3-codespan\">main()<\/code>\u00a0method will be executed. Don&#8217;t worry about the keywords before and after main. You will get to know them bit by bit while reading this tutorial.<\/p>\n<p>For now, just remember that every Java program has a\u00a0<code class=\"w3-codespan\">class<\/code>\u00a0name which must match the filename, and that every program must contain the\u00a0<code class=\"w3-codespan\">main()<\/code>\u00a0method.<\/p>\n<hr \/>\n<h2>System.out.println()<\/h2>\n<p>Inside the\u00a0<code class=\"w3-codespan\">main()<\/code>\u00a0method, we can use the\u00a0<code class=\"w3-codespan\">println()<\/code>\u00a0method to print a line of text to the screen:<\/p>\n<div class=\"w3-example\">\n<pre class=\"w3-white language-java\" tabindex=\"0\"><code class=\"language-java\"><span class=\"token keyword keyword-public\">public<\/span> <span class=\"token keyword keyword-static\">static<\/span> <span class=\"token keyword keyword-void\">void<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n  <strong><span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Hello World\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/strong>\r\n<span class=\"token punctuation\">}<\/span>\r\n<\/code><\/pre>\n<p>Try it Yourself \u00bb<\/p>\n<\/div>\n<div class=\"w3-note w3-panel\">\n<p><strong>Note:<\/strong>\u00a0The curly braces\u00a0<code class=\"w3-codespan\">{}<\/code>\u00a0marks the beginning and the end of a block of code.<\/p>\n<p><code class=\"w3-codespan\">System<\/code>\u00a0is a built-in Java class that contains useful members, such as\u00a0<code class=\"w3-codespan\">out<\/code>, which is short for &#8220;output\". The\u00a0<code class=\"w3-codespan\">println()<\/code>\u00a0method, short for &#8220;print line\", is used to print a value to the screen (or a file).<\/p>\n<p>Don&#8217;t worry too much about\u00a0<code class=\"w3-codespan\">System<\/code>,\u00a0<code class=\"w3-codespan\">out<\/code>\u00a0and\u00a0<code class=\"w3-codespan\">println()<\/code>. Just know that you need them together to print stuff to the screen.<\/p>\n<p>You should also note that each code statement must end with a semicolon (<code class=\"w3-codespan\">;<\/code>).<\/p>\n<\/div>\n<p>\n<\/div><\/div><\/div><\/div><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"site-sidebar-layout":"no-sidebar","site-content-layout":"","ast-site-content-layout":"full-width-container","site-content-style":"boxed","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[9],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":3,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/posts\/77\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tuhintech.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}