No history yet

Java Basics

Setting Up Your Workspace

Before you can write Java, you need a few tools. Think of it like setting up a workshop. You need tools to build things, a space to work in, and a way to run what you've built.

ToolWhat It IsAnalogy
JDK (Java Development Kit)The compiler and other tools to turn your source code into a runnable program.The a full set of workshop tools.
JRE (Java Runtime Environment)The software that actually executes your compiled Java code.The power supply for your tools.
IDE (Integrated Development Environment)A smart text editor that helps you write, organize, and run your code.Your workbench and organizer.

The JDK includes the JRE, so you only need to install the JDK. For the IDE, popular choices include IntelliJ IDEA, Eclipse, and Visual Studio Code with Java extensions. Most modern IDEs will help you install a JDK, making the setup process straightforward.

Your First Java Program

Every Java program is built around at least one class. A class is like a blueprint for creating objects, but for now, just think of it as a container for your code. Let's look at the classic "Hello, World!" program.

public class HelloWorld {
    public static void main(String[] args) {
        // This line prints "Hello, World!" to the console.
        System.out.println("Hello, World!");
    }
}

Let's break that down. public class HelloWorld declares a class named HelloWorld. The file you save this in must be named HelloWorld.java to match. The curly braces {} define the scope of the class, meaning everything inside them belongs to the class.

The main method is the entry point for any Java application. When you run your program, the JRE looks for this specific method and starts executing the code inside it.

The line public static void main(String[] args) is the main method's signature. You don't need to memorize what every word means right now, but recognize this line as the starting point. The code to be executed goes inside its curly braces.

Input and Output

Programs aren't very useful if they can't communicate. In our "Hello, World!" example, we used System.out.println() to send a message to the console. The println part stands for "print line," and it automatically adds a new line after the message.

What about getting input from a user? For that, we can use a built-in Java class called Scanner. To use it, you first need to tell your program where to find it by adding import java.util.Scanner; at the very top of your file, before the public class line.

import java.util.Scanner; // Import the Scanner class

public class Greeter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("What is your name?");
        String name = scanner.nextLine(); // Read user input

        System.out.println("Hello, " + name + "!");
    }
}

Here, we create a Scanner object to read from the standard input (System.in), which is typically your keyboard. The scanner.nextLine() method waits for the user to type something and press Enter, then it returns the text as a string.

Java Syntax Rules

Every language has grammar rules, and Java is no different. Following its syntax rules is essential for writing code that the compiler can understand.

Semicolons: Every statement in Java must end with a semicolon ;. This tells the compiler where one instruction ends and the next begins.

Case-Sensitivity: Java is case-sensitive. This means myVariable is a different variable from MyVariable.

Comments: You can leave notes in your code that the compiler will ignore. Use // for a single-line comment, or wrap multi-line comments between /* and */.

Code Blocks: Curly braces {} are used to group statements into blocks. You've already seen them used for classes and methods.

There are also naming conventions, which are strong recommendations rather than strict rules. Class names should start with a capital letter (e.g., HelloWorld), while variable and method names should start with a lowercase letter (e.g., scanner, nextLine).

Now, let's test what you've learned.

Quiz Questions 1/6

To compile and run Java programs, which package is essential to install?

Quiz Questions 2/6

What is the primary purpose of the public static void main(String[] args) method in a Java application?

Getting these fundamentals down is the first big step. With these tools and rules, you're ready to start building more complex programs.