Writing your first Java program helps you learn how to tell the computer what to do using Java language. It shows you the basic structure of a Java program.
Writing first Java program
public class ClassName { public static void main(String[] args) { System.out.println("Your message here"); } }
public class ClassName defines the program's name and container.
main method is where the program starts running.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }
public class Greeting { public static void main(String[] args) { System.out.println("Good morning!"); } }
This program prints a welcome message to the screen. It shows the basic parts of a Java program: class, main method, and printing text.
public class FirstProgram { public static void main(String[] args) { System.out.println("Welcome to Java programming!"); } }
Make sure the file name matches the class name exactly (FirstProgram.java for class FirstProgram).
Use a Java compiler like javac to turn your code into something the computer can run.
Run the program using java ClassName command after compiling.
Java programs start with a class and a main method.
Use System.out.println() to print messages on the screen.
File name must match the class name for the program to run correctly.
Practice
main method in a Java program?Solution
Step 1: Understand the role of the main method
Themainmethod is special because Java looks for it to start running the program.Step 2: Identify the program entry point
Without themainmethod, the program has no defined starting point to execute code.Final Answer:
It is the starting point where the program begins execution. -> Option AQuick Check:
Main method = program start [OK]
- Thinking main defines a class
- Confusing main with variable declaration
- Assuming main imports libraries
Solution
Step 1: Identify the correct print method in Java
Java usesSystem.out.println()to print text with a new line.Step 2: Check each option for syntax correctness
System.out.print("Hello, World!"); usesprintwhich prints without a new line, but is valid. System.out.println("Hello, World!"); usesprintlnwhich prints with a new line and is the common way. print("Hello, World!"); and D are not valid Java syntax.Final Answer:
System.out.println("Hello, World!"); -> Option CQuick Check:
Print line = System.out.println() [OK]
- Using print without System.out
- Using non-Java print commands
- Missing quotes around text
public class Hello {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}Solution
Step 1: Read the print statement inside main
The program prints the exact string "Welcome to Java!" using System.out.println.Step 2: Understand println adds a new line but does not show \n literally
The output shows the text and moves to the next line, but the new line is not visible as characters.Final Answer:
Welcome to Java! -> Option DQuick Check:
Print output = Welcome to Java! [OK]
- Confusing output with class name
- Expecting \n characters in output
- Assuming syntax error without reason
public class Test {
public static void main(String[] args) {
System.out.println("Hello Java")
}
}Solution
Step 1: Check syntax of print statement
The print statement is missing a semicolon at the end, which is required in Java.Step 2: Verify other parts for correctness
Class name capitalization is allowed, main method signature is correct, and System.out.println is the right method.Final Answer:
Missing semicolon after the print statement. -> Option BQuick Check:
Semicolon missing = syntax error [OK]
- Omitting semicolon at line end
- Changing class name case unnecessarily
- Incorrect main method signature
Solution
Step 1: Understand Java file and class naming rules
The file name must exactly match the public class name including case, with .java extension.Step 2: Check each option for matching names and extension
File name: GoodMorning.java, Class name: GoodMorning matches file and class names exactly with correct extension. Options B and C have case mismatches. File name: GoodMorning.txt, Class name: GoodMorning has wrong file extension.Final Answer:
File name: GoodMorning.java, Class name: GoodMorning -> Option AQuick Check:
File name = Class name + .java [OK]
- Mismatching class and file names
- Using wrong file extension
- Ignoring case sensitivity
Ready to prove it?
Pass the test and this pattern moves unknown
