• Java Development Kit (JDK) and Java Runtime Environment (JRE)ย 

    To download and install the Java Development Kit ( JDK) and Java Runtime Environment (JRE), follow these step-by-step instructions for Windows, macOS, and Linux.The overall process is similar across all platforms, with minor differences in installation steps and setting up environment variables.

    โœ…What is JDK in Java?

    The Java Development Kit (JDK) is a complete software development kit used to build Java applications. It includes:

    • Java Runtime Environment (JRE) โ€“ provides the libraries and tools to run Java programs.
    • Java Virtual Machine (JVM) โ€“ executes the compiled Java bytecode.
    • Development tools โ€“ such as the Java compiler (javac), debugger, and packaging utilities.

    In short, the JDK is more than just a runtime environmentโ€”it provides everything needed to develop, compile, and run Java programs.

    โœ…Why Do We Need JDK Instead of Just JRE?

    A common question developers ask is:
    ๐Ÿ‘‰ โ€œTo run Java programs, isnโ€™t the JRE enough? Why do we need the complete JDK?โ€

    The answer:

    • The JRE is enough to run Java applications.
    • But to develop Java applications, you need tools like the compiler (javac), debugger, and other utilities that are only included in the JDK.

    Thatโ€™s why programmers use the JDK for development and the JRE for execution.

    โœ…The Architecture of JDK in Java

    The architecture of JDK consists of three essential components:

    1. JVM (Java Virtual Machine)
      • Provides a runtime environment for Java programs.
      • Converts compiled Java bytecode into machine code.
      • Powers Javaโ€™s famous feature: โ€œWrite once, run anywhere.โ€
    2. JRE (Java Runtime Environment)
      • Provides the core libraries, Java ClassLoader, and other components required to run applications.
      • It does not include development tools like the compiler.
    3. Development Tools (inside JDK)
      • Includes javac (Java compiler), jdb (debugger), jar (archiver), and other utilities required for building Java applications.

    โœ…Downloading Java JDK and JRE

    1. Visit the Official Website
    Go to the Oracle Java Downloads page or the OpenJDK official site. The current stable Java versions are JDK 21 (LTS) and JDK 23 (latest release).

    2. Choose Your Operating System
    Download the installer that matches your operating system:

    • Windows (.exe)
    • macOS (.dmg or .pkg)
    • Linux (.tar.gz or Debian package)

    3. Accept the License Agreement
    For Oracle JDK downloads, review and accept the license agreement before proceeding with the download.

    โœ… Installing Java JDK on Windows

    1. Run the Installer
    Double-click the downloaded .exe file to start the installation.

    2. Follow Installation Prompts
    Proceed with the setup wizard, selecting the default options unless you prefer a custom location. By default, Java is installed in:

    C:\Program Files\Java\jdk-xx
    

    3. Set the JAVA_HOME Environment Variable

    • Right-click This PC โ†’ Properties โ†’ Advanced system settings โ†’ Environment Variables.
    • Add a new variable:
      • Name: JAVA_HOME
      • Value: path of your JDK installation (e.g., C:\Program Files\Java\jdk-21).
    • Edit the existing Path variable and add: %JAVA_HOME%\bin

    ๐Ÿ‘‰Follow this detailed guide on how to set up the Java PATH environment variable

    4. Verify Installation
    Open Command Prompt and run:

    java --version
    javac --version
    

    ๐Ÿ“ŒIf both commands show the installed version, your Java JDK setup on Windows is complete.

    โœ… Installing Java JDK on macOS

    1. Run the Installer
    Open the downloaded .dmg or .pkg installer and follow the on-screen instructions to complete the installation.

    2. Configure Environment Variables
    To set up JAVA_HOME and update the PATH:

    • If using bash, open ~/.bash_profile
    • If using zsh (default on newer macOS), open ~/.zshrc

    Add:

    export JAVA_HOME=$(/usr/libexec/java_home)
    export PATH=$JAVA_HOME/bin:$PATH
    

    Save the file and reload it:

    source ~/.zshrc   # or source ~/.bash_profile
    

    โœ… Installing Java JDK on Linux

    1. Download the Package
    Choose the installer based on your Linux distribution:

    • Debian/Ubuntu: .deb package
    • RedHat/CentOS/Fedora: .rpm package
    • Generic: .tar.gz tarball

    2. Install via Terminal

    For Debian/Ubuntu:

    sudo dpkg -i jdk-xx_linux-x64_bin.deb
    

    For tar.gz (generic installation):

    tar -xvzf jdk-xx_linux-x64_bin.tar.gz
    sudo mv jdk-xx /opt/
    

    3. Configure Environment Variables
    Add the following lines to ~/.bashrc or ~/.zshrc:

    export JAVA_HOME=/opt/jdk-xx
    export PATH=$JAVA_HOME/bin:$PATH
    

    Reload the file:

    source ~/.bashrc   # or source ~/.zshrc
    

    4. Verify Installation
    Run the following commands to confirm Java is installed correctly:

    java --version
    javac --version

    โœ… Installing Java JRE

    On Windows

    Download the Java Runtime Environment (JRE) installer from the official Oracle Java Downloads page. Run the installer, follow the setup prompts, and configure the JRE path in System Environment Variables, similar to the JDK setup process.

    On macOS/Linux

    In most modern setups, the JRE is already bundled with the JDK. If you still need a standalone JRE, the process is similar: download the installer for your OS, complete the installation, set the JAVA_HOME environment variable, and verify the installation.


    ๐Ÿ“Œ Important Notes

    • Most modern JDK packages include JRE by default, so a separate JRE installation is usually unnecessary.
    • Always download Java from official sources (Oracle or OpenJDK) to ensure security and long-term support.
    • Correct installation and environment variable setup ensure Java is ready for both development (JDK) and runtime (JRE) tasks on your system.
  • Complete Guide to Garbage Collection in Java

    What is Garbage Collection

    Garbage Collection (GC) is the process by which the JVM automatically frees up memory by removing objects that are no longer reachable in the program. It runs in the background along with the Java application, reclaiming unused memory to improve performance and avoid memory leaks.

    ๐Ÿ”นPurpose of Garbage Collection in Java

    In Java, garbage collection (GC) is the process of automatically reclaiming memory by removing objects that are no longer reachable or needed by the application.

    • Java programs create many objects dynamically (using new).
    • Some of these objects may only be used temporarily. Once no references point to them, they become eligible for garbage collection.
    • Instead of requiring developers to explicitly free memory (like in C/C++ with free() or delete), Javaโ€™s GC automates memory cleanup.

    ๐Ÿ”นAutomated Nature of Garbage Collection

    • No manual deallocation: Developers donโ€™t have to explicitly release memory. The Java Virtual Machine (JVM) handles it.
    • Background process: The GC runs in the background, periodically scanning the heap to find unused objects.
    • Reachability analysis: GC uses a graph of object references starting from “roots” (like local variables, static fields, and active threads). If an object cannot be reached from any root, it is considered garbage.
    • Algorithms: The JVM uses different GC algorithms (e.g., Mark-and-Sweep, G1, ZGC) depending on performance requirements.

    ๐Ÿ”นWhy Memory Management Matters

    Even though Java has automatic GC, memory issues can still occur if resources are not handled correctly:

    1. Avoiding OutOfMemoryError
      • If the heap fills up with objects that are still referenced (or not collected quickly enough), the JVM can throw an OutOfMemoryError.
      • Example: Loading millions of large objects without proper disposal can exhaust heap space.
    2. Preventing Memory Leaks
      • A memory leak happens when objects are no longer needed but still kept referenced (e.g., in collections like List or Map).
      • GC cannot collect these objects, leading to gradual memory exhaustion.
      • Example: Forgetting to remove old listeners, cache entries, or closing database connections.
    3. Application Performance
      • Efficient memory management ensures fewer GC pauses, smoother performance, and better scalability.
      • Poor management โ†’ frequent GC cycles, longer pauses, sluggish application behavior.

    ๐Ÿ“ In short:
    Garbage collection in Java automates memory cleanup, reducing developer burden and errors. Still, developers must write mindful code to avoid memory leaks and manage resources properly, or they risk OutOfMemoryError and performance issues.

    ๐Ÿ”น JVM Memory Architecture Overview

    When a Java program runs, the JVM divides memory into different runtime areas. These ensure that objects, variables, and methods are managed efficiently.

    1. Heap Memory

    Heap is the runtime area where all objects are stored. It is shared among all threads.It is further divided into generations to optimize garbage collection:

    • Young Generation
      • Where new objects are created.
      • Further divided into:
        • Eden Space โ†’ New objects are allocated here first.
        • Survivor Spaces (S0, S1) โ†’ Objects that survive garbage collection in Eden are moved here.
      • Frequent garbage collections happen here (Minor GC).
    • Old Generation (Tenured)
      • Stores long-lived objects that survived multiple GC cycles.
      • Garbage collection here is less frequent but more expensive (Major GC / Full GC).
    • Metaspace (Java 8+)
      • Replaced PermGen.
      • Stores class metadata (class definitions, method info, static variables).
      • Grows dynamically, unlike the fixed-size PermGen.

    2. Stack Memory

    • Each thread has its own stack.
    • Stores:
      • Method call frames.
      • Local variables (including primitives).
      • References to objects (but not the objects themselves).
    • When a method is invoked, a new frame is pushed; when the method ends, the frame is popped.

    If stack space runs out (e.g., due to deep recursion), a StackOverflowError occurs.

    3. Other Areas (briefly)

    • Program Counter (PC) Register: Holds the address of the currently executing instruction for each thread.
    • Native Method Stack: Supports execution of native (non-Java) code.

    ๐Ÿ”น JVM Memory Structure Diagram

               +-------------------------+
               |     JVM Memory          |
               +-------------------------+
    
               +-------------------------+
               |       Heap Memory       |   (Shared across threads)
               |-------------------------|
               |   Young Generation      |
               |    - Eden Space         |
               |    - Survivor 0 (S0)    |
               |    - Survivor 1 (S1)    |
               |-------------------------|
               |   Old Generation        |
               |-------------------------|
               |   Metaspace (Java 8+)   |
               +-------------------------+
    
               +-------------------------+
               |    Stack Memory         |   (Per thread)
               |  - Method frames        |
               |  - Local variables      |
               |  - References           |
               +-------------------------+
    
               +-------------------------+
               | PC Register (per thread)|
               +-------------------------+
               | Native Method Stack     |
               +-------------------------+
    

    ๐Ÿ“ In short:

    • Heap โ†’ Objects (Young: Eden + Survivor, Old, Metaspace).
    • Stack โ†’ Method calls, primitives, object references (thread-local).
    • Other parts like PC Register and Native Stack support execution.

    ๐Ÿ”นWays to Make an Object Eligible for Garbage Collection : 

    1. Nullifying the reference variable
    2. Reassign the reference variable
    3. Objects created inside a metho

    โœ…1. Nullifying the reference variable

    If an object is no longer required, you can make it eligible for GC by assigning null to its reference variable:

    Student s1 = new Student();
    s1 = null; // Now eligible for GC

    โœ…2.Reassigning the reference variable

    If a reference variable is reassigned to point to another object, the old object becomes unreachable and eligible for Garbage Collection(GC).

    Student s1 = new Student();
    Student s2 = new Student();
    s1 = s2; // Old s1 object is now eligible for GC

    โœ…3.Objects created inside a method

    Objects created inside a method are eligible for Garbage Collection (GC) once the method completes, provided there are no references to them outside the method:

    void myMethod() {
    MyClass obj = new MyClass();
    // obj is eligible for GC after method execution
    } 
    

    Example:

    class MyClass {
        int id;
    
        MyClass(int id) {
            this.id = id;
            System.out.println("Object " + id + " created");
        }
    
        // finalize() is deprecated after Java 9, 
        // but here we use it only for demonstration
        @Override
        protected void finalize() throws Throwable {
            System.out.println("Object " + id + " is garbage collected");
        }
    }
    
    public class GCDemo {
        public static void main(String[] args) {
            createObject();
            
            // Requesting JVM to run GC
            System.gc();
    
            // Adding delay to give GC some time
            try { Thread.sleep(1000); } catch (InterruptedException e) {}
            
            System.out.println("End of main method");
        }
    
        static void createObject() {
            MyClass obj = new MyClass(1);
            // After this method ends, 'obj' goes out of scope
            // and becomes eligible for Garbage Collection
        }
    }
    

    Explanation

    1. Inside createObject()
      • obj is a local variable referring to a MyClass object.
      • When the method finishes, the variable obj goes out of scope.
    2. After method execution
      • No references to that object remain.
      • Hence, the object becomes eligible for Garbage Collection.
    3. System.gc() call
      • Requests JVM to run the GC.
      • If the JVM runs GC, the overridden finalize() method is called, and youโ€™ll see:
    Object 1 is garbage collected

    โœ…Ways for Requesting the JVM to Run Garbage Collector

    Although you cannot force garbage collection, you can request it using the folowing methods:

    โœ…System.gc()

    A static method in the System class that requests GC.

    System.gc()

    System.gc() method is used to call the garbage collector to perform clean-up processing.

    โœ…Runtime.getRuntime().gc()

    The Runtime class, present in the java.lang package, is a singleton that provides the gc() method through the getRuntime() factory method.

    Runtime.getRuntime().gc();

    ๐Ÿ‘‰ Both methods are (System.gc() and Runtime.getRuntime().gc() )equivalent, but the JVM is not obligated to perform garbage collection immediately.

    Quest : Can Garbage Collection be forced?

    โŒ No. Garbage Collection cannot be forced. You can only request it, but the JVM decides when (or if) it will actually run.

    โœ…Finalization (โš ๏ธ Deprecated)

    Before destroying an object, the Garbage Collector (GC) once invoked the finalize() method to perform cleanup activities.

    @Override
    protected void finalize() throws Throwable {
        // cleanup code
    }
    • The finalize() method is defined in the Object class:

    โš ๏ธ Note: Since Java 9, the finalize() method has been deprecated and its use is strongly discouraged. Developers should instead use alternatives such as try-with-resources or implement the AutoCloseable interface for cleanup tasks.

    • try-with-resources for automatic resource management, or
    • AutoCloseable interface for cleanup tasks.

    โœ…Memory Leak

    A memory leak occurs when an object is no longer needed by the application but is still being referenced. This prevents GC from reclaiming it, leading to unnecessary memory consumption.

    This results in unnecessary memory consumption and may cause the application to slow down or even crash.

    โœ…Common Causes of OutOfMemoryError(OOM) in JVM

    When the JVM runs out of memory and cannot allocate more objects or native resources, it throws an OutOfMemoryError. This can happen in several scenarios depending on how the application uses memory.

    • Java Heap Space Exhaustion
    • GC Overhead Limit Exceeded
    • Metaspace Exhaustion (Java 8 and later)
    • Native Memory Exhaustion
    • Array Size Limit Exceeded
    • Kernel or OS Memory Limits

    ๐Ÿ‘‰ To learn more about the different types of OutOfMemoryError and how to fix them, check out our detailed guide on OutOfMemoryError in Java.

  • Introduction To Java Virtual Machine (JVM)

    What is JVM?

    The Java Virtual Machine (JVM) is a virtual machine that provides the runtime environment to execute Java bytecode.
    The JVM does not understand Java source code directly. Thatโ€™s why .java files are first compiled into .class files containing bytecode, which the JVM can interpret and execute.This bytecode is platform-independent and can run on any machine with a compatible JVM.
    The JVM is responsible for controlling the execution of every Java program and enables important features such as automatic memory management and security.

    It also enables important features such as:

    • Automatic exception handling
    • Garbage collectionย for memory management
    • Platform independence (through bytecode execution)

    JVM Architecture : 

    The JVM architecture consists of several key components that work together to execute Java programs:

    1.Class Loader

    The Class Loader loads classes into memory for execution.

    There are three main types of class loaders:

    1. Boot Strap ClassLoader โ†’ Loads core Java classes from rt.jar file (inside JRE/lib). It has the highest priority.
    2. Extension ClassLoader โ€“ โ†’ Loads classes from the ext directory (jre/lib/ext).
    3. Application (System) ClassLoader โ†’ Loads classes from the applicationโ€™s classpath (environment variables, project paths, etc.).

    3.Method Area

    Stores class-level data such as class structures, metadata, method code, and static variables. There is only one Method Area per JVM instance.

    3.Heap

    Stores all Java objects, their instance variables, and arrays. The heap is shared among all threads, and hence the data stored here is not thread-safe by default.

    4.JVM Stack

    Each thread has its own JVM stack, created when the thread starts. It stores:

    • Local variables
    • Partial results
    • Method call frames (each frame holds data, local variables, and references to objects in the heap

    5.Program Counter (PC) Register

    Each thread has its own PC register. It holds the address of the current JVM instruction being executed.

    6.Native Method Stack

    Contains instructions for native methods (non-Java code, usually written in C/C++). It supports interaction between Java applications and platform-specific native libraries.

    7.Execution Engine

    The Execution Engine runs the bytecode. It consists of:

    • Interpreter โ†’ Executes bytecode line by line (simple but slower).
    • JIT (Just-In-Time) Compiler โ†’ Improves performance by compiling frequently used bytecode into native machine code.
    • Garbage Collector (GC) โ†’ Automatically manages memory by removing unused objects from the heap

    8.Native Method Interface (JNI)

    Acts as a bridge between Java code and native applications/libraries (C, C++, etc.), allowing Java to call platform-specific methods.

    9.Native Method Libraries

    A collection of native libraries required by the Execution Engine. These are loaded and linked using the JNI.


    ๐Ÿ“Key Features of JVM

    • Provides platform independence by executing bytecode on any system.
    • Ensures memory management with garbage collection.
    • Handles runtime exceptions automatically.
    • Supports integration with native libraries through JNI.

    .

  • Complete Guide to Java Environment Setup for Beginners โ€“ JDK, IDE, and PATH Configuration

    โœ…Go to My Computer / This PC icon on your desktop (or File Explorer). Right-click on it and select Properties.

    โœ…In the System window, click on Advanced system settings.

    โœ…In the System Properties dialog box, click on the Advanced tab.

    โœ…Click the Environment Variables button.

    โœ…Under System Variables, click on the New button.

    โœ…A dialog box will appear. Donโ€™t close it.

    โœ…Open My Computer / This PC โ†’ C:\Program Files\Java\jdk1.8.0_144\bin (navigate to your installed JDK folder). Copy this full path:

    C:\Program Files\Java\jdk1.8.0_144\bin

    โœ…Go back to the New System Variable dialog box.In Variable Name, type : Path and In Variable Value, paste the copied path:

    C:\Program Files\Java\jdk1.8.0_144\bin

    ๐Ÿ‘‰ If there are already other values in the Path variable, donโ€™t delete them โ€” instead, click Edit and add a semicolon (;) at the end of the existing values, then paste your Java path.

    โœ…Click OK โ†’ OK โ†’ OK to close all dialogs.

    โœ…Restart Command Prompt (CMD) and type:

    java -version<br>javac -version

  • How to Use Java isEnum() Method โ€“ Complete Guide with Syntax and Examples

    The isEnum() method in Java, available in the java.lang.Class class, is used to check whether a given Class object represents an enumeration type.

    If the class represents an enum, the method returns true, otherwise, it returns false.

    This method is particularly useful in reflection-based programming, where enums need to be verified or processed dynamically (for example, in frameworks, libraries, or configuration systems).

    Syntax :

    public boolean isEnum()

    โœ…Parameters

    • This method takes no parameters.

    โœ…Returns

    • true โ†’ if the class object represents an enum type.
    • false โ†’ if the class object is not an enum type.

    Understanding of isEnum():

    The method allows developers to check at runtime whether a class is an enum.This can be very useful when you want to dynamically handle enums (for example, iterating through their constants, generating dropdown options, or validating user inputs).

    Example 1: Basic Usage

    public class IsEnumExample {
        public static void main(String[] args) {
            Class<PaymentStatus> clazz = PaymentStatus.class;
            boolean result = clazz.isEnum();
    
            System.out.println("Is PaymentStatus an enum? " + result);
        }
    
        public enum PaymentStatus {
            PENDING, PROCESSING, SUCCESS, FAILED
        }
    }

    Output :

    Is PaymentStatus an enum? true

    โœ…๐Ÿ‘‰ Here, PaymentStatus is an enum, so isEnum() returns true.

    Example 2: Checking a Non-Enum Class

    public class NonEnumExample {
        public static void main(String[] args) {
            Class<Integer> clazz = Integer.class;
            boolean result = clazz.isEnum();
    
            System.out.println("Is Integer an enum? " + result);
        }
    }

    Output:

    Is Integer an enum? false

    Note :๐Ÿ‘‰  Since Integer is not an enum, the result is false.


    ๐Ÿ“Key Ponts:

    The Class.isEnum() method provides a reliable way to identify enums at runtime.

    • It returns true for enums and false otherwise.
    • Itโ€™s often used in reflection-based frameworks to dynamically handle enums for configuration, validation, or UI generation.
    • This makes it an important tool when building generic and reusable libraries.

  • Java Enum Tutorial: How to Define and Use Enums in Java with Examples

    Enums were introduced in Java 5 (JDK 1.5) as a way to define a collection of named constants in a type safe manner. Unlike enums in older languages (like C or C++), Java enums are far more powerful since they are implemented using class concepts and can contain constructors, methods, and variables.

    โœ…What is an Enum?

    • Enum is a special data type used to define a group of named constants.
    • Each enum constant is implicitly:
      • publicstatic, and final.
    • Enums make the code readable, maintainable, and less error-prone compared to using int or String constants.

    Example :

    enum Month {
      JAN,FEB,MAR,DEC;
    }

    โœ…Internal Implementation of Enum

    • Internally, an enum is implemented as a class.
    • Every enum constant is a reference variable that points to its own enum object.
    • You can think of enums as a fixed set of static final objects.

    Enum Declaration and Usage :

    enum Month{
        JAN, FEB, MAR, DEC; // semicolon at the end is optional if no extra members
    }
    
    class Test {
        public static void main(String[] args) {
            Month mon = Month.FEB;
            System.out.println(mon);
        }
    }

    Output:

    FEB

    ๐Ÿ‘‰ Note: Since enum constants are implicitly static, we can access them using EnumName.CONSTANT.

    โœ…Enum with Switch Statement :

    Before Java 5, switch allowed only byte,short,char,int (and their wrappers). From Java 5 onwardsenum types can also be used in a switch.

    enum PaymentStatus {
        PENDING, PROCESSING, SUCCESS, FAILED;
    }
    
    class Test {
        public static void main(String[] args) {
            PaymentStatus status = PaymentStatus.PROCESSING;
    
            switch (status) {
                case PENDING:
                    System.out.println("Payment is pending. Please wait...");
                    break;
                case PROCESSING:
                    System.out.println("Payment is being processed. Do not refresh the page.");
                    break;
                case SUCCESS:
                    System.out.println("Payment successful! Thank you for your purchase.");
                    break;
                case FAILED:
                    System.out.println("Payment failed. Please try again.");
                    break;
                default:
                    System.out.println("Unknown payment status.");
            }
        }
    }

    Example:

    Output :

    Payment is being processed. Do not refresh the page.

    ๐Ÿ‘‰ Every case label must be a valid enum constant, otherwise youโ€™ll get a compile-time error.

    โœ…Enum and Inheritance

    • Every enum in Java is implicitly a child of java.lang.Enum.
    • Hence, enums cannot extend other classes.
    • Enums are implicitly final, so they cannot be extended.
    • But enums can implement interfaces.

    โœ…Useful Enum Methods

    1. values() โ€“ returns all constants as an array.
    2. ordinal() โ€“ returns the index (zero-based position) of the constant.
    3. valueOf(String name) โ€“ returns the enum constant with the specified name 

    Enum values():

    • The values() method is a built-in static method that is automatically added by the Java compiler for every enum.
      ๐Ÿ‘‰ It returns an array of all the constants defined in the enum, in the order they are declared.
    • Return type: EnumType[]

    ๐Ÿ“Œ Syntax :

    public static EnumType[] values()
    โœ… Why Use values()?
    • To iterate over all enum constants.
    • Useful when you want to display options, validate input, or implement logic based on all possible values.

    Example :

    public enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    }
    
    public class EnumExample {
        public static void main(String[] args) {
            Day[] days = Day.values();  // Calls the values() method
            
            for (Day day : days) {
                System.out.println(day);
            }
        }
    }

    Output :

    MONDAY
    TUESDAY
    WEDNESDAY
    THURSDAY
    FRIDAY
    SATURDAY
    SUNDAY

    ordinal():

    The ordinal() method is a built-in method provided by Java for enum constants.
    ๐Ÿ‘‰ It returns the position (index) of the enum constant in its declaration, starting from 0.

    • Return type: int

    ๐Ÿ“Œ Syntax :

    int ordinal() 
    โœ… Why Use ordinal()?
    • To get the relative position of a constant in the enum.
    • It is helpful when you need the position or order of the enum constant (e.g., for iteration, comparison, or sorting purposes).
      โš ๏ธ However, avoid using ordinal() for persistent logic (e.g., storing in databases) because adding or reordering constants changes ordinal values.

    Example :

    public enum Priority {
        LOW, MEDIUM, HIGH, CRITICAL;
    }
    public class OrdinalExample {
        public static void main(String[] args) {
            for (Priority level : Priority.values()) {
                System.out.println(level + " has ordinal: " + level.ordinal());
            }
        }
    }
    

    Output :

    LOW has ordinal: 0  
    MEDIUM has ordinal: 1  
    HIGH has ordinal: 2  
    CRITICAL has ordinal: 3  

    ๐Ÿ“Œ Notes :

    โš ๏ธ Donโ€™t use ordinal() for business logic that persists data (e.g., database values), because adding/reordering enum constants changes their ordinal values, which breaks the logic.

    valueOf(String name)

    The valueOf(String name) method is a static method automatically provided by the Java compiler for every enum.
    ๐Ÿ‘‰ It returns the enum constant that exactly matches the given string name

    • Returns the enum constant with the specified name.
    • The name must exactly match the declared constant (case-sensitive).
    • Return type: EnumType
    • Use case: Useful for converting a string into the corresponding enum constant.

    ๐Ÿ“Œ Syntax :

      public static EnumType valueOf(String name)
    โœ… Why Use valueOf(String name) in Enum?

    The valueOf(String name) method is used to convert a String into its corresponding Enum constant.

    โœ… Key Use Cases:
    1. User Input Conversion
      When a user provides input as a string (e.g., from a form, API, or command line), you can convert it to the corresponding enum constant for type-safe processing.
    2. Configuration Parsing
      Configuration files often store values as strings. Using valueOf(), you can convert these string values into enums for easy and reliable usage in your code.
    3. Validation
      Instead of using complex if-else chains to validate a string against a predefined set of constants, valueOf() allows direct validation by attempting the conversion (and handling exceptions if invalid).

    Example :

    public enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    }
    
    public class ValueOfExample {
        public static void main(String[] args) {
            String input = "FRIDAY";
            
            // Convert String to Enum constant
            Day day = Day.valueOf(input);
            
            System.out.println("Enum constant from String: " + day);
        }
    }
    
    
     

    Output :

    Enum constant from String: FRIDAY

    โš ๏ธ Important Note:

    • โœ” The input string must match the enum constant name exactly (case-sensitive).
    • โŒ Otherwise, it throws IllegalArgumentException
    • "monday" (lowercase) would fail because enum names are case-sensitive.
    • null input throws NullPointerException.

    โœ… So yes, valueOf(String name) is always available for enums in Java, either via the compiler-generated method or the generic Enum.valueOf() method.

    ๐Ÿ“ŒNote : 

    • ordinal() is defined in the java.lang.Enum class.
    • valueOf(String) is also defined in the Enum class. 
    • values() is not defined in Enum; instead, it is a synthetic method automatically generated by the compiler for each enum type. Thatโ€™s why you wonโ€™t find it in the JDK source of Enum.

    โœ… Key Differences :

    Featureordinal()valueOf(String name)values()
    PurposeReturns index (position) of constantReturns enum constant by its nameReturns an array of all enum constants
    Return TypeintEnum type itselfEnum array (EnumType[])
    InputNo input (called on enum constant)Takes String argumentNo input (called on enum type)
    Example UsageDay.MONDAY.ordinal()โ†’0Day.valueOf("MONDAY") โ†’ Day.MONDAYDay.values() โ†’ [MONDAY, TUESDAY, โ€ฆ]
    RiskChanges if enum order is modifiedThrows IllegalArgumentException if not foundNone (safe, but array order depends on enum order)

    โœ…Enum with Constructors and Fields

    Enums in Java are more powerful than simple lists of constants.

    • ๐Ÿ‘‰ You can define fields, constructors, and methods inside an enum to associate additional data with each constant.
    • ๐Ÿ‘‰Each enum constant is actually an object, and constructors are executed at class loading time.
    โœ… Why Use Enum Fields and Constructors?
    • To store additional information (metadata) for each enum constant.
    • Helps model real-world entities more effectively.
    • Makes the code cleaner, avoids external mappings (e.g., maps or switch cases).

    ๐Ÿ“Œ Syntax

    public enum EnumName {
      CONSTANT1(value1),
      CONSTANT2(value2);
    
      private DataType field; // Field to store additional data
      // Constructor
      EnumName(DataType field) {
        this.field = field;
      }
    
      // Getter
      public DataType getField() {
        return field;
      }
    }

    Example :

    Step 1 โ€“ Define the Enum(UserRole.java)

    public enum UserRole {
        ADMIN(1, "Full access to all system features"),
        MODERATOR(2, "Can manage user content and moderate discussions"),
        USER(3, "Standard user with limited access"),
        GUEST(4, "Read-only access to public content");
    
        private final int accessLevelCode;
        private final String description;
    
        // Constructor
        UserRole(int accessLevelCode, String description) {
            this.accessLevelCode = accessLevelCode;
            this.description = description;
        }
    
        // Getters
        public int getAccessLevelCode() {
            return accessLevelCode;
        }
    
        public String getDescription() {
            return description;
        }
    }

    Step 2 โ€“ Using the Enum in Application(UserAccessTest.java)

    public class UserAccessTest {
        public static void main(String[] args) {
            UserRole userRole = UserRole.MODERATOR;
    
            System.out.println("User Role: " + userRole);
            System.out.println("Access Level Code: " + userRole.getAccessLevelCode());
            System.out.println("Description: " + userRole.getDescription());
    
            // Display all Access Levels
            System.out.println("\nAll Access Levels:");
            for (UserRole role : UserRole.values()) {
                System.out.printf("%s (%d): %s%n",
                    role, role.getAccessLevelCode(), role.getDescription());
            }
        }
    }

    Output :

    User Role: MODERATOR
    Access Level Code: 2
    Description: Can manage user content and moderate discussions
    
    All Access Levels:
    ADMIN (1): Full access to all system features
    MODERATOR (2): Can manage user content and moderate discussions
    USER (3): Standard user with limited access
    GUEST (4): Read-only access to public content  
    

    ๐Ÿ“Œ Note :

    • ๐Ÿ‘‰ You cannot create enum objects manually (new UserRole() is not allowed). They are created internally at load time.
    • ๐Ÿ‘‰ This way, each UserRole constant represents a real application role with an access level (like permissions in a system).

    โœ… Why Is This Useful in Real Applications?

    • Instead of hardcoding access levels, this enum stores both a code (levelCode) and a description.
    • Helps manage user permissions in a type-safe and centralized way.
    • Easy to display in UI (dropdowns) or use in logic:
    if (userAccess == AccessLevel.ADMIN) {
        System.out.println("Grant full system access");
    }

    ๐Ÿ“ŒNote : Using fields and constructors in enums makes your code cleaner and models real-world scenarios more effectively than plain enums or constants.

    โœ…Enum with Methods

    An enum in Java can have fields, constructors, and methods just like a regular class.

    • ๐Ÿ‘‰ Methods allow adding behavior to enums, making them powerful for modeling real-world concepts.
    • ๐Ÿ‘‰Enums can override methods just like classes.
    โœ… Why Use Methods in Enum?
    • To provide logic related to the enum constants.
    • Avoids switch-case or if-else statements scattered across the code.
    • Encourages encapsulation and clean code structure.

    ๐Ÿ“Œ Syntax :

    public enum EnumName {
        CONSTANT1, CONSTANT2;
        
        // Custom method
        public ReturnType methodName() {
            // Logic
        }
    }
    

    Enum Example with Methods and Override

    public enum NotificationType {
        EMAIL {
            @Override
            public void send() {
                System.out.println("Sending Email Notification...");
            }
        },
        SMS {
            @Override
            public void send() {
                System.out.println("Sending SMS Notification...");
            }
        },
        PUSH {
            // Uses default implementation
        };
    
        // Default behavior
        public void send() {
            System.out.println("Sending Push Notification...");
        }
    }
    //Main class
    public class Test {
        public static void main(String[] args) {
            for (NotificationType type : NotificationType.values()) {
                type.send();
            }
        }
    }

    Output :

    Sending Email Notification...  
    Sending SMS Notification...  
    Sending Push Notification...  

    โœ… Explanation

    • Each enum constant (EMAIL, SMS) overrides the send() method with its own implementation.
    • The PUSH constant uses the default implementation of send().
    • This pattern is useful when different constants require different behaviors.

    โœ…Static Import with Enum

    What Is Static Import in Java?

    Normally, to access enum constants, you use the full reference like:

    Day.MONDAY
    • ๐Ÿ‘‰ The static import feature allows you to import static members (including enum constants) directly, so you donโ€™t need to prefix them with the enum type every time.
    • ๐Ÿ‘‰ You can use static imports to avoid qualifying enum constants.

    โœ… Why Use Static Import with Enum?

    • ๐Ÿ‘‰ Improves code readability by avoiding repetitive enum type references.
    • ๐Ÿ‘‰ Makes code cleaner when using enums frequently (like in switch cases or comparisons).

    ๐Ÿ“Œ Syntax of Static Import

    import static packageName.EnumType.*;

    This imports all enum constants statically.

    Or, import a specific constant:

    import static packageName.EnumType.CONSTANT_NAME;

    Example :

    package pack1;
    public enum Fish {
        STAR, GUPPY;
    }
    package pack2;
    import static pack1.Fish.*;
    
    class A {
        public static void main(String[] args) {
            System.out.println(STAR);
            System.out.println(GUPPY);
        }
    }

    Valid imports:

    1. import static pack1.Fish.*; โœ…
    2. import static pack1.Fish.STAR; โœ…

    Invalid imports:

    1. import pack1.*; โŒ
    2. import pack1.Fish; โŒ

    ๐Ÿ“Important

    • โš ๏ธ Avoid overusing static imports for large enums in complex projects, as it can reduce code clarity if multiple enums have overlapping constant names.
    • โœ… Recommended for enums with small, well-defined constants frequently used in the same context.

    ๐ŸŽฏKey Points (Latest Java Versions โœ…)

    • Enums are type-safe constants.
    • They can have fields, methods, and constructors.
    • They cannot extend other classes, but can implement interfaces.
    • Enums are thread-safe since all constants are created at class loading.
    • From Java 5 onwards, enums can be used in switch.
    • Useful methods: values()ordinal()name()compareTo().
    • They work seamlessly with Collections, Generics, and Streams (Java 8+).

    ๐Ÿ“ŒYou can also explore how to identify enum types using the isEnum() method in Java.

  • Table of Contents

    Java Basics


    Object Oriented Programming (OOPs)


    Java String Handling


    Exception Handling

    • exception-handling-in-java
    • exception-vs-error-in-java
    • checked-vs-unchecked-exception
    • try-catch-finally-in-java
    • user-defined-exceptions-in-java
    • final-vs-finally-vs-finalize-in-java
    • return-statement-in-try-catch-finally
    • throw-keyword-in-java
    • throw-vs-throws-in-java
    • multiple-catch-blocks-in-java
    • try-block-without-catch-block
    • different-ways-to-print-exception-message

    Wrapper Classes


    Object Class

    • object-class-in-java

    Collections Framework


    Other Core Concepts

    • inner-classes-in-java

    Enum in Java

  • What Is Java? Complete Introduction to Java Programming Language

    Java is a programming language developed by James Gosling and his team at Sun Microsystems in 1995. Initially named Oak, after an oak tree outside Gosling’s office, it was later renamed Java due to trademark issues, with the new name inspired by Java coffee.

    ๐Ÿ“Œ Notes:

    • Development of Java started in 1991 under the “Green Project.”
    • It was officially released in 1995.

    In 2010, Oracle Corporation acquired Sun Microsystems, and since then Oracle has been responsible for the continued development, stewardship, and support of Java.

    The main goal of Java is โ€œwrite once, run anywhereโ€ (WORA) โ€“ meaning a program written in Java can run on multiple operating systems (such as Windows, Linux, and macOS) without modification, provided a compatible Java Virtual Machine (JVM) is available.

    • Initial Release: Java 1.0 (January 1996)
    • Major Milestones:
      • Java 2 (J2SE 1.2, 1998): Introduced Swing, Collections, strict JVM specification.
      • Java 5 (2004): Added generics, annotations, enhanced for-loop, autoboxing.
      • Java 8 (2014): A landmark release introducing Lambda expressions, Streams API, and Date/Time API.
      • Java 9 (2017): Introduced the Module System (Project Jigsaw).
      • Java 10 (March 2018): Added var keyword for local variable type inference.
      • Java 11 (September 2018, LTS): Removed Applets, added new HTTP Client API, and became a Long-Term Support (LTS) version.
      • Java 17 (September 2021, LTS): Introduced pattern matching, sealed classes, enhanced switch expressions.
      • Java 21 (September 2023, LTS): The latest LTS version as of 2025, featuring Virtual Threads (Project Loom), record patterns, string templates, and significant JVM improvements.

    Many enhanced versions of Java have been released over the years, bringing improvements in performance, security, scalability, cloud support, and developer productivity. (โš ๏ธ Note: The latest long-term support (LTS) version as of 2025 is Java 21, released in September 2023.)

    Setting Up the Java Environment

    Learn how to set up the Java environment on your system to start building and running Java applications smoothly.

    How to Download and Install Java Development Kit (JDK) and Java Runtime Environment (JRE)

    In this๐Ÿ‘‰ step-by-step guide to downloading and installing the latest JDK and JRE, youโ€™ll learn how to set up Java for Windows, macOS, and Linux, configure environment variables, and verify your installation for seamless Java programming and application execution.

    Overview of Integrated Development Environments (IDEs) like Eclipse, IntelliJ IDEA, and NetBeans

    Explore the most popular Java IDEsโ€”Eclipse, IntelliJ IDEA, and NetBeansโ€”and understand their features for faster and more efficient coding.

    Instructions for Setting Up the PATH Environment Variable and Verifying Installation

    ๐Ÿ‘‰Follow this detailed guide on how to set up the Java PATH environment variable to configure Java correctly and verify the installation on your system

    Key Features of Java

    Java was designed with the following important properties:

    • Simple
    • Platform Independent
    • Object-Oriented
    • Portable
    • Robust
    • Secure
    • Multithreaded

    ๐Ÿ”นSimple

    Java is easy to learn and has a clean, simple syntax, making it ideal for beginners. It removed complex features like pointers and operator overloading found in C and C++, which helps prevent errors. Java uses automatic ๐Ÿ‘‰garbage collection, so developers donโ€™t need to manually delete unused objects. This simplifies memory management and improves stability, making Java productive and reliable for various applications.

    ๐Ÿ”น Platform Independent

    Platform independence refers to the ability of a program to run on any operating system (Windows, Mac, Linux, etc.) without changes to the source code. Java achieves platform independence through the ๐Ÿ‘‰Java Virtual Machine (JVM). The compiler converts Java code into bytecode, which can be executed on any machine that has a JVM installed. This is made possible by separating the compilation and execution environments using the platform-independent bytecode.

    ๐Ÿ”นObject-Oriented Programming Language

    Java is an object-oriented programming language where everything is treated as an object. It organizes software by combining data and behavior into objects. Each object represents a real-world entity with attributes (data) and methods (behavior). This approach makes code more modular, reusable, and easier to maintain.

    The four main OOP principles in Java are:

    1. ๐Ÿ‘‰Abstraction
    2. ๐Ÿ‘‰Encapsulation
    3. ๐Ÿ‘‰Inheritance
    4. ๐Ÿ‘‰Polymorphism

    ๐Ÿ”นPortable

    Java is portable because its compiled bytecode can be executed on any platform that supports the JVM. This means code written on one machine can run on another without modification.

    ๐Ÿ”นRobust

    Java is considered robust because it has strong memory managementgarbage collection, and exception handling, reducing the chances of crashes and errors.

    ๐Ÿ”นSecure

    Java is designed with security in mind. Since it does not use pointers (which can lead to memory access vulnerabilities) and runs inside the JVM sandbox, it prevents unauthorized access and ensures a safer execution environment.

    ๐Ÿ”นMultithreaded

    Java supports multithreading, allowing developers to write programs that can perform multiple tasks simultaneously.Threads in Java share a common memory area, making it efficient and suitable for high-performance applications.


    ๐Ÿ“Œ Important Notes:

    • Since Java 9, Oracle follows a time-driven release cycle with a new version every 6 months (March & September).
    • LTS (Long-Term Support) versions are released every 3 years (e.g., Java 8, 11, 17, 21). Enterprises typically adopt LTS versions for stability.
    • Java continues to be widely used in enterprise applications, Android development, cloud computing, big data, and microservices.