Java Tutorial: First Steps

Java is a widely-used, object-oriented programming language designed to be platform-independent. It is commonly used for web applications, enterprise software, and mobile development. This tutorial covers the essential steps to start programming in Java.

Setting Up the Environment

To write and run Java programs, you need the following:

  • JDK (Java Development Kit) – Includes the Java compiler and runtime environment.
  • IDE (Integrated Development Environment) – Some popular choices are:
    • Eclipse – Feature-rich and widely used.
    • IntelliJ IDEA – Offers advanced features and a user-friendly interface.
    • NetBeans – An open-source alternative.

Alternatively, you can write Java code in a text editor and compile it using the command line.

Writing Your First Java Program

A simple Java program consists of a main method that serves as the entry point. Below is an example:

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

Compiling and Running the Code

To compile and run the program, use the following commands in a terminal or command prompt:

javac Main.java  # Compiles the Java file
java Main        # Runs the compiled program

Understanding the Code

  • public class Main – Defines a class named Main.
  • public static void main(String[] args) – The main method where execution starts.
  • System.out.println("Hello, World!"); – Prints text to the console.

Basic Syntax and Concepts

Java follows a structured syntax with clear rules. Here are some fundamental elements:

Variables and Data Types

Java supports different data types:

int age = 25;
double pi = 3.1416;
char grade = 'A';
boolean isStudent = true;
String name = "Alice";

Control Structures

Conditional statements and loops control the program flow:

if (age > 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

for (int i = 0; i < 5; i++) {
    System.out.print(i + " ");
}

Methods

Methods allow modular programming and code reuse:

public static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int sum = add(3, 4);
    System.out.println("Sum: " + sum);
}

Next Steps

Once you understand these basics, explore topics like object-oriented programming, exception handling, and Java libraries.

If you didn't find Java interesting, here are some other programming languages you may like!

Comments