Ever wondered why a simple line like System.out.println("Hello"); can make your computer talk?

💡 In Simple Words: A Java program is a set of instructions wrapped in a specific format. Think of it as a recipe: you list ingredients (variables), steps (methods), and the kitchen rules (syntax) so the computer knows exactly what to do.

How to write a simple Java program

When you open a text editor and type a few lines, you’re not just scribbling – you’re following a tiny blueprint that Java understands. The blueprint has three main parts:

  • Class declaration – tells Java the name of the container for your code.
  • main method – the entry point where the program starts running. (A method is a named block of code that does something.)
  • Statements – the actual instructions, like printing text or doing math.

Step‑by‑step: From typing to seeing output

graph TD A[Write code] --> B[Save .java file] B --> C[Compile with javac] C --> D[Run with java] D --> E[See output]

Basic skeleton of a Java program

Here’s the smallest program that prints Hello, World! on the screen:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Let’s break it down, line by line.

LineWhat it does
1public class HelloWorld { – declares a class named HelloWorld. public means anyone can see it; class is a container for code.
2public static void main(String[] args) { – the main method. static lets Java call it without creating an object first. void means it returns nothing. String[] args is a place to receive command‑line words, but you can ignore it for now.
3System.out.println("Hello, World!"); – tells the system (your computer) to print the text inside the quotes. System.out is the console output stream, and println means “print line” (adds a new line after).
4} – closes the main method.
5} – closes the class.

Why the curly braces?

Curly braces { } are like the walls of a room. Everything inside belongs to that room. In our example, the first pair encloses the whole class, and the second pair encloses the main method.

Compiling and running the program

Writing code is only half the battle. The computer speaks a language called bytecode, not plain English. To translate, you use two commands:

  • javac HelloWorld.java – the compiler (javac) turns your .java file into a .class file.
  • java HelloWorld – the runtime (java) reads the .class file and executes the main method.

If you see Hello, World! on the terminal, you’ve just built and run your first Java program!

Common mistakes to avoid

  • Forgetting the .java extension when you save the file.
  • Mismatching the class name and file name (they must be identical, case‑sensitive).
  • Leaving out a semicolon ; at the end of a statement.
  • Using wrong braces – every opening { needs a matching closing }.

Quick reference table

ComponentPurposeTypical Syntax
ClassContainer for related codepublic class ClassName { … }
main methodProgram’s entry pointpublic static void main(String[] args) { … }
StatementAction the program performsSystem.out.println("text");
CommentNotes for humans, ignored by Java// single line comment

📝 Likely Exam Questions

  1. Write the complete structure of a Java program that prints your name.
    Answer: Provide a class named NamePrint with a main method that contains System.out.println("Your Name");.
  2. What is the role of the main method in a Java program?
    Answer: It is the starting point where the JVM (Java Virtual Machine) begins execution. Without it, the program won’t run.
  3. Explain why the class name and file name must match.
    Answer: The compiler looks for a public class that has the same name as the file. If they differ, compilation fails.
  4. Identify the error in the following snippet and correct it:
    public class Test {
        public static void main(String args) {
            System.out.println("Hi")
        }
    }
    
    Answer: String args should be String[] args, and a semicolon is missing after println("Hi"). Corrected line: System.out.println("Hi");
  5. List the four steps to run a Java program from source code.
    Answer: Write the code, save with .java extension, compile with javac, run with java.
#ICSE#Class 10#Java#Computer Applications#Programming Basics