Introduction to Java

Install Apache Netbeans.

Open Netbeans and go to File > New project > Java with Maven > Java application.

Click Next and select a project name as well as a project location. You can also name the package.

Hello World

The very first program to start with: print “Hello World”. The file which is automatically created is called HelloWorld.java (it must have the same name with the class). Moreover the class name must start with un uppercase letter.

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

Everything in Java is associated with a class. Even the simpler program (in our case the “HelloWorld” program) must be inside a class. The class is “public” as it is out in the open for anyone to see. A public class is accessible from another program.

Inside the class we usually start with the main() method.

A method in Java is a function, i.e. a block of code that performs specific actions when called. The default choice for most Java programs is the public static void main() method.

Public means accessible from external classes. Static means that the main() method belongs to the class rather than to a specific instance. Void means that the method returns nothing. More of these aspects will be discussed later.

The arguments of the main() method could be empty. Usuallly we use String[] args, which is an array of Strings named args. The point is that the user may pass some arguments to the program. For more see here.

Inputs and Outputs

To get an input from the user (through the keyboard), the java.util.Scanner package must be imported to the program.

To output (print) a message or a variable in Java, use the System.out.print() or System.out.println() to print in separate lines.

import java.util.Scanner;
public class InputsOutputs {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a number");
        int num = scan.nextInt();
        System.out.println("The number is " + num);
    }
}

The variable scan is an object of the class Scanner which is used to “scan” inputs from the user.

The scan.nextInt() is a method that returns the input as integer.

There are also the following methods:

  • scan.nextDouble() returns the input as a double number (decimal)
  • scan.next() returns the input as a string
  • scan.nextLine() returns the whole line as a string
import java.util.Scanner;
public class InputsOutputs {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter an integer");
        int i = scan.nextInt();
        System.out.println("The integer is " + i);
        System.out.println ("enter a double");
        double d = scan.nextDouble();
        System.out.println("The double is " + d);
        System.out.println ("enter a word");
        String w = scan.next();
        System.out.println("The word is " + w);
        System.out.println ("enter a phrase");
        String p = scan.nextLine();
        System.out.println("The phrase is " + p);
    }
}