Introduction to Java

Introduction to Java Programming

Introduction to Java

Learn the fundamentals of Java programming language - features, JVM, JRE, and JDK

๐Ÿ“– What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle) in 1995. It's designed to be platform-independent, meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

Did you know? Java was originally called Oak after an oak tree that stood outside James Gosling's office. Later it was renamed to Java, from Java coffee, said to be consumed in large quantities by the language's creators.

๐ŸŽฏ Key Characteristics

  • Write Once, Run Anywhere (WORA) - Java code compiles to bytecode that runs on any Java Virtual Machine (JVM)
  • Object-Oriented - Follows OOP principles like encapsulation, inheritance, and polymorphism
  • Robust - Strong memory management, exception handling, and type checking
  • Secure - Provides security features like bytecode verification and sandboxing
  • Multithreaded - Supports concurrent programming with built-in thread management

๐Ÿ’ป Your First Java Program

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

Try It Yourself

Click the button below to see the output of the HelloWorld program:

// Output will appear here

Key Features of Java

Java incorporates many powerful features that contribute to its popularity and widespread use:

๐ŸŒ

Platform Independent

Java code compiles to bytecode that runs on any system with a JVM

๐Ÿ›ก️

Secure

No explicit pointers, bytecode verification, and runtime security checks

๐Ÿ’ช

Robust

Strong memory management, exception handling, and type checking

๐Ÿ“š

Object-Oriented

Based on objects and classes with inheritance, polymorphism, encapsulation

High Performance

Just-In-Time (JIT) compilers enable high performance

๐Ÿงต

Multithreaded

Built-in support for multithreaded programming

๐Ÿ“ฆ

Portable

Architecture-neutral and portable to any platform

๐Ÿ”ง

Dynamic

Capable of adapting to evolving environments

๐Ÿ“Š Java vs Other Languages

Feature Java C++ Python
Compilation Compiled to bytecode Compiled to machine code Interpreted
Memory Management Automatic (Garbage Collection) Manual Automatic (Garbage Collection)
Platform Independence Yes (Write Once, Run Anywhere) No Yes
Performance High Very High Moderate
Learning Curve Moderate Steep Easy

⚙️ Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program. It provides a runtime environment in which Java bytecode can be executed.

Java Virtual Machine (JVM)

Runtime environment that executes Java bytecode

Platform Dependent - Different JVM for each OS

Java Bytecode

Intermediate representation of Java code (.class files)

Platform Independent - Same bytecode runs on any JVM

Java Source Code

Original Java program written by developers (.java files)

Compiled to bytecode using javac compiler

๐Ÿ”ง How JVM Works

  1. Loading - ClassLoader loads the .class file into memory
  2. Linking - Verifies, prepares, and resolves references
  3. Initialization - Initializes static variables and executes static blocks
  4. Execution - Interpreter/JIT compiler executes the bytecode
  5. Garbage Collection - Automatically frees memory of unused objects

๐Ÿ“ JVM Architecture Components

  • ClassLoader - Loads class files into memory
  • Runtime Data Areas - Memory areas used during execution
  • Execution Engine - Executes the bytecode
  • Native Method Interface (JNI) - Interface for native libraries
  • Native Method Libraries - Collection of native libraries

Note: JVM is platform-dependent, which means you need a specific JVM for Windows, Linux, macOS, etc. However, the bytecode it executes is platform-independent.

๐Ÿ“ฆ Java Runtime Environment (JRE)

The Java Runtime Environment (JRE) provides the libraries, Java Virtual Machine (JVM), and other components to run applications written in Java programming language.

Java Runtime Environment (JRE)

Contains everything needed to run Java applications

Java Virtual Machine (JVM)

Executes Java bytecode

Runtime Class Libraries

Standard Java classes (java.lang, java.util, etc.)

User Application

Your compiled Java program (.class files)

๐ŸŽฏ JRE Components

  • Java Virtual Machine (JVM) - The runtime engine
  • Java Class Libraries - Collection of pre-written code
  • Other Supporting Files - Property files, resources, etc.

๐Ÿ’ก When do you need JRE?

You only need JRE if you want to run Java applications. If you need to develop Java applications, you need JDK (Java Development Kit).

Important: JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications. For development, you need JDK.

๐Ÿ› ️ Java Development Kit (JDK)

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the JRE plus development tools.

Java Development Kit (JDK)

Complete development environment for Java

Java Runtime Environment (JRE)

Runtime environment to execute Java programs

Development Tools

javac, jar, javadoc, debugger, etc.

๐Ÿ”ง Key JDK Tools

  • javac - The Java compiler
  • java - The Java application launcher
  • javadoc - Documentation generator
  • jar - Creates Java archive files
  • jdb - The Java debugger
  • appletviewer - Runs and debugs Java applets

๐Ÿ“ JDK Directory Structure

Typical JDK Installation Directory
jdk1.8.0/
├── bin/        # Binary files (javac, java, etc.)
├── lib/        # Library files
├── include/    # C/C++ header files
├── jre/        # Java Runtime Environment
└── src.zip     # Source code of Java libraries

Compilation Process Demo

See how Java source code is compiled and executed:

// Compilation steps will appear here

๐Ÿ“Š JVM vs JRE vs JDK

Understanding the relationship between JVM, JRE, and JDK is crucial for Java developers:

Component Purpose Contains Used For
JVM
(Java Virtual Machine)
Provides runtime environment to execute Java bytecode Runtime engine, memory areas, execution engine Running Java applications
JRE
(Java Runtime Environment)
Provides environment to run Java applications JVM + Class Libraries + Other files Running Java applications (end-users)
JDK
(Java Development Kit)
Provides environment to develop Java applications JRE + Development Tools (javac, debugger, etc.) Developing Java applications (developers)

๐Ÿ”— Relationship Diagram

JDK = JRE + Development Tools

For Java Developers

JRE = JVM + Library Classes

For End Users

JVM (Java Virtual Machine)

Runs Java Bytecode

๐Ÿ’ก Quick Reference

  • To run Java programs: You need JRE
  • To develop Java programs: You need JDK
  • JVM is the heart of Java's platform independence
  • JDK contains JRE, and JRE contains JVM

Remember: When you install JDK, JRE is automatically installed. But if you only want to run Java applications, you can install JRE separately.

Java Programming Tutorial | Understanding JVM, JRE, and JDK

Java is a registered trademark of Oracle and/or its affiliates.

Comments

Popular posts from this blog

What is Software Testing? Complete Beginner-Friendly Guide

Defect Management: Complete Guide with Tools & Best Practices

SDLC Models: Complete Guide with Advantages & Disadvantages