Introduction to Java
By the Syncfusion DevTutorials team · Last updated
Learn what Java is, where it came from, the JVM, JRE and JDK, how to install Java, and how a Java program is written, compiled and run.
Every software application is built from instructions that tell a computer what to do. Java is one of the programming languages developers use to write those instructions.
Developers use Java to build many kinds of software, including web applications, business systems, desktop tools, cloud services, and Android applications.
Java is designed to work across different supported systems. You will learn how Java makes this possible later in this lesson.
What You Will Learn
In this lesson, you will learn:
- What Java is and where it came from.
- The main features that make Java popular.
- The three Java editions: Java SE, Jakarta EE and Java ME.
- What the JVM, the JRE and the JDK are, and how they differ.
- How to install Java on your computer.
- How to write, compile and run your first Java program.
- How a Java program is built, and why every program needs a
main()method. - Where Java is used in the real world.
What Is Java?
Java is both a programming language and a software platform.
The Java language provides the words, syntax, and rules used to write Java programs.
The Java platform provides the environment and tools needed to compile and run those programs.
In simple words: the Java language is used to write the program, while the Java platform helps build and run it.
Start With a Working Example
Press Run. The first run on a page takes a few seconds, because the Java compiler loads in your browser. Every run after that is quick.
Your first Java program prints one line
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Expected output:
Hello, Java!
The program prints one line of text.
Do not worry about every word yet. By the end of this lesson, you will know what each part does.
Try This
Change Hello, Java! to your own name. Keep the quotation marks. Press Run again.
Write Once, Run Anywhere
Bytecode is not created for a specific operating system. It is run by the Java Virtual Machine (JVM), which is software that understands and executes bytecode on a computer.
This means the same compiled Java program can run on Windows, macOS, or Linux without being compiled separately for each operating system, as long as a compatible JVM is available on that system.
This approach is known as Write Once, Run Anywhere.
Java Is General Purpose
Java is a general-purpose programming language, which means developers can use it to create different kinds of software.
Java is commonly used for:
- Web applications and services
- Desktop applications
- Android applications
- Large business systems
- Cloud services
Java and JavaScript are two different languages. They share part of a name, but they have different rules and different uses.
History of Java
Java began in 1991 as a project at Sun Microsystems, led by James Gosling. The language was first called Oak and was designed for electronic devices that used different types of hardware.
Oak was later renamed Java and introduced publicly in 1995. The first official version, JDK 1.0, was released in 1996.
Sun Microsystems later began making Java open source through OpenJDK. Oracle acquired Sun Microsystems in 2010, and Java continues to be developed with contributions from the wider Java community.
Features of Java
Java provides several features that help developers build reliable applications.
Simple
Java uses readable keywords and handles tasks such as memory management automatically.
Object-Oriented
Java helps organise related data and actions using classes and objects. You will learn these concepts in detail later.
Platform Independent
Java programs are compiled into bytecode, which can run on different supported systems using a compatible JVM. This feature was explained in the Write Once, Run Anywhere section.
Strongly Typed
Every value in Java has a type. Java checks these types during compilation, which helps identify many mistakes before the program runs.
Secure
The JVM checks bytecode before running it and provides features that support safer program execution.
Robust
Java provides automatic memory management and exception handling to help programs manage resources and respond to problems.
Multithreaded
Java supports multiple threads, allowing a program to manage more than one task.
High Performance
The JVM uses a JIT compiler to optimise frequently used code while the program runs.
Java Editions
Java comes in editions. An edition is a set of tools and libraries for one kind of program.
Think of a toolbox. A home toolbox has basic tools. A builder's toolbox has those same basics, plus heavy tools for big jobs.
All Java editions share the same core language. They differ in the extra tools they add.
Java SE
Java SE stands for Java Standard Edition.
It is the core of Java. It includes the language, the JVM and the basic libraries for text, numbers, files, lists and networking.
Use Java SE for desktop apps, command-line tools and learning Java. This course uses Java SE.
Jakarta EE
Jakarta EE is the edition for large business applications. It was called Java EE until 2018.
It is built on top of Java SE. It adds tools for web servers, databases, messaging and security.
Use Jakarta EE for banking systems, online shops and other large web applications.
Java ME
Java ME stands for Java Micro Edition.
It is a small version of Java for devices with little memory. Examples include old mobile phones, smart cards and small sensors.
Java ME is used much less today. Modern phones run Android instead.
Comparing the Editions
| Edition | Full name | Used for |
|---|---|---|
| Java SE | Java Standard Edition | Core Java, desktop apps, tools, learning |
| Jakarta EE | Jakarta Enterprise Edition (formerly Java EE) | Large web and business applications |
| Java ME | Java Micro Edition | Small devices with little memory |
In simple words: SE is the base, EE adds business tools on top, and ME is a cut-down version for small devices.
Understanding the JVM, JRE, and JDK
Think about creating and playing a video.
A video editor needs special tools to create and prepare the video. After the video is ready, a media player is needed to play it. The device then handles the instructions required to show the video.
Java works in a similar way:
- The JDK provides the tools used to create Java programs.
- The JRE provides the environment needed to run Java programs.
- The JVM runs the compiled program instructions.
JVM (Java Virtual Machine)
Before a Java program can run, the Java compiler converts it into instructions called bytecode.
The JVM, or Java Virtual Machine, reads the bytecode and runs the program on the computer.
Different operating systems use compatible JVM implementations. This allows the same Java bytecode to run on different supported systems.
In simple words: the JVM runs the compiled Java program.
What the JVM Does
Think of a translator at a meeting. The speaker talks in one language. The translator repeats each sentence in the listener's language.
The JVM is that translator. Your program speaks bytecode. The JVM turns it into instructions your computer understands.
Inside the JVM
The JVM does three main jobs:
- It loads the compiled
.classfiles needed by the program. - It checks that the bytecode follows the JVM’s rules.
- It runs the bytecode.
One JVM Per System
Each operating system has its own JVM. Windows has one, macOS has one, and Linux has one.
Your bytecode stays the same. Only the JVM changes.
This is why the same .class file runs everywhere.
JRE (Java Runtime Environment)
The Java Runtime Environment, provides the environment needed to run a Java program.
It includes:
- The JVM, which runs the compiled Java program
- Core Java libraries, which provide ready-made features that programs can use while running
The JRE is meant for running Java programs. It does not include development tools such as the Java compiler.
Most modern Java installations do not provide the JRE as a separate download. Developers usually install the JDK, which includes the runtime components needed to run Java programs.
JDK (Java Development Kit)
The JDK, or Java Development Kit, provides the tools needed to create and run Java programs.
It includes:
- The runtime components needed to run Java programs
javac, the compiler that converts Java source code into bytecodejava, the launcher that starts a Java program- Other development tools, such as
javadocfor creating documentation andjarfor packaging Java files
As a Java developer, you install a JDK because it allows you to compile, run, and package your programs.
In simple words: the JDK is the toolkit used to build and run Java programs.
JDK vs JRE vs JVM
These three names confuse almost every beginner. Here is the simple picture.
Think of boxes inside boxes:
+-------------------------------------------+
| JDK (build + run) |
| javac, jar, javadoc ... |
| +-----------------------------------+ |
| | JRE (run only) | |
| | core libraries | |
| | +---------------------------+ | |
| | | JVM (runs bytecode) | | |
| | +---------------------------+ | |
| +-----------------------------------+ |
+-------------------------------------------+
The JDK holds the JRE. The JRE holds the JVM.
| JVM | JRE | JDK | |
|---|---|---|---|
| Full name | Java Virtual Machine | Java Runtime Environment | Java Development Kit |
| Main job | Runs bytecode | Runs Java programs | Builds and runs Java programs |
| Contains | Loader, checker, JIT compiler | JVM + core libraries | JRE + compiler + tools |
Has the javac compiler? | No | No | Yes |
| Who needs it? | Every Java program | People who only run Java apps | Developers |
Easy Way to Remember: JVM runs it. JRE is the room it runs in. JDK is the workshop where you build it.
Installing Java
You do not need to install Java to complete the examples in this course. The Run button uses a Java compiler inside your browser.
To create Java projects on your own computer, you need to install a JDK.
Step 1: Choose a JDK
A JDK distribution is a ready-to-install version of the Java Development Kit.
Two commonly used distributions are:
- Eclipse Temurin
- Oracle JDK
Choose the JDK version required by your course or project. If no version is required, a supported LTS version is usually a practical choice.
LTS means Long-Term Support. An LTS version receives updates for a longer period than a regular feature release, although the exact support period depends on the JDK provider.
Step 2: Install It
Choose the installer that matches your operating system:
- Windows: Download the Windows installer and run it. If the installer provides options to update
PATHor setJAVA_HOME, enable them. - macOS: Download the macOS installer and follow the installation steps.
- Linux: Install the required JDK using the package manager provided by your Linux distribution.
The exact installer name and package command may change between Java versions, so follow the instructions provided by your chosen JDK distributor.
Step 3: Check the Installation
Open a terminal (Command Prompt or PowerShell on Windows) after the installation is complete. Type these two commands:
java -version
javac -version
The java command checks the Java launcher. The javac command checks the Java compiler.
If both commands display version information, the JDK is installed and ready to use.
What PATH and JAVA_HOME Mean
When you enter a command in the terminal, your computer uses PATH to find the program that should run.
The JDK contains a folder named bin, which includes commands such as java and javac. Adding this folder to PATH allows you to use these commands from any folder in the terminal.
JAVA_HOME stores the location where the JDK is installed. Development tools such as build systems and code editors may use this setting to locate Java.
If the terminal says that javac is not recognised, the JDK may not be installed, its bin folder may not be included in PATH, or the terminal may need to be reopened after installation.
First Java Program
Let us build the classic first program. It prints a greeting.
Hello, World! in Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("I am learning Java.");
}
}
Expected output:
Hello, World!
I am learning Java.
Each System.out.println line prints one line of text.
The lines run from top to bottom. So Hello, World! comes first.
println and print
System.out.println prints text and then moves to a new line.
System.out.print prints text and stays on the same line.
print stays on the line, println ends it
public class Main {
public static void main(String[] args) {
System.out.print("Java ");
System.out.print("is ");
System.out.println("fun.");
System.out.println("This starts on a new line.");
}
}
Expected output:
Java is fun.
This starts on a new line.
The three print and println calls build one line. The last println starts a new one.
Try This
Change the first print to println. Predict the output before you press Run.
Program Structure
Every Java program follows the same basic shape. Here is the first program again, with each part labelled.
public class Main { // 1. the class
public static void main(String[] args) { // 2. the main method
System.out.println("Hello, World!"); // 3. a statement
} // end of main
} // end of class
The Class
public class Main {
A class is a structure that holds related Java code. In this beginner program, the class contains the main() method and the instructions that the program runs.
In this line:
publicallows the class to be accessed from other code.classtells Java that you are declaring a class.Mainis the name of the class.- The opening curly brace
{marks the beginning of the class body. - The matching closing brace
}marks its end.
The File Name Rule
A public class must be saved in a file with the same name as the class.
Java is case-sensitive, so Main.java and main.java are different file names. If the public class name and file name do not match, the program will not compile.
Every runnable example in this course uses a class named Main because the browser compiler saves the code as Main.java. On your own computer, you can choose another class name, but the file name must match the public class name exactly.
Statements
A statement is an instruction that asks the program to perform an action.
System.out.println("Hello, World!");
This statement has three parts:
Systemis a built-in class.outprovides access to standard output, which is where the program displays text.println(...)prints the value inside the parentheses and then moves to a new line.
Many simple Java statements end with a semicolon ;. The semicolon tells Java where the statement ends.
Curly Braces
Curly braces { } mark the beginning and end of a block of code.
In the example program, one pair of curly braces surrounds the class, while another pair surrounds the main() method.
Every opening brace { needs a matching closing brace }. Missing or misplaced braces can prevent the program from compiling.
Java does not require indentation to understand the code, but consistent indentation makes each block easier to recognise and read.
Comments
A comment is a note for people. Java ignores it.
Comments explain the code but never run
public class Main {
public static void main(String[] args) {
// A single-line comment starts with two slashes.
System.out.println("Comments are skipped.");
/* A multi-line comment
can span several lines. */
System.out.println("Only these two lines print.");
}
}
Expected output:
Comments are skipped.
Only these two lines print.
Java skipped both comments. Only the two println statements ran.
Java Is Case Sensitive
Java treats capital and small letters as different.
System and system are two different names. Only System is correct.
main() Method
The main() method is where your program starts.
Think of the front door of a house. Visitors always come in through the front door. The JVM always comes into your program through main().
The main Method Line
public static void main(String[] args)
Each word has a job:
| Word | Meaning |
|---|---|
public | The JVM can call this method from outside the class |
static | The JVM can call it without creating an object first |
void | The method does not return a value |
main | The exact name the JVM looks for |
String[] args | A list of text values passed in when the program starts |
What args Holds
The args variable receives optional text values entered after the class name when the application starts.
java Main red blue
This command passes two values, "red" and "blue", to the application.
The course’s Run button starts the application without any extra values. Therefore, the following example reports that the number of arguments is 0.
args is empty when no extra words are typed
public class Main {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
}
}
Expected output:
Number of arguments: 0
Why the Exact Spelling Matters
The JVM searches for a method with this exact shape. If you change it, the program still compiles, but the JVM cannot find a starting point.
For example, public static void Main(String[] args) with a capital M is a different method. Running it gives:
Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)
In simple words: no correct main() method means no starting point, so nothing runs.
Compilation and Execution Process
A Java program goes through four stages before you see output.
Main.java --javac--> Main.class --java--> JVM --> Output
(source) (compiler) (bytecode) (launcher) (runs)
Think of writing a letter in English for a friend abroad. First, a translator turns it into a common language. Then your friend's local reader reads it out loud in their own language.
Your code is the letter. javac is the translator. Bytecode is the common language. The JVM is the local reader.
Source Code (.java)
Source code is the Java code written by the developer.
It is stored in a text file with the .java extension. For example, a public class named Main is saved in a file named Main.java.
Compiler (javac)
A compiler is a tool that checks source code and converts it into another form.
The Java compiler is called javac. To compile Main.java, run this command in a terminal:
javac Main.java
First, javac checks your code for mistakes. If it finds one, it stops and shows an error.
If there are no mistakes, it creates a new file called Main.class.
A missing semicolon stops the compiler
public class Main {
public static void main(String[] args) {
System.out.println("Hello")
}
}
The compiler reports that a ; is expected. No .class file is created, so nothing runs.
On your own computer, the message looks like this:
Main.java:3: error: ';' expected
System.out.println("Hello")
^
1 error
The message names the file, the line number (3) and the problem. Always read the first error first.
Bytecode (.class)
Bytecode is the compiled form of a Java program. It is stored in a .class file.
Bytecode is not the original Java source code, and it is not machine code created for one particular operating system. It contains instructions designed for the JVM.
You do not write bytecode yourself. The Java compiler creates it, and the JVM runs it.
For the Hello program, the main part looks like this:
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Hello, Java!
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
You will never need to write bytecode. Just know that it exists, and that it is the same on every computer.
JVM Execution
After compilation, run the program using the java command:
java Main
Use the class name Main, without adding .class.
The java command starts the JVM. The JVM loads the required class, finds the main() method, and begins executing the program.
The program then displays its output:
Hello, Java!
A Shortcut for Small Programs
For a small, single-file program, Java can compile and run the source file with one command:
java Main.java
Java compiles the source file in memory and runs it without saving a separate Main.class file beside it.
This is useful for practising and testing small programs. Larger Java projects usually use a build process that compiles their source files before running them.
The Whole Process at a Glance
| Stage | File or tool | What happens |
|---|---|---|
| 1. Source code | Main.java | You write the program |
| 2. Compiler | javac Main.java | Code is checked and translated |
| 3. Bytecode | Main.class | A file of JVM instructions is created |
| 4. JVM execution | java Main | The JVM loads, checks and runs the bytecode |
Why Learn Java?
Java is a useful language to learn because it is widely used, has a large development community, and provides tools for building many kinds of applications.
Widely Used
Java is used in many business and server applications, including banking, booking, retail, and government systems. Learning Java can prepare you to work with large, long-running software projects.
Large Community
Java has a large community of developers. When you face a problem, you can often find documentation, tutorials, and discussions that help you understand it.
Rich Collection of Libraries
A library is reusable code that helps you perform a task without building everything from the beginning.
Java provides libraries for working with files, collections, networks, databases, web applications, and many other areas.
Stable and Reliable
Java is often used for applications that need to run and receive updates for many years. Newer Java versions also aim to maintain compatibility with older Java programs whenever possible.
Builds Strong Programming Skills
Java’s type checking and structured syntax help you learn important programming concepts. Many of these concepts are also useful when learning other languages.
Applications of Java
Java is used in almost every area of software.
Web Applications
Many websites run Java on the server. The server is the computer that receives your request and sends back a page.
Popular tools include Spring Boot and Jakarta EE. Online shops, booking sites and banking portals often use them.
Desktop Applications
Java can build programs that run on your computer with windows and buttons.
Tools include JavaFX and Swing. Well-known Java desktop apps include the IntelliJ IDEA and Eclipse code editors.
Mobile Applications
Android apps were first written in Java. Many still are.
Today Google also recommends Kotlin for Android. Kotlin runs on the same Java platform, so Java knowledge carries over directly.
Enterprise Applications
Enterprise applications are large systems that run a whole business. Examples include payroll, stock control and banking systems.
Java is a top choice here, because it is stable, secure and handles heavy work well.
Cloud Applications
Many cloud services are built with Java. A cloud service runs on remote servers that you reach over the internet.
Frameworks like Spring Boot, Quarkus and Micronaut help build small, fast services for the cloud.
Where Java Is Used: Summary
| Area | Example uses | Common tools |
|---|---|---|
| Web applications | Online shops, booking sites | Spring Boot, Jakarta EE |
| Desktop applications | Code editors, office tools | JavaFX, Swing |
| Mobile applications | Android apps | Android SDK |
| Enterprise applications | Banking, payroll, stock control | Jakarta EE, Spring |
| Cloud applications | Small web services, APIs | Spring Boot, Quarkus, Micronaut |
Common mistakes
The following mistakes are common when writing and running your first Java programs. Each one stops the program from compiling or running, or prints something you did not expect.
| Symptom | Cause | Fix |
|---|---|---|
';' expected | A statement has no semicolon, as in System.out.println("Hello") | Add ; at the end of every statement |
package system does not exist | System was typed with a small s. Java is case sensitive | Type System with a capital S |
Main method not found in class Main | main is misspelled, has a capital M, or has the wrong shape | Use exactly public static void main(String[] args) |
class Main is public, should be declared in a file named Main.java | public class Main was saved in a file with a different name, such as Hello.java | Rename the file so it matches the class: Main.java |
javac is not recognized or javac: command not found | Only a JRE is installed, or the JDK bin folder is not on PATH | Install a JDK and add its bin folder to PATH |
Could not find or load main class Main.class | The file extension was typed after java, as in java Main.class | Run the class by its name: java Main |
reached end of file while parsing | A closing brace } is missing at the end of the method or class | Make sure every { has a matching } |
unclosed string literal | A string is missing its closing double quote | Put the text between two " marks: "Hello" |
| Several words print on separate lines instead of one | Each part uses println, which ends the line after printing | Use print for the parts that should stay on the same line |
| Many errors appear at once and the code looks fine | One early mistake confuses the compiler, and the later errors follow from it | Fix the first error the compiler reports, then compile again |
What to Remember
- Java is a language and a platform. You write a program once, and it runs anywhere Java is installed.
javacturnsMain.javainto bytecode inMain.class. The JVM runs that bytecode.- The JVM runs bytecode. The JRE is the JVM plus core libraries. The JDK is the JRE plus the compiler and tools.
- Every program starts at
public static void main(String[] args), and every statement ends with;. - Java SE is the core edition. Jakarta EE adds business tools. Java ME is for small devices.
Next: Variables and Data Types