Introduction to Java

by - Kumar Vicky

September 19, 2025

An Introduction to Java for Beginners

Java is a powerful, high-level, and object-oriented programming language. Its defining feature is its portability, encapsulated in the philosophy of "Write Once, Run Anywhere" (WORA).

This is achieved through the Java Virtual Machine (JVM), which allows Java code to run on any device or operating system that has a JVM installed, without needing to be recompiled.

Java is a fantastic language for beginners to learn because it is structured, widely used, and has a strong community. It's the backbone of many large-scale enterprise applications, mobile apps (Android), and web backends.


Key Concepts of Java

1. Object-Oriented Programming (OOP)

Java is built on the principles of OOP, which means it organizes code around objects rather than functions and logic. This makes code more modular, reusable, and easier to manage.

The four pillars of OOP are:

  • Encapsulation: Bundling data and methods that operate on that data into a single unit (a class).
  • Inheritance: Creating a new class from an existing one, inheriting its properties and behaviors.
  • Abstraction: Hiding complex implementation details and showing only the essential features to the user.
  • Polymorphism: The ability of an object to take on many forms.

2. Java Virtual Machine (JVM)

The JVM is the core component that makes Java platform-independent.

When you write and compile a Java program, it is converted into bytecode, which is then executed by the JVM on any machine.


3. Syntax and Structure

Java has a specific and strict syntax:

  • Every Java program must have a class.
  • The execution of a program begins in the main method:
public static void main(String[] args)

Statements are terminated with a semicolon ;.

Code blocks are enclosed in curly braces {}.

Simple Java Code for Beginners

  1. "Hello, World!" Program

This is the classic first program for any language. It helps you understand the basic structure of a Java class and how to print output to the console.

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

public class HelloWorld: Declares a class named HelloWorld.

public static void main(String[] args): This is the entry point of your program. The JVM looks for this method to start execution.

System.out.println("Hello, World!");: Prints text to the console.
  1. Variables and Data Types

Variables are used to store data. In Java, you must declare the data type of a variable before you use it.

public class VariablesExample {

    public static void main(String[] args) {
        // Declaring and initializing variables of different data types
        int number = 10;
        double decimalNumber = 20.5;
        boolean isJavaFun = true;
        char singleCharacter = 'J';
        String text = "This is a string.";

        // Printing the values of the variables
        System.out.println("Integer variable: " + number);
        System.out.println("Double variable: " + decimalNumber);
        System.out.println("Boolean variable: " + isJavaFun);
        System.out.println("Character variable: " + singleCharacter);
        System.out.println("String variable: " + text);
    }
}
  1. Conditional Statements (if-else)

Conditional statements allow your program to make decisions based on certain conditions.

import java.util.Scanner;

public class AgeChecker {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();

        if (age >= 18) {
            System.out.println("You are an adult.");
        } else {
            System.out.println("You are not yet an adult.");
        }
    }
}

import java.util.Scanner;: Imports the Scanner class for user input.
scanner.nextInt();: Reads an integer value entered by the user.
  1. Loops (for loop)

Loops are used to execute a block of code repeatedly.

public class LoopExample {

    public static void main(String[] args) {
        // A for loop that prints numbers from 1 to 5
        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
        }
    }
}
  1. Creating a Simple Method

Methods are blocks of code that perform a specific task. They help organize your code and make it reusable.

public class MethodExample {

    public static void main(String[] args) {
        // Calling the method to add two numbers
        int sum = addNumbers(5, 7);
        System.out.println("The sum is: " + sum);
    }

    // A method that takes two integers and returns their sum
    public static int addNumbers(int num1, int num2) {
        return num1 + num2;
    }
}

✅ With these basics, you’re ready to start building small programs in Java!

Happy Coding... 🚀

Comments