Ever wondered how the apps on your phone are built? Java is the secret sauce behind many of them.
💡 In Simple Words: Java is a language that lets you write instructions once and run them on almost any computer, just like a story that can be read in many languages without changing the plot.
What is Java and why should you care?
Java is a programming language that lets you tell a computer what to do, kind of like giving step‑by‑step instructions to a robot. It was created by Sun Microsystems in the mid‑1990s and is still everywhere – from Android phones to big‑bank servers.
How do you write a simple Java program?
Think of a Java program as a recipe. You write the ingredients (variables) and the steps (methods) inside a special container called a class. A class is just a blueprint, like a Lego set that tells you how pieces fit together.
Step‑by‑step example
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, ICSE 9!");
}
}
Explanation of the first time terms:
- public: a keyword that says anyone can see this class.
- class: the blueprint that holds code.
- static: tells Java you don’t need to create an object to run this method.
- void: means the method doesn’t return any value.
- String[] args: a way to pass extra information to the program when it starts.
When you run this code, the computer prints “Hello, ICSE 9!” on the screen.
Compiling and running Java – the three‑step cycle
Java code isn’t understood directly by the computer. First it gets turned into an intermediate language called bytecode (think of it as a secret code that the Java engine can read). Then the Java engine turns that bytecode into actions your computer understands.
Key features of Java (quick comparison)
| Feature | What it means for you |
|---|---|
| Platform‑independent | Write code once, run it on Windows, Mac or Linux without changes. |
| Object‑oriented | Organise code into “objects” that bundle data and actions, like a real‑world toy. |
| Strongly typed | The computer checks the kind of data you use, reducing hidden bugs. |
| Rich standard library | Ready‑made tools for maths, text, files, and more – you don’t have to build everything from scratch. |
Common mistakes beginners make
- Forgetting the
public static void main(String[] args)line – without it the program won’t start. - Using a file name that doesn’t match the class name – Java is picky about that.
- Skipping the semicolon (;) at the end of a statement – it’s like forgetting a period in a sentence.
- Mixing tabs and spaces for indentation – it works, but looks messy and can cause compile errors in some IDEs.
How to test your code without an IDE
You can use the command line that comes with the JDK (Java Development Kit). Open a terminal, navigate to the folder where you saved HelloWorld.java, then type:
javac HelloWorld.java // compiles
java HelloWorld // runs
If everything is correct, you’ll see the greeting printed.
📝 Likely Exam Questions
- What is a class in Java? A class is a blueprint that groups variables (data) and methods (behaviour) together, allowing you to create objects that follow that blueprint.
- Write the smallest valid Java program that prints your name.
public class MyName { public static void main(String[] args) { System.out.println("YourName"); } } - Explain the purpose of the
mainmethod. Themainmethod is the entry point; when you run a Java program, the JVM (Java Virtual Machine) looks for this method to start execution. - List two advantages of Java being platform‑independent. (i) The same .class file runs on any operating system with a JVM. (ii) You don’t need to rewrite code for different devices, saving time and effort.
- What does the
System.out.printlnstatement do? It sends the specified text to the standard output stream, usually the console, and then moves the cursor to a new line.