Getting started with Java

In this article, We will learn how to start with Java Programming and will write a small print “Hello World” program.

Steps:

  1. IDE (Java Editor) installation
  2. Writing Hello World Java program.

Java Editor

To write Java programs, you will need a text editor. There are different IDEs (Integrated Development Environment) available in the market like Eclipse, Netbeans or  IntelliJ IDEA, which are particularly useful when managing larger collections of Java files.

I will prefer to use the IntelliJ IDEA  Community version. Now go to the above-mentioned link to download IntelliJ IDEA and install.

Creating a New Project in IntelliJ IDEA

1. After installation the IntelliJ platform looks like this:

Screenshot 2020-03-04 12.20.31

2. Click on Create New Project.

Screenshot 2020-03-04 12.21.23

3. I am going to create Java projects so I will select Java from the available left side options and then click Next.

4 . Now give your project a name and then click on finish.

5 . Inside the src folder, to create a new java file right-click and then select New then Java Class.

Screenshot 2020-03-06 16.35.32

 

6. Now named the class which you want In my case I will name it as HelloWorld.

Screenshot 2020-03-06 17.05.36

7. Inside the HelloWorld.java file, I will write the following code to print “Hello, World!” to the screen:

Java “Hello, World!” Program

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

8 . Run the program using the Run button.

Screenshot 2020-03-06 12.21.31

When you run the program, You can see the output of the program in the Console screen, the output will be:

Screenshot 2020-03-06 12.45.56

Understanding Code:

Let’s understand how “Hello, World!” program works in Java.

1 . public class HelloWorld { … }

In Java, every application begins with a class definition. In the program, HelloWorld is the name of the class and class definition is:.

public class HelloWorld {
....
}

For now, just remember that every Java application has a class definition, and the name of the class should match the filename in Java.

This above line declares a class named HelloWorld, which is public, which means that any other class can access it.

Notes:

  1. A class should always start with an uppercase first letter. For example, if you want to name your class as helloworldexample you have to write like this HelloWorldExample.
  2. Java is case-sensitive: means It will treat “HelloWorld” and “helloworld” differently.
  3. When we declare a public class (HelloWorld), we must declare it inside a file with the same name (HelloWorld.java), otherwise we’ll get an error when compiling.

The main Method

2 . public static void main(String args[]){ }

This is the main method. Every application in Java must contain the main method.

The main method is the entry point of our Java program which means It tells the compiler to start executing the code from here.

The main method must have this exact signature in order to be able to run our program.

public static void main(String... args){
    ...
}
  • public means that anyone can access it.
  • static means that you can run this method without creating an instance of HelloWorld class.
  • void means that this method doesn’t return any value.
  • main is the name of the method.
  • The main method takes array of String as an argument which you can specify with any name and with any of the following syntax :
String[] args or
String args[]

3 . System.out.println(“Hello World”);

The following code prints the string inside quotation marks Hello, World! to standard output (your screen).

  • System is a pre-defined class that Java provides us and it holds some useful methods and variables.
  • out is a static variable within System that represents the output of your program (stdout).
  • println method is an upgraded version of print() inside PrintStream which prints any argument passed to it on the screen and adds a new line to the output. In this program, it will print Hello, World! and add a new line.

Note: Each code statement must end with a semicolon.

There is no need to worry about class, static, modifier(public) we will talk about it later in detail.

Conclusion

For now, just remember the below points:

1 .Every Java application has a class definition, and if the class is public then it is mandatory that the name of the class should match with the Java file name.

2 .Every application in Java must contain the main method which tells the compiler to start executing the code from here.

 

Leave a Reply