- 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
mainmethod and print output withprintln
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.
- 1Write the code
Save the program in
Main.java. The file name must match the name of thepublicclass. - 2Compile
Type
javac Main.javain the terminal: the compiler checks the code for errors and creates the bytecode fileMain.class. - 3Run
The command
java Mainstarts the JVM, which executes the bytecode.
| Term | Stands for | What it is for |
|---|---|---|
| JVM | Java Virtual Machine | executes bytecode |
| JRE | Java Runtime Environment | JVM + standard libraries: for running finished programs |
| JDK | Java Development Kit | JRE + the javac compiler and other tools: for writing programs |
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:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}Hello, world!
public class Maindeclares a class namedMain. 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 themainmethod. 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.
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);
}
}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.
/* 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
}
}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.
| Code | Compiler message | Why |
|---|---|---|
System.out.println("Hi") | ';' expected | The semicolon at the end is missing. |
system.out.println("Hi"); | package system does not exist | Java is case-sensitive: System starts with a capital letter. |
System.out.println("Hi); | unclosed string literal | The closing quote is missing. |
public class Main in the file Hello.java | class Main is public, should be declared in a file named Main.java | The file name must match the name of the public class. |
Key points
- Java is a general-purpose, object-oriented language widely used for Android, servers and enterprise systems.
javaccompiles 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. printlnmoves to a new line andprintdoes not; every statement ends with;, and Java is case-sensitive.
Check yourself
10 questions. Every correct answer earns XP.