Writing your first program in Java

Table of contents:

Writing your first program in Java
Writing your first program in Java

Java is an object-oriented programming language developed in 1991 by James Gosling, meaning that it uses concepts such as "objects" with "fields" (which describe characteristics of the object) and "methods" (actions that the object can perform). Java is a "write once, run anywhere" language, meaning it is designed to run on any platform and on any Java Virtual Machine (JVM). Since Java uses a lot of common language, it is easy for beginners to learn and understand. This tutorial is an introduction to writing programs in Java.


Method 1 of 3: Your first Java program

91968 1

Step 1. In order to start writing programs in Java, you will first need to set up a working environment

Many programmers use integrated development environments (IDEs) such as Eclipse and Netbeans for programming in Java, but one can write and compile a Java program without heavy IDEs.

91968 2

Step 2. Any kind of Notepad-like program will suffice for programming in Java

Hardcore programmers sometimes prefer the simple text editors from the terminal, such as vim and emacs. A very good text editor that can be installed on both a Windows computer and a Linux-based machine (Ubuntu, Mac, etc.) is Sublime Text, which we will be using in this tutorial.

91968 3

Step 3. Make sure you have installed the Java Software Development Kit

You will need this to compile your programs.

  • Under Windows, if the environment variables are incorrect, you may get an error when executing


    . To avoid these errors, please refer to the Java Software Development Kit installation article for more information.

Method 2 of 3: Hello world program

91968 4

Step 1. First, we create a program that shows "Hello World" on the screen

Create a new file in your text editor, and save it as "HelloWorld.java". HalloWereld is the name of your class, which must be the same as that of your file.

91968 5

Step 2. Declare your class and the main method

The main method

public static void main(String[] args)

is the method that is executed when the program is running. This main method has the same method declaration in every Java program.

public class HelloWorld { public static void main(String[] args) { } }

91968 6

Step 3. Write the line of code that will show "Hello World"

System.out.println("Hello world.");

  • Let's break down this rule into its different components:

    • System

      tells the system that something must be done.
    • out

      tells the system that there is an output.
    • println

      stands for "print this line," thus telling the system that the output is a line of text.
    • The quotes around

      ("Hello World.")

      means that the method


      asks for a parameter; in this case it's the string

      "Hello World."

  • Note that there are some Java rules that we must follow here:

    • Always place a semicolon at the end of a program line.
    • Java is case sensitive, so you will have to put the names of the methods, variables and classes in the correct font size, otherwise an error will follow.
    • Blocks of code associated with a particular method or loop are enclosed in braces.
91968 7

Step 4. Put it all together

The final Hello World program should now look like this:

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

91968 8

Step 5. Save your file and open Command Prompt or Terminal to compile the program

Navigate to the folder where you saved HalloWereld.java and type

javac HelloWorld.java

. This tells the Java compiler that you want to compile HalloWereld.java. If there are errors, the compiler will see what you did wrong. In all other cases the compiler will not display any messages. If you look at the directory where you saved HalloWereld.java, you should see the file HalloWereld.class. This is the file that Java uses to run your program.

91968 9

Step 6. Run the program

Finally we can start running the program! In the command window or terminal, type the following:

java HelloWorld

. Indicates that Java should execute the class HalloWereld. You should see "Hello World" printed on the screen (in the console).

91968 10

Step 7. Congratulations, you have written your first Java program

Method 3 of 3: Input and output

91968 11

Step 1. We will next extend our Hello World program by accepting input from the user

In our Hello World program, we have a string printed on the screen, but the interactive part of programs is where the user can enter data. We are now going to extend our program with a request to the user to enter his or her name, followed by a greeting, followed by the name of the user.

91968 12

Step 2. Import the Scanner class

In Java, there are a number of built-in libraries (libraries) that we can use, but we'll have to import them first. One of these libraries is java.util, which has a Scanner object that we need to accept user input. To be able to import the Scanner class we add the following line at the beginning of our code.

import java.util.Scanner;

  • This tells our program that we want to use the Scanner object in the java.util package.
  • If we want to access every object in java.util, we write

    import java.util.*;

    at the beginning of our code.
91968 13

Step 3. Within our main method we create a new instance of the Scanner object

Java is an object-oriented language, so its concepts will use objects. The Scanner object is an example of an object with fields and methods. To be able to use the Scanner class we create a new Scanner object which we can then fill in the fields and use the methods of. You do this as follows:

Scanner userInputScanner = new Scanner(System.in);

  • userInputScanner

    is the name of the Scanner object we just instantiated. Note that every part of the name is capitalized (camel case); this is the convention for naming variables in Java.
  • We use the


    operator to create a new instance of an object. So, in this case we created a new instance of the Scanner object using the code

    new Scanner(System.in)

  • The Scanner object asks for a parameter that tells the object what to scan. In this case, we place the


    as parameters.


    tells the program to look for input from the system, which in this case is what the user types into the program.
91968 14

Step 4. Ask the user for input

We will have to ask the user to type something as input so that the user knows when to enter something into the console. You can do this with


or with



System.out.print("What is your name?");

91968 15

Step 5. Ask the Scanner object to take the next line of what the user types and store it as a variable

The Scanner will always save what the user types. The next line of code will ask the Scanner to store what the user has typed as a name in a variable:

String userInputName = userInputScanner.nextLine();

  • In Java, the convention for using an object's method is the code


    . Of


    we call the Scanner object under the name we just gave it, then we call its method with


    calls without parameters.
  • Note that we store the following line in another object: the String. We have our String object


91968 16

Step 6. Print a greeting on the screen to the user

Now that we have saved the user's name, we can print a greeting to the user. do you know the

System.out.println("Hello world.");

any code we wrote in the main class? All the code we just wrote should be above that line. Now we can modify that line to say the following:

System.out.println("Hello " + userInputName + "!");

  • The way we put "Hello", the username and "!" linked together by

    "Hello " + userInputName + "!"

    to write is called String concatenation.
  • What happens here is that we are dealing with three strings (strings): "Hello ", userInputName, and "!". Strings in Java are immutable and thus cannot be changed. So when we concatenate these three strings, we essentially create a new string with the greeting.
  • Then we take this new string and use it as a parameter for


91968 17

Step 7. Combine it and save your work

Our code should now look like this:

import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner userInputScanner = new Scanner(System.in); System.out.print("What is your name?"); String userInputName = userInputScanner.nextLine(); System.out.println("Hello " + userInputName + "!"); } }

91968 18

Step 8. Compile and run the program

Open the Command Window or the Terminal and run the same commands as for our first version of HalloWereld.java. We will have to compile the program first:

javac HelloWorld.java

. Then we can run it:

java HelloWorld



  • Java is an object-oriented programming language, so it's helpful to read more about the fundamentals of object-oriented programming languages.
  • Object Oriented Programming (OOP) has many features specific to its paradigm. Three of these key features are:

    • encapsulation: (encapsulation) the ability to restrict access to some parts of the object. Java has private, protected and public modifiers for fields and methods.
    • Polymorphism: The ability for objects to assume different identities. In Java, an object can become part of another object in order to use the methods of the other object.
    • Inheritance: (inheritance) the ability to use fields and methods from another class in the same hierarchy as the current object.

Popular by topic