No history yet

Introduction to Java

The Building Blocks of Java

Java is a programming language, which is just a structured way to give instructions to a computer. Think of it like a recipe. Each step must be written in a specific way for the chef—in this case, the computer—to understand. This set of rules is called syntax.

Let's look at the simplest Java program, the classic "Hello, World!".

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

This might look complicated, but let's break it down.

  • public class HelloWorld { ... } defines a class, which is like a blueprint for our program. Everything in Java lives inside a class.
  • public static void main(String[] args) { ... } is the main entry point. When you run the program, the computer looks for this exact main method and starts executing the code inside it.
  • System.out.println("Hello, World!"); is the instruction that prints the text to the screen. Notice it ends with a semicolon ;. Most lines of code in Java do.

Pay close attention to the syntax: the curly braces {} group code together, and the semicolon ; marks the end of a statement. Missing either will cause an error.

Storing Information

Programs need to store and manage information. We do this using variables. A variable is like a labeled box where you can keep a piece of data. Before you can use a box, you have to tell Java what kind of data you plan to put in it. This is called the variable's data type.

Java has several built-in data types, often called primitive types. Here are the most common ones you'll use.

Data TypeDescriptionExample
intStores whole numbersint age = 30;
doubleStores decimal numbersdouble price = 19.99;
booleanStores true or false valuesboolean isLoggedIn = true;
StringStores sequences of charactersString name = "Alex";

String is technically not a primitive type—it's a class—but it's so fundamental that we often treat it like one. Here’s how you declare and use variables in code:

public class UserProfile {
    public static void main(String[] args) {
        String name = "Maria";
        int age = 28;
        double accountBalance = 150.75;
        boolean isSubscribed = false;

        System.out.println(name);
        System.out.println(age);
    }
}

Making Decisions and Repeating Actions

A program that just runs from top to bottom isn't very smart. To create useful applications, we need code that can react to different situations and perform repetitive tasks. That's where control structures come in.

Conditional statements allow your program to make choices. The most common is the if-else statement. It checks if a condition is true and runs a block of code accordingly.

int score = 85;

if (score >= 60) {
    System.out.println("You passed!");
} else {
    System.out.println("You need to study more.");
}

Loops, on the other hand, are for repeating actions. The for loop is great when you know exactly how many times you want to repeat something.

// This loop prints the numbers 1 through 5.
for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

The while loop is useful when you want to repeat an action as long as a certain condition is true, but you don't know how many times that will be.

int batteryLevel = 100;

while (batteryLevel > 0) {
    System.out.println("Using device...");
    batteryLevel = batteryLevel - 10;
}

System.out.println("Battery dead!");

A Glimpse into Object-Oriented Programming

Java is an object-oriented programming (OOP) language. This sounds intimidating, but the core idea is simple: we can model real-world things as objects in our code. An object bundles together related data (properties) and behaviors (methods).

The blueprint for an object is called a class. Let's create a simple Dog class.

// This is the blueprint for creating Dog objects.
public class Dog {
    // Properties (data)
    String name;
    String breed;

    // Method (behavior)
    public void bark() {
        System.out.println("Woof!");
    }
}

This Dog class defines that any dog we create will have a name and a breed. It also has a behavior: it can bark().

Now we can use this class to create actual Dog objects, which are called instances of the class.

public class Kennel {
    public static void main(String[] args) {
        // Create an instance of the Dog class
        Dog myDog = new Dog();

        // Set its properties
        myDog.name = "Fido";
        myDog.breed = "Golden Retriever";

        // Call its method
        myDog.bark(); // This will print "Woof!"
    }
}

Object-oriented programming helps organize complex programs by grouping related data and behaviors into neat, reusable packages called objects.

Time to check your understanding of these core concepts.

Quiz Questions 1/5

What is the primary purpose of a 'class' in object-oriented programming with Java?

Quiz Questions 2/5

In a Java application, what is the exact signature of the method that serves as the main entry point for execution?

These are the absolute fundamentals of Java. By combining variables, control structures, and simple objects, you can start building powerful and interesting programs.