Advertisements

This article, is designed to help newcomers to programming take their first steps in Java. It provides a clear, step-by-step explanation of how to create and run a simple Java program that outputs “Hello World” to the console. The guide includes an introduction to Java, an explanation of basic syntax, and instructions for setting up a development environment. By the end of the article, readers will have a solid understanding of how Java works and will feel confident running their first program.

Setting Up Your Java Environment

Before writing your first Java program, you need to set up the required tools and environment. Here’s how:

Install Java Development Kit (JDK):

Visit the Oracle JDK or OpenJDK website.

Download and install the appropriate version for your operating system.

Install an Integrated Development Environment (IDE):

Popular IDEs for Java include IntelliJ IDEA, Eclipse, and NetBeans. For beginners, IntelliJ IDEA Community Edition is a great choice.

Download and install the IDE from its official website.

Set Up the Environment Variable (if needed):

Advertisements

Download the free ebook

Add the JDK bin folder to your system’s PATH variable.

On Windows, this is done via System Properties > Advanced > Environment Variables.

Verify Installation:

Open your terminal or command prompt and type java -version and javac -version to check if Java is installed correctly.

Once your environment is ready, you’re all set to write and run Java programs!

Understanding the Hello World Program

The “Hello World” program is the simplest Java application. Here’s the complete code:

public class HelloWorld {

  public static void main(String[] args) {

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

  }

}

Breaking It Down

public class HelloWorld:

Every Java program starts with a class declaration. Here, HelloWorld is the name of the class.

The class name should match the filename (e.g., HelloWorld.java).

public static void main(String[] args):

Advertisements

This is the entry point of any Java application.

public means the method is accessible from outside the class.

static means the method can be run without creating an instance of the class.

void indicates the method does not return any value.

String[] args is used to pass command-line arguments to the program.

System.out.println(“Hello, World!”);:

This line prints the text “Hello, World!” to the console.

System.out is a standard output stream, and println prints the text followed by a new line.

Running the Program

Save the code in a file named HelloWorld.java.

Open a terminal or command prompt, navigate to the file’s directory, and compile it with:

javac HelloWorld.java

Affiliate Link for This Product

Run the compiled program with:

java HelloWorld

You should see:

Hello, World!

Congratulations, you’ve written and executed your first Java program!

Expanding Beyond Hello World

Mastering the “Hello World” program is an excellent starting point, but there’s so much more to learn in Java. In this article, we’ll explore the next steps in your programming journey, including understanding variables, loops, and building your first project.

Variables: Storing Data

Variables are used to store data in a program. In Java, you must declare a variable with a specific data type. Here’s an example:

public class VariablesExample {

  public static void main(String[] args) {

  int age = 25; // Declaring an integer variable

  double price = 19.99; // Declaring a double variable

  String name = "John"; // Declaring a String variable

  System.out.println("Name: " + name);

  System.out.println("Age: " + age);

  System.out.println("Price: $" + price);

  }

}

Key Points:

Primitive Types: int, double, boolean, char, etc.

Reference Types: String, arrays, and objects.

Loops: Repeating Code

Loops allow you to repeat a block of code multiple times. Java supports several types of loops, including for, while, and do-while.

Example: Using a for Loop

public class LoopsExample {

  public static void main(String[] args) {

  for (int i = 1; i <= 5; i++) {

  System.out.println("Count: " + i);

  }

  }

}

Example: Using a while Loop

public class WhileLoopExample {

  public static void main(String[] args) {

  int i = 1;

  while (i <= 5) {

  System.out.println("Count: " + i);

  i++;

  }

  }

}

Key Points:

Use for loops when you know the number of iterations.

Use while loops when the number of iterations depends on a condition.

Building Your First Project

Once you understand the basics, it’s time to create a simple project to consolidate your skills. Let’s build a basic calculator that performs addition and subtraction.

Example: Basic Calculator

import java.util.Scanner;

public class BasicCalculator {

  public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);

  System.out.println("Enter first number: ");

  double num1 = scanner.nextDouble();

  System.out.println("Enter second number: ");

  double num2 = scanner.nextDouble();

  System.out.println("Enter operation (+ or -): ");

  char operation = scanner.next().charAt(0);

  double result;

  if (operation == '+') {

  result = num1 + num2;

  } else if (operation == '-') {

  result = num1 - num2;

  } else {

  System.out.println("Invalid operation");

  return;

  }

  System.out.println("The result is: " + result);

  }

}

Key Points:

Use the Scanner class to take user input.

Use if-else statements to perform different operations based on user input.

Conclusion

Expanding beyond “Hello World” involves mastering variables, loops, and creating small projects to build confidence. As you continue, explore more advanced topics like methods, arrays, and object-oriented programming (OOP). Practice consistently, and you’ll be building complex applications in no time!


Edson Camacho
Software Engineer

Advertisements

Download the free ebook