Writing first Java program - Time & Space Complexity
When writing your first Java program, it's helpful to know how the program's steps grow as you add more instructions or data.
We want to see how the time to run changes when the program gets bigger.
Analyze the time complexity of the following code snippet.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
This program prints a simple greeting message once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing one message to the screen.
- How many times: Exactly once.
Since the program prints only one message, the time it takes does not change with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps stays the same no matter how big the input is.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "The program takes longer if I run it on a bigger computer or with more data."
[OK] Correct: This program always does just one print, so its time does not depend on input size.
Understanding that some programs run in constant time helps you explain how simple tasks behave, which is a great foundation for learning more complex code.
"What if we added a loop to print the message multiple times? How would the time complexity change?"
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
