Introduction to Java Programming

Learn Introduction to Java Programming with practical examples, explanations, best practices, and real-world coding techniques.

Java is a general-purpose programming language widely used to build backend services, enterprise applications, desktop software, Android applications, distributed systems, and many other types of software. Java was designed around portability, strong typing, object-oriented programming, and a large standard and third-party ecosystem.

One of Java's most important ideas is that source code is compiled into bytecode, which can then run on a Java Virtual Machine (JVM). This model allows Java applications to run across different operating systems as long as a compatible Java runtime is available.

Java is also a strong foundation for learning object-oriented programming. Concepts such as classes, objects, inheritance, interfaces, encapsulation, polymorphism, and exception handling are central to the language and are widely used in real-world Java applications.

What You Will Learn

  • What Java is and why it is widely used
  • How Java source code becomes executable bytecode
  • The difference between the JDK, JRE, and JVM
  • Where Java is used in real-world software
  • Why Java is considered cross-platform
  • How Java supports object-oriented programming
  • How to write and run a basic Java program
  • What makes Java different from languages such as JavaScript and Python

What Is Java?

Java is a statically typed programming language designed to let developers write software that can execute on multiple platforms through the JVM. Java source files normally use the .java extension and are compiled into bytecode stored in .class files.

Java is commonly associated with the idea "Write Once, Run Anywhere." The phrase describes the portability provided by the JVM-based execution model. The same compiled bytecode can be executed on different operating systems when an appropriate JVM is available.

How Java Works

Understanding the Java execution process makes the language much easier to reason about.

Java Source Code
      |
      |  javac
      v
Java Bytecode (.class)
      |
      v
Java Virtual Machine (JVM)
      |
      v
Operating System / Hardware

A developer writes Java source code first. The Java compiler converts that source into bytecode. The JVM then loads, verifies, and executes the bytecode on the target machine.

This extra runtime layer is one of the major reasons Java applications can remain portable across operating systems.

JDK, JRE and JVM

These three terms are commonly encountered when learning Java. Understanding their roles removes a lot of confusion when setting up a development environment.

Component Purpose
JVM Runs Java bytecode and provides the runtime execution environment.
JRE Traditionally describes the runtime components needed to run Java applications.
JDK Provides development tools such as the compiler together with the Java runtime components.

When you are developing Java applications, the JDK is the important installation because you need development tools such as javac. The JVM is responsible for executing the resulting bytecode.

The Java Development Kit

The JDK contains the tools required to write, compile, run, inspect, and package Java applications. Depending on the installed JDK and project requirements, developers may work with commands for compilation, execution, documentation, packaging, and debugging.

javac HelloWorld.java
java HelloWorld

The first command compiles the source code. The second starts the application using the Java runtime.

Your First Java Program

A basic Java program contains a class and a main method that can serve as the application's entry point.

public class HelloWorld {

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

The class is named HelloWorld. The main method is where execution begins for this simple standalone application. System.out.println() writes text to the console.

Understanding the Main Method

The standard entry-point form contains several keywords:

public static void main(String[] args)
Part Purpose
public Makes the method accessible to the runtime.
static Allows the method to be called without creating an object of the class.
void Indicates that the method does not return a value.
main Identifies the conventional application entry point.
String[] args Provides command-line arguments supplied to the application.

Where Is Java Used?

Java has a large ecosystem and is used across many software domains. Its long history, tooling, libraries, and enterprise adoption have contributed to its continued presence in production systems.

Backend Development

Java is widely used for server-side applications, REST APIs, microservices, authentication systems, payment services, and business applications.

Enterprise Applications

Large organizations use Java for systems involving transactions, financial processing, internal business applications, and long-running backend services.

Android Development

Java has historically played a major role in Android development. Kotlin is now heavily used for modern Android applications, while Java remains an important language in the Android ecosystem.

Distributed Systems

Java is used in many large-scale distributed systems, messaging systems, data-processing platforms, and infrastructure projects.

Desktop Applications

Java can also be used to build desktop software using Java GUI technologies and third-party frameworks.

Large Development Ecosystem

Developers can choose from mature libraries, frameworks, build tools, testing tools, IDEs, monitoring systems, and cloud integrations.

Why Learn Java?

Java is a useful language to learn when you want a strong foundation in programming, object-oriented design, backend development, or enterprise software development.

  • Strong typing: Variable and method types are explicit and checked by the compiler.
  • Object-oriented design: Java provides a rich model for classes, objects, inheritance, interfaces, and composition.
  • Portability: JVM-based execution supports multiple operating systems.
  • Mature ecosystem: Java has extensive libraries, tools, frameworks, and development environments.
  • Production use: Java is common in backend and enterprise environments where reliability and maintainability matter.
  • Career relevance: Core Java concepts are useful across many backend and software engineering roles.

Java and Object-Oriented Programming

Java is strongly associated with object-oriented programming. In an object-oriented design, data and behavior can be organized into classes and objects.

class User {

    String name;

    void greet() {
        System.out.println(
            "Hello, " + name
        );
    }
}


public class Main {

    public static void main(String[] args) {

        User user = new User();

        user.name = "Alex";

        user.greet();
    }
}

This example introduces several fundamental Java ideas at once: a class, a field, a method, object creation, and method invocation. These concepts become the foundation for larger object-oriented designs.

Java Is Statically Typed

Java requires variables to have a declared type. The compiler uses these type declarations to detect many invalid operations before the program runs.

int age = 25;

double price = 99.50;

String name = "Alex";

boolean active = true;

Java's static type system helps make method contracts and data structures explicit. It also enables strong compiler and IDE assistance when working on larger codebases.

Java Is Not JavaScript

Java and JavaScript are different programming languages. Their names are historically related, but their syntax, runtime environments, type systems, application ecosystems, and common development patterns are different.

Java JavaScript
Primarily JVM-based Primarily executed by JavaScript engines
Statically typed Dynamically typed
Common in backend and enterprise systems Common in browsers and JavaScript runtimes
Source files commonly use .java Source files commonly use .js

Java Program Structure

Java programs are commonly organized into classes and packages. A class may contain fields, constructors, methods, nested types, and other members.

package com.example.app;

public class UserService {

    private String serviceName;

    public UserService(
        String serviceName
    ) {
        this.serviceName = serviceName;
    }

    public void printName() {
        System.out.println(
            serviceName
        );
    }
}

Packages help organize related classes and create namespaces for larger applications. You will explore packages in more detail later.

Compiling and Running Java

A simple command-line workflow helps explain what happens between source code and execution.

javac HelloWorld.java

java HelloWorld

The compiler produces bytecode, and the Java runtime executes that bytecode. In everyday development, IDEs and build tools automate much of this process, but understanding the underlying commands is still valuable for debugging and environment setup.

Java Development Tools

Java development can be performed using a simple text editor and the JDK, but most professional projects use an IDE or code editor with Java support.

  • JDK for compilation and Java development tools
  • IDE or editor for writing and navigating code
  • Build tools for dependency management and project builds
  • Testing frameworks for automated tests
  • Version control such as Git for source management

Tools make development easier, but learning the language itself is more important than becoming dependent on one IDE.

Java Portability in Practice

Consider a developer who compiles a Java application on one operating system and then needs to run it on another.

Java source
     |
     v
Compiler
     |
     v
Bytecode
     |
     +------------------+
     |                  |
     v                  v
JVM on Windows      JVM on Linux
     |                  |
     v                  v
Platform-specific execution

The bytecode remains platform-independent while the JVM handles the platform-specific execution details.

A Simple Real-World Example

Java's basic syntax already allows you to create small application components that resemble real software features.

public class Order {

    private int quantity;
    private double price;

    public Order(
        int quantity,
        double price
    ) {
        this.quantity = quantity;
        this.price = price;
    }

    public double getTotal() {
        return quantity * price;
    }


    public static void main(
        String[] args
    ) {

        Order order = new Order(
            3,
            250.0
        );

        System.out.println(
            "Order total: "
            + order.getTotal()
        );
    }
}

This example is intentionally simple, but it already introduces the style of code found in larger applications: objects represent domain concepts, constructors initialize state, and methods expose behavior.

Strengths of Java

Portability

JVM-based execution helps Java applications target multiple operating systems without rewriting the application for each platform.

Strong Type System

Explicit types and compile-time checking can catch many programming mistakes before execution.

Large Ecosystem

Java developers have access to a large collection of libraries, frameworks, build tools, testing tools, IDEs, and cloud integrations.

Maintainable Large Codebases

Strong conventions, explicit types, packages, interfaces, and object-oriented design can support large teams and long-lived applications.

Things Beginners Often Find Difficult

Java can feel more verbose than some dynamically typed languages. Beginners may initially need to understand classes, types, access modifiers, constructors, packages, and the Java build and execution process.

That extra structure is not necessarily a disadvantage. Once these concepts become familiar, they provide a clear foundation for organizing larger programs.

Common Beginner Mistakes

Confusing Java with JavaScript

They are separate languages with different runtimes, syntax details, ecosystems, and common use cases.

Skipping Core Java

Jumping directly into a framework without understanding classes, methods, types, collections, exceptions, and control flow makes framework code harder to understand.

Ignoring Compiler Errors

Java's compiler provides useful information. Learn to read the error message and understand the line, type, and operation causing the problem.

Memorizing Without Understanding

Java has many keywords and APIs, but understanding the underlying programming concepts is more valuable than memorizing every class or method.

A Good Way to Start Learning Java

Start with core language concepts before moving into large frameworks. Learn how Java programs are structured and then gradually build more complex applications.

  1. Understand Java syntax and program structure.
  2. Learn variables, primitive types, reference types, and operators.
  3. Practice conditions and loops.
  4. Learn methods and parameter passing.
  5. Work with arrays, strings, and collections.
  6. Understand classes, objects, constructors, and methods.
  7. Learn inheritance, polymorphism, encapsulation, and abstraction.
  8. Practice exception handling.
  9. Learn packages and project organization.
  10. Build small Java applications before moving into frameworks.

Java in Professional Development

In professional development, Java is often used as part of a larger ecosystem rather than as an isolated language. A backend project may combine Java with a build system, a database, REST APIs, testing tools, logging, monitoring, containerization, cloud infrastructure, and frameworks such as Spring.

Learning Java itself is therefore the foundation. Framework knowledge becomes much easier once you understand the language features that the framework is built around.

Core Java Before Frameworks

A common beginner mistake is to start with framework annotations and configuration without understanding the language underneath them. A better approach is to first become comfortable with Java classes, methods, interfaces, collections, exceptions, generics, and basic object-oriented design.

Once the language is familiar, frameworks stop feeling like a collection of unrelated annotations and start making more architectural sense.

Summary

Java is a general-purpose, statically typed programming language with a strong focus on portability, object-oriented programming, maintainability, and large-scale application development. Its JVM-based execution model allows compiled Java bytecode to run on multiple platforms.

The JDK provides the tools needed to develop Java applications, while the JVM is responsible for executing Java bytecode. Understanding this relationship is one of the first important steps for a new Java developer.

The most valuable way to begin learning Java is to focus on core language fundamentals before moving into frameworks. Once variables, control flow, methods, classes, objects, collections, exceptions, and object-oriented concepts become comfortable, building larger Java applications becomes much easier.

Interview Questions to Prepare

  • What is Java and why is it considered platform independent?
  • What is the difference between JDK, JRE, and JVM?
  • How does Java source code become executable?
  • What is Java bytecode?
  • What is the role of the JVM?
  • What is the purpose of the main() method?
  • Why is Java considered a statically typed language?
  • Why is Java widely used for enterprise and backend applications?
  • What is object-oriented programming in Java?
  • What is the difference between Java and JavaScript?
  • What does "Write Once, Run Anywhere" mean in Java?

Code Examples

Your First Java Program

java
public class HelloWorld {

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

Variables and Basic Types

java
public class Main {

    public static void main(String[] args) {

        int age = 25;
        double price = 99.50;
        String name = "Alex";
        boolean active = true;

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

Creating a Class and Object

java
class User {

    String name;

    void greet() {
        System.out.println(
            "Hello, " + name
        );
    }
}


public class Main {

    public static void main(String[] args) {

        User user = new User();

        user.name = "Alex";

        user.greet();
    }
}
    

Compiling and Running Java

java
// Save this file as HelloWorld.java

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println(
            "Java is running!"
        );
    }
}

/*
Compile:

javac HelloWorld.java

Run:

java HelloWorld
*/
    

Simple Real-World Order Class

java
public class Order {

    private int quantity;
    private double price;

    public Order(
        int quantity,
        double price
    ) {
        this.quantity = quantity;
        this.price = price;
    }

    public double getTotal() {
        return quantity * price;
    }

    public static void main(
        String[] args
    ) {

        Order order = new Order(
            3,
            250.0
        );

        System.out.println(
            "Order total: "
            + order.getTotal()
        );
    }
}
    

Pro Tip

Build a strong Core Java foundation before moving into Spring or other frameworks. Understand how classes, objects, methods, types, collections, exceptions, and the JVM work first; framework concepts become much easier afterward.

More Java lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.