Skip to content
Educora
Beginner12 min1 / 18

What is Java? Your first program

Find out what Java is, what the JVM and the JDK do, and write your first program with a main method.

Check yourself
In this lesson you will learn
  • Explain what Java is and where it is used
  • Describe how code runs through the JDK, bytecode and the JVM
  • Write a program with a main method and print output with println

When you pay with a bank card, open an app on an Android phone or play the Java Edition of Minecraft, there is a good chance that code written in Java is running behind the scenes. For more than 30 years Java has been one of the most popular programming languages in the world. In this lesson you will learn how it works and write your first program.

What is Java?

Java is a general-purpose, object-oriented programming language. It was created at Sun Microsystems by a team led by James Gosling and was released to the public in 1995. Since 2010 Java has been developed by Oracle together with an open community (the OpenJDK project). A new version of the language comes out every six months.

  • Android apps — for many years Android apps were written mainly in Java, and today Java and its close relative Kotlin are both widely used.
  • Enterprise systems — banks, retail chains and airlines use Java on their servers.
  • Big data and search — well-known tools such as Apache Hadoop and Elasticsearch are written in Java.
  • Education — many universities teach programming with Java.

How a Java program runs

You write code in an ordinary text file, for example Main.java. Then the compiler (javac) translates this text into bytecode, producing a file called Main.class. Bytecode is a set of instructions not for one particular computer but for the Java Virtual Machine (JVM). And there are JVMs for Windows, macOS, Linux, servers and many other devices.

  1. 1
    Write the code

    Save the program in Main.java. The file name must match the name of the public class.

  2. 2
    Compile

    Type javac Main.java in the terminal: the compiler checks the code for errors and creates the bytecode file Main.class.

  3. 3
    Run

    The command java Main starts the JVM, which executes the bytecode.

TermStands forWhat it is for
JVMJava Virtual Machineexecutes bytecode
JREJava Runtime EnvironmentJVM + standard libraries: for running finished programs
JDKJava Development KitJRE + the javac compiler and other tools: for writing programs
As a programmer you need the JDK — it includes everything else.

Your first program

By tradition, the first program in a new language prints “Hello, world!”. In Java, even the smallest program lives inside a class:

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
Expected output
Hello, world!
The file Main.java
  • public class Main declares a class named Main. In Java all code is written inside classes, and the file must be named after the class: Main.java.
  • public static void main(String[] args) is the program's entry point: the JVM always starts with the main method. You will learn every word of this line in later lessons; for now, just remember it as it is.
  • System.out.println(...) prints the value in the parentheses and then moves to a new line.
  • Curly braces { } group blocks of code, and every statement ends with a semicolon ;.

Printing to the screen

println moves to a new line every time, while print stays on the same line. Text in double quotes is called a string. 7 + 8 written without quotes is an expression that Java calculates. When + is used with a string, it joins pieces of text together.

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("My name is Murad.");
        System.out.print("I live in ");
        System.out.println("Baku.");
        System.out.println(2026);
        System.out.println(7 + 8);
        System.out.println("7 + 8 = " + (7 + 8));
        System.out.println("7 + 8 = " + 7 + 8);
    }
}
Expected output
My name is Murad.
I live in Baku.
2026
15
7 + 8 = 15
7 + 8 = 78

Comments and errors

Comments are notes for people reading the code; the compiler ignores them. // turns the rest of the line into a comment, and /* ... */ can cover several lines. A comment is also a handy way to “switch off” a statement for a while without deleting it.

Java
/* My first Java program.
   It prints two lines. */
public class Main {
    public static void main(String[] args) {
        // greeting
        System.out.println("Welcome to Educora!");
        // System.out.println("This line is switched off");
        System.out.println("Java is fun!"); // end of program
    }
}
Expected output
Welcome to Educora!
Java is fun!

Before Java code runs, the compiler checks it. If there is even a small mistake, the program will not start at all, and you will see a compile error. Don't worry: the message tells you which line has the problem and what is missing.

CodeCompiler messageWhy
System.out.println("Hi")';' expectedThe semicolon at the end is missing.
system.out.println("Hi");package system does not existJava is case-sensitive: System starts with a capital letter.
System.out.println("Hi);unclosed string literalThe closing quote is missing.
public class Main in the file Hello.javaclass Main is public, should be declared in a file named Main.javaThe file name must match the name of the public class.
The compile errors beginners meet most often
1 / 6

Key points

  • Java is a general-purpose, object-oriented language widely used for Android, servers and enterprise systems.
  • javac compiles code into bytecode, and the JVM runs that bytecode on any operating system.
  • To write programs you need the JDK, which also includes the JRE and the JVM.
  • A program starts in public static void main(String[] args); the public class name must match the file name.
  • println moves to a new line and print does not; every statement ends with ;, and Java is case-sensitive.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What does the JVM execute?