Introduction to Java
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
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:
⭐ 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.
Runtime environment that executes Java bytecode
Platform Dependent - Different JVM for each OS
Intermediate representation of Java code (.class files)
Platform Independent - Same bytecode runs on any JVM
Original Java program written by developers (.java files)
Compiled to bytecode using javac compiler
๐ง How JVM Works
- Loading - ClassLoader loads the .class file into memory
- Linking - Verifies, prepares, and resolves references
- Initialization - Initializes static variables and executes static blocks
- Execution - Interpreter/JIT compiler executes the bytecode
- 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.
Contains everything needed to run Java applications
Executes Java bytecode
Standard Java classes (java.lang, java.util, etc.)
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.
Complete development environment for Java
Runtime environment to execute Java programs
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
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:
๐ 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
For Java Developers
For End Users
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.
Comments
Post a Comment