{"id":19,"date":"2018-11-08T02:31:00","date_gmt":"2018-11-08T01:31:00","guid":{"rendered":""},"modified":"2020-08-22T14:28:16","modified_gmt":"2020-08-22T12:28:16","slug":"java-progamming-for-beginners-lesson-3-your-second-program-java-basic-syntax","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-3-your-second-program-java-basic-syntax\/","title":{"rendered":"Java Progamming For Beginners Lesson 3: Your Second Program (Java Basic Syntax)"},"content":{"rendered":"<p>In this Tutorial 3, we would write a second program and also understand java basic syntax. In <a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-1-introduction-to-java-and-intallation-of-netbeans\/\" target=\"_blank\" rel=\"noopener\">Tutorials 1<\/a> we installed NetBeans which is the compiler you will be using.<br \/>\nIn <a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-2-write-your-first-java-program\/\" target=\"_blank\" rel=\"noopener\">Tutorial 2<\/a>, we wrote our first program.<\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\nLet&#8217;s start with a little exercise. (Your Second Program)<br \/>\nWrite and run the program below. You already know how to create Java application in NetBeans.<\/p>\n<div style=\"display: block;\">\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/1.bp.blogspot.com\/-711aRaQr4OA\/W-Oe59YddsI\/AAAAAAAACco\/TtNW2eAsmjkY6gSnyxlbdjcI7ak8xIu6QCLcBGAs\/s1600\/Program%2B2.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" src=\"https:\/\/1.bp.blogspot.com\/-711aRaQr4OA\/W-Oe59YddsI\/AAAAAAAACco\/TtNW2eAsmjkY6gSnyxlbdjcI7ak8xIu6QCLcBGAs\/s640\/Program%2B2.jpg\" alt=\"\" width=\"640\" height=\"340\" border=\"0\" data-original-height=\"626\" data-original-width=\"1178\" \/><\/a><\/p>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\nString name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson The Genius\"<\/span><span style=\"color: #333333;\">;<\/span> <span style=\"color: #888888;\">\/\/You can replace Kindson The Genius with your own name<\/span>\r\n      String title <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Researcher\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n      String location <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Europe\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> duration <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span>\r\n\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"My Name is \"<\/span><span style=\"color: #333333;\">+<\/span> name <span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I am a \"<\/span> <span style=\"color: #333333;\">+<\/span> title <span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I live in \"<\/span> <span style=\"color: #333333;\">+<\/span> location<span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I have stayed here for \"<\/span> <span style=\"color: #333333;\">+<\/span> duration <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\" year\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Your Second Program<\/p>\n<p>&nbsp;<\/p>\n<p>If you have written it correctly and run it, the window would be as shown in Figure 1 below.<\/p>\n<p>Figure 1<\/p>\n<p>Let&#8217;s understand a few things about the program above<\/p>\n<p>1. The first four lines following the main line show a declaration of four variables. The first 3 are strings while the last one is an integer(number). A variable is just a memory location to hold values.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">      String name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson The Genius\"<\/span><span style=\"color: #333333;\">;<\/span> <span style=\"color: #888888;\">\/\/YOu can replace Kindson The Genius with your own name<\/span>\r\n      String title <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Researcher\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n      String location <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Europe\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> duration <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span><\/pre>\n<p>Variable declaration in Java<\/p>\n<p>2. Note in the first line that there is a part that starts with \/\/ You can replace&#8230; This is example of a comment. The compiler ignores comments. Comments are what you add to a program to explain it to someone reading it. The compiler does not execute it.<\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #888888;\">\/\/YOu can replace Kindson The Genius with your own name<\/span><\/pre>\n<p>Example of comment in Java<\/p>\n<p>3. Note the use of + sign in the last four lines. This plus sign is called &#8216;concatenation operation&#8217; (say in 5 times!). It is used to combine two variable together into a string.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"My Name is \"<\/span><span style=\"color: #333333;\">+<\/span> name <span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I am a \"<\/span> <span style=\"color: #333333;\">+<\/span> title <span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I live in \"<\/span> <span style=\"color: #333333;\">+<\/span> location<span style=\"color: #333333;\">);<\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"I have stayed here for \"<\/span> <span style=\"color: #333333;\">+<\/span> duration <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\" year\"<\/span><span style=\"color: #333333;\">);<\/span><\/pre>\n<p>Concatenation in Java<\/p>\n<p>4. Also note that part of the statement to be printed is enclosed in double quotes. This is called string literal. The next part not enclosed in quotes is called variable (can be string variable or integer variable)<\/p>\n<p>Try to digest this all that have been mentioned here and then Tutorial 4 would be out is couple of days.<\/p>\n<p>&nbsp;<\/p>\n<p>Tutorial 1:\u00a0<a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-1-introduction-to-java-and-intallation-of-netbeans\/\">Introduction to Java and NetBeans Installation<\/a><br \/>\nTutorial 2:\u00a0<a href=\"https:\/\/kindsonthegenius.com\/blog\/java-progamming-for-beginners-lesson-2-write-your-first-java-program\/\" target=\"_blank\" rel=\"noopener\">Write Your First Java Program in NetBeans<\/a><\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\">\u00a0<\/ins><\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\">\u00a0<\/ins><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Tutorial 3, we would write a second program and also understand java basic syntax. In Tutorials 1 we installed NetBeans which is the &hellip; <\/p>\n","protected":false},"author":2,"featured_media":314,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[85],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":7,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":1084,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/19\/revisions\/1084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/314"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}