In this lesson, we will write our first Java program, understand its structure, compile the program, and execute it to display output on the screen.
Write the following code in your editor:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Output:
Hello World!
This program displays the message Hello World! on the screen.
In Java, the class keyword is used to
declare a class.
public class Hello {
}
Here:
class declares a class.Hello is the class name.public makes the class accessible from other code.{ } define the class body.A Java application can contain one or more classes. The class containing the main method can serve as the program's starting point.
The main() method is the standard entry
point used to launch a simple Java application.
public static void main(String[] args) {
// Program statements
}
Meaning of each part:
public — allows the launcher to access the method.static — lets it be called without creating an object of the class.void — means the method does not return a value.main — the method name recognized as the entry point.String[] args — receives command-line arguments.
The System.out.println() statement displays
information and then moves to a new line.
System.out.println("Welcome to Java");
System.out.println("Welcome to Soopro Pathshala");
Output:
Welcome to Java
Welcome to Soopro Pathshala
Text displayed using quotation marks is called a string literal.
Most Java statements end with a semicolon
(;).
System.out.println("Hello");
System.out.println("Java Programming");
If you forget a required semicolon, the compiler may report a syntax error.
Curly braces { } group statements inside
classes and methods.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Every opening brace must have a matching closing brace.
Follow these steps:
Hello.java.public class Hello,
save it as Hello.java. The public class
name and filename must match, including capitalization.
Open a terminal or command prompt in the folder
containing Hello.java.
Run this command:
javac Hello.java
The javac compiler checks the source code
and, if compilation succeeds, generates
Hello.class.
If the terminal reports that javac is not
recognized, check your JDK installation and PATH
configuration.
After successful compilation, enter:
java Hello
Output:
Hello World!
The java command launches the compiled
class. Do not add .class to the class
name in this command.
Java provides both print() and
println() for displaying output.
Example using print():
System.out.print("Hello ");
System.out.print("Java");
Output:
Hello Java
Example using println():
System.out.println("Hello");
System.out.println("Java");
Output:
Hello
Java
print() does not automatically move to
a new line. println() does.
Java can also display numbers and calculation results.
public class Addition {
public static void main(String[] args) {
System.out.println(10);
System.out.println(10 + 20);
}
}
Output:
10
30
Numbers do not need quotation marks when you want Java to treat them as numeric values.
If you write "10" + "20", Java joins the
two strings and produces 1020.
| Error | Correction |
|---|---|
| Missing semicolon | Add ; at the end of the statement. |
| Wrong filename | Match the filename with the public class name. |
| Incorrect capitalization | Write Java keywords and identifiers with correct capitalization. |
| Missing curly brace | Check that every opening brace has a closing brace. |
| Wrong command | Use javac Hello.java to compile and java Hello to run. |
Practice Task: Write a Java program that displays your name, your institute's name, and your favorite programming language.
public class MyDetails {
public static void main(String[] args) {
System.out.println("My Name: Rahul");
System.out.println("Institute: Soopro Pathshala");
System.out.println("Favorite Language: Java");
}
}
Possible output:
My Name: Rahul
Institute: Soopro Pathshala
Favorite Language: Java
Summary:
System.out.println() displays output on a new line.javac and run with java.1. Which method is commonly used as the starting point of a Java application?
2. Which command compiles Hello.java?
3. What is the output of System.out.println(10 + 20);?