Java - Hello World Program

Welcome, future programmers! Today, we're embarking on an exciting journey into the world of Java programming. I remember my first "Hello World" program - it was like magic, seeing those words appear on the screen. Let's create that magic together!

Java - Hello World Program

What is Java?

Before we dive in, let's briefly talk about Java. Java is a popular, versatile programming language used for developing all kinds of applications, from mobile apps to web services. It's known for its "write once, run anywhere" philosophy, meaning you can run Java programs on any device that supports Java.

Your First Java Program: Hello World

The "Hello World" program is a rite of passage for every programmer. It's simple, yet it teaches us fundamental concepts. Let's get started!

Steps to Write, Save, and Run Hello World Program

  1. Write the Code: Open a text editor (like Notepad on Windows or TextEdit on Mac) and type the following code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  1. Save the File: Save this file as HelloWorld.java. The filename must match the class name and have a .java extension.

  2. Compile the Code: Open a command prompt or terminal, navigate to the directory where you saved the file, and type:

    javac HelloWorld.java
  3. Run the Program: If there are no errors, type:

    java HelloWorld

You should see "Hello, World!" printed on your screen. Congratulations! You've just run your first Java program!

Explanation of Hello World Program

Let's break down this program line by line:

public class HelloWorld {

This line declares a public class named HelloWorld. In Java, every program must have at least one class, and the class name should match the filename.

    public static void main(String[] args) {

This is the main method. It's the entry point of our Java program. When you run a Java program, it starts executing from this method.

        System.out.println("Hello, World!");

This line prints "Hello, World!" to the console. System.out is an object that represents the console output, and println is a method that prints a line of text.

    }
}

These closing braces end the main method and the class, respectively.

Java Control Statements

Now that we've got our feet wet, let's explore some basic control statements in Java. These are the building blocks of programming logic.

If-Else Statement

The if-else statement allows you to make decisions in your code. Here's an example:

int age = 18;
if (age >= 18) {
    System.out.println("You're an adult!");
} else {
    System.out.println("You're a minor.");
}

This code checks if the age is 18 or older. If it is, it prints "You're an adult!". Otherwise, it prints "You're a minor."

For Loop

Loops allow you to repeat actions. The for loop is commonly used when you know how many times you want to repeat something:

for (int i = 1; i <= 5; i++) {
    System.out.println("Count: " + i);
}

This loop will print numbers from 1 to 5.

While Loop

The while loop repeats an action as long as a condition is true:

int count = 0;
while (count < 5) {
    System.out.println("Count: " + count);
    count++;
}

This loop also prints numbers from 0 to 4.

Object Oriented Programming

Java is an object-oriented programming (OOP) language. This means it's based on the concept of "objects" that contain data and code. Let's create a simple class to demonstrate:

public class Dog {
    String name;
    int age;

    public void bark() {
        System.out.println(name + " says Woof!");
    }
}

This Dog class has two attributes (name and age) and a method (bark()). We can create and use a Dog object like this:

Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.age = 3;
myDog.bark();  // Outputs: Buddy says Woof!

Java Built-in Classes

Java provides many built-in classes that make programming easier. Let's look at a few:

String Class

The String class is used to work with text:

String greeting = "Hello, Java!";
System.out.println(greeting.length());  // Outputs: 12
System.out.println(greeting.toUpperCase());  // Outputs: HELLO, JAVA!

ArrayList Class

ArrayList is a dynamic array that can grow and shrink:

import java.util.ArrayList;

ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits);  // Outputs: [Apple, Banana]

Conclusion

We've just scratched the surface of Java programming. Remember, learning to code is like learning a new language - it takes time and practice. Don't get discouraged if something doesn't make sense right away. Keep coding, keep experimenting, and most importantly, have fun!

In future lessons, we'll dive deeper into topics like file handling, error handling, multithreading, and more. Until then, try modifying the Hello World program. Can you make it print your name? Or a favorite quote? The possibilities are endless!

Happy coding, future Java masters!

Credits: Image by storyset