Introduction
Introduction
Introduction
In this lesson, you'll learn about introduction in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with introduction.
Java has its own approach to introduction, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Java: every program lives inside a class
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Comparing to Python
Here's how you might have written similar code in Python:
# Python: run directly, no boilerplate
print("Hello, World!")
# Entry point convention
if __name__ == "__main__":
print("Running as main script")You may be used to different syntax or behavior.
Python uses indentation for blocks; Java uses braces and semicolons
You may be used to different syntax or behavior.
Java requires a main() method as the program entry point
You may be used to different syntax or behavior.
The Java file name must match the public class name exactly
You may be used to different syntax or behavior.
Java is compiled to bytecode; Python is interpreted line by line
You may be used to different syntax or behavior.
Java has no interactive REPL used in production
Step-by-Step Breakdown
1. Class Wrapper Required
Every Java program must be inside a class. Python code can live at the top level of a file.
print("Hello")public class Main {
// all code lives here
}2. The main Method
Java's entry point is always public static void main(String[] args). Python uses the if __name__ == '__main__' convention.
if __name__ == "__main__":
print("Hello")public static void main(String[] args) {
System.out.println("Hello");
}3. Printing Output
Python's print() becomes System.out.println() in Java.
print("Hello, World!")System.out.println("Hello, World!");4. Compilation vs Interpretation
Python runs directly with 'python main.py'. Java must first be compiled with 'javac Main.java', then run with 'java Main'.
# python main.py// javac Main.java
// java MainCommon Mistakes
When coming from Python, developers often make these mistakes:
- Python uses indentation for blocks; Java uses braces and semicolons
- Java requires a main() method as the program entry point
- The Java file name must match the public class name exactly
Key Takeaways
- Every Java file needs a public class matching the filename
- main() is mandatory; Python uses if __name__ == '__main__'
- Compile with javac, run with java