Java Installation
Some PCs might already have Java installed. To check if you have Java installed on a Windows PC, search in the start menu for Java or type the following in Command Prompt (cmd.exe):
C:\Users\YourName>java -version
If Java is installed, you will see something like this (depending on the version):
java version "22.0.0" 2024-08-21 LTS Java(TM) SE Runtime Environment 22.9 (build 22.0.0+13-LTS) Java HotSpot(TM) 64-Bit Server VM 22.9 (build 22.0.0+13-LTS, mixed mode)
If you do not have Java installed on your computer, you can download it for free at oracle.com.
Note: In this tutorial, we will write Java code in a text editor. However, you can also use an Integrated Development Environment (IDE) like IntelliJ IDEA, NetBeans, or Eclipse, which are particularly useful when managing larger collections of Java files.
Java Quickstart
In Java, every application begins with a class name, and that class must match the filename.
Let’s create our first Java file called WelcomeApp.java
, which can be done in any text editor (like Notepad).
The file should contain a “Hello, Java Universe!” message, which is written with the following code:
WelcomeApp.java public class WelcomeApp { public static void main(String[] args) { System.out.println("Hello, Java Universe!"); } }
Don’t worry if you don’t understand the code above – we will discuss it in detail in later chapters. For now, focus on how to run the code.
- Save the Code: Save the code in Notepad as
WelcomeApp.java
. - Compile the Code: Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type
javac WelcomeApp.java
:C:\Users\YourName>javac WelcomeApp.java
This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. - Run the Code: Now, type
java WelcomeApp
to run the file:C:\Users\YourName>java WelcomeApp
The output should read:Hello, Java Universe!
Example Explained
- Class Declaration: Every line of code in Java must be inside a class. The class name should always start with an uppercase first letter. In our example, we named the class
WelcomeApp
. Note: Java is case-sensitive: “MyClass” and “myclass” have different meanings. - File Name and Class Name: The name of the Java file must match the class name. When saving the file, save it using the class name and add “.java” to the end of the filename.
- The
main
Method: Themain()
method is required and you will see it in every Java program:public static void main(String[] args)
Any code inside themain()
method will be executed. This is where the program begins execution. System.out.println()
: Inside themain()
method, we use theprintln()
method to print a line of text to the screen:public static void main(String[] args) { System.out.println("Hello, Java Universe!"); }
System
is a built-in Java class that contains useful members, such asout
, which is short for “output”. Theprintln()
method, short for “print line”, is used to print a value to the screen (or a file). Note: Each code statement in Java must end with a semicolon(;).