First Java program
What you will learn
Java is a general-purpose programming language. A program is a set of instructions that a computer can execute. Start by writing a program that prints a message, then change it to calculate a useful result.
public class Main { public static void main(String[] args) { System.out.println("Welcome to the library!"); }}Expected output:
Welcome to the library!The quoted text is passed to the printing operation; the quotation marks are not printed.
How it works
Save the complete example as Main.java. The conventional public class Main groups the program, and public static void main(String[] args) is its entry point on the Java 21 compatibility lane. For now, keep that line unchanged. Braces mark the program body; each print statement ends with a semicolon. Compile with javac Main.java and launch with java Main.
public class Main { public static void main(String[] args) { System.out.println(4 + 3); }}Expected output:
7Java evaluates the addition before printing the result.
Common mistakes
Java is case-sensitive: system is not System. The file name for this public class must be Main.java, including its capitalization.
Try it yourself
Change the greeting to introduce a museum. Then print the sum of 12 adult tickets and 8 child tickets.
These are local Java practice tasks, not automatically graded ByteMind exercises. Use a patched JDK; no network or third-party dependency is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
First Java program — knowledge check
Check your understanding of first java program.
What is printed by `System.out.println(4 + 3);`?
Which command launches the compiled Main class?