ShowTutorials.com

Refreshing your ideas and broadening your visions


First Java Program

In this chapter, we are going to write our first java program. Lets create a directory, like C:\Java-Programs. We are going to write our first Java program in this directory/folder.
You can use any editor like Notepad, VS Code.

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

Name the file as Sample.java, with the same name as Class

Compile the code using command: javac Sample.java

After compilation Sample.class file is created

Then, run the program using command: java Sample

‘Hello World’ output is printed on the screen.

Understanding the program

Every Java program is in the form of a class. So there is nothing outside classes in Java, all code should be inside an class.
Also, for the writing the main method requires to be inside the class. We will go through the main method in detail below.

 

So, in our program Sample.java, it starts with class Sample. The it starts and ends with curly braces{}. All code inside these curly braces is the body of the program. Also note, that the class name should be same as file name, in this case it is Sample.
Note: Class name and file name can be different in some scenarios. However, the standard is to have same name, as it will give error when we provide access modifiers to the class.

Inside the class, we have one method, which is the main method
public static void main(String[] args)

The main method is the starting point in any Java program. That is, when we execute the Java program, the JVM looks for the main method in the file and then executes it.

Lets breakup and understand the method public static void main(String[] args). We will go into depth for each in later chapters

public: This is the access modifier for the method, it is public, that means, it is accessible from outside.
static: A static method in Java is a method that is part of a class rather than an instance of that class. Since, we want to execute main method without creating instance object of this class, hence the method is static. So the JVM will execute Sample.main() method
void: This is the return type from the method, void means this method will not return anything. The main method should always be void.
main: This is the name of the method.
(String[] args): The round brackets () implies that it is a method, and anything inside these brackets are the things that we are passing to this method, in this case String[] args
String[] args: Here String implies the data type String, and square brackets[] implies that it is an array. And args is the name of this String Array and these are the command-line arguments passed to the method.

The main method also has opening and closing curly brackets. You guessed it right, it is the body of this method. So, when the method is executed, all code within these brackets will be executed.

Then we have
System.out.println(“Hello World”);

This is an statement, which will get executed.
This line is used for printing on the screen. Whatever text is there in double quotes will get printed, in this case “Hello World” will get printed on the screen.

println(): println is an method, which takes string to print on the screen

Note: There are multiple methods for printing, like
print(): prints on same line
println(): prints on next line

System.out: out is an object in System class, and println is the method of the out object in class System.

Our first program prints on the screen, Lets add some code to read input from the keyboard

Reading from Keyboard

Java provides a class called Scanner that is used for reading data from different sources. One of the source is keyboard, which we will demonstrate here

The class Scanner is present in util package provided by Java. We will see how we import and use this class in our code. This class was provided from Version 5 onwards.

The modified program:

import java.util.Scanner;

class Sample {
    public static void main(String[] args) {
        System.out.println("Hello World");
        Scanner scan = new Scanner(System.in);

        // take input from user
        System.out.println("Enter your favourite programming language");
        String lang = scan.nextLine();
        System.out.println(lang + " is awesome");
        scan.close();
    }
}

Output:

Java Scanner example

In the above program:

Line 1: import java.util.Scanner
This is the import statement, to use Scanner class in our program.

Line 6: Scanner scan = new Scanner(System.in);
Here, we are creating an instance object of class Scanner with the reference name scan. The new operator is used to create a instance.
System.in is passed as constructor parameter. It represents input stream for Keyboard. The Scanner works as a pipe to get the input from the Keyboard to the program.

Line 10: String lang = scan.nextLine();
Here, we are reading the next line in String lang, which the user will provide using keyboard.

Line 11: System.out.println(lang + ” is awesome”);
We are printing the input user provided, with a message

Line 12: scan.close();
Calling the close() method of Scanner, to close reading.

 


Latest Topics: