JAVA Basics #3 - Hello World

chathurashmini

Chathumi Kumarapeli

Posted on May 7, 2021

JAVA Basics #3 - Hello World

Since you have now successfully installed java in your machines, now it's the time to get our hands dirty!
We are going to create a new java project and going to learn java syntaxes one by one.

Before writing your first piece of code, you should have to get familiar with few words. First one is functions. Even if you are new to programming, I am pretty sure that you have heard the word functions at least in your Mathematics class.

alt whatAreFunctions

When it comes to programming we use the word 'method' in the place of function. The structure of a method looks like this;

returnType methodName (dataType arg1, dataType arg2, ...) {
... ... executable code;
}

Ex:

void getArea (int lenght, int width) {
}
Enter fullscreen mode Exit fullscreen mode

The above mentioned structure and the example will be explained in detail in upcoming paragraphs. For now just keep in mind that, in java the opening curly brace '{' is usually placed to the right of the argument list after the closing parenthesis ')', in the same line. And also mark that we name methods in camel naming convention as it is shown in the above example.

A java program should at least consists of one method which is the main function. This is said to be the entry point to any program. That is because whenever a java program is executed, the main function of that program gets called and the code inside it gets executed.

Next you need to know the word CLASS.
alt class

The main method is always included inside the Main class. Why is that?

Java is an Object Oriented Programming language. Therefore, every Java object is a part of a particular java class.

Therefore, every java program must have the 'Main class'. Classes are named in Pascal Naming Convention. Together the Main class and the main method look like this;

public class Main {
       public static void main(String[] args) {
                // code to be executed ...
       }
  }
Enter fullscreen mode Exit fullscreen mode

You may be wondering what are meant by these words public, static, and void, and why they are needed. Let's not worry about that right now, since we will be discussing about them in detail later.

NOW IT'S TIME FOR YOU TO WRITE YOUR FIRST JAVA PROGRAM.

Setting up a java rpoject

Step 01

Open ' intellij IDEA' and click 'New Project'.
alt jNewProject1

Step 02

alt jNewProject2

Step 03

alt jNewProject3

Step 04

Give a name and the location to your project as you wish, and click 'Finish'.
alt jNewProject4

Step 05

If you have followed all the steps correctly, your final outcome should look like this;
alt jNewProject6

As you can see, the name I have given for my java project is 'JavaForBeginners' (red rectangle). Inside that folder you can see a folder named 'src' which stores all the source files that belongs to our project. 'com.company' is my default package. You can see the package in the code set as well (yellow rectangle). Inside that folder we have our class file 'Main' (green rectangle). Note that the name the file and the name of the class must be the same. For example the class Main has to be saved in a file named Main.java.

Your First Java Program

Let us get our hands dirty! Now you are ready for your first java program. Yes, your guess is correct. We are going to print the sentence "Hello World" using Java. If you are a person who came to learn Java after learning Python, then you may start to hate Java at this very moment :) because when it comes to Java, print statement is not as handy as it is in Python.

Let's straightaway jump into the code. It looks like this;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
        }
}
Enter fullscreen mode Exit fullscreen mode

Let's go through this program line by line

public class Main
Enter fullscreen mode Exit fullscreen mode

This is where our Main class begins.
public means that it can be accessed by other classes as well. Main class is not the only class you can have in your Java program. You can declare many classes as per your requirement. When you make this Main class public, other classes also have the authority to access the methods and variables inside the Main class.
class is the key word we use to make the program know that we are declaring a class.
Main is the name of the class.

public static void main(String[] args)
Enter fullscreen mode Exit fullscreen mode

Now you know what is meant by public.
static means to run this method you need not to create an instance (object) of the Main class.
void gives the return type of the 'main' method. void means that this main method does not return anything.
main is the name of the method

System.out.println("Hello World");
Enter fullscreen mode Exit fullscreen mode

Yes you need to take this much of a trouble to print a single statement. Let's find out what these keywords are.
System is one of the pre-defined java classes. It contain various methods and variables that are useful for programmers. If you use dot operator after System, like this System. you will be able to see all the methods belong to this class.
out is one of such variables which is in the System class. It is used to represent the output of the program. out has its very own methods as well. You can also explore them using the dot operator (out.).
println() is a method that belongs to out. This prints a line. You can include the sentence (what you need to get printed) inside parenthesis () within double quotations (if it is a string) and it will be printed by the program.
; semicolon is required in Java programs unlike in Python. If you miss it you program won't run as it is in C and C++.

Now it is time to give you a task :p

TASK

Write a program to display the following output.

"Nessie? You nicknamed my daughter after the Loch Ness MONSTER?", Bella lunges at Jacob again.
Seth, in wolf form, jumps at Bella to protect Jacob.

ANSWER

Let's look at the code directly.

package com.company;
public class Main {
    public static void main(String[] args) {
        System.out.println("\"Nessie? You nicknamed my daughter after the Loch Ness MONSTER?\", Bella lunges at Jacob again.\nSeth, in wolf form, jumps at Bella to protect Jacob.";
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the accurate code that you have to type to get the given output. I think that you might have faced trouble to get double quotations ("") and the new line printed. However, if you have copy pasted the given output straightaway in the main method, Intellij IDEA might have automatically done the hard job for you, by adding the backslash \. Let's evaluate the code.

Here the given output starts with a double quotation. So, to get it printed you need to use the backslash which is known as an 'escape sequence'. So it should start like, "\"Nessie? ...
When you need to print a sentence in a new line, then you have to use \n as shown in the code.
How can you print a backslash then? Yes you have to use double backslashes.
For example, to print the line C:\Users\Documents, the code should look like this;

package com.company;
public class Main {
    public static void main(String[] args) {
     System.out.println("C:\\Users\\Documents"); 
// to print with the backslash
    }
}
Enter fullscreen mode Exit fullscreen mode

Here you will see that the line // to print with the backslash is not printing as well as not even giving any error also. That it because it has // at the beginning, which means that is a comment. Programmers can use comments to increase the readability of their code.

Now you have written your first program. Be familiar with the System.out.println(); command before you head to the rest of the articles :)

💖 💪 🙅 🚩
chathurashmini
Chathumi Kumarapeli

Posted on May 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related