🚀 Introduction to Java
Discover what Java is, its rich history, core features, and how the Java ecosystem operates.
What is Java?
Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
History of Java
Java was developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. Initially called "Oak", it was later renamed to Java. Sun Microsystems was later acquired by Oracle Corporation in 2010.
Key Java Features
- Platform Independent: Java code is compiled into bytecode, which is platform-independent and can be run on any machine with a JVM.
- Object-Oriented: Everything in Java is an object (except primitives). It models real-world entities using objects.
- Robust: Strong memory management, automatic garbage collection, and exception handling make Java extremely reliable.
- Secure: Java provides a secure execution environment, preventing unauthorized memory access and minimizing virus threats.
- Multithreaded: Java supports multithreading out-of-the-box, allowing concurrent execution of multiple tasks.
- Distributed: Java facilitates building distributed applications over networks using RMI and EJB.
JVM, JRE, and JDK Differences
To understand the Java ecosystem, you must know the difference between the JVM, JRE, and JDK:
- JVM (Java Virtual Machine): An abstract machine that enables your computer to run a Java program. It converts Java bytecode into machine-specific code.
- JRE (Java Runtime Environment): Contains the JVM plus Java libraries and other components needed to run Java applications.
- JDK (Java Development Kit): A full featured SDK for Java. It contains everything the JRE has, plus development tools like the compiler (
javac) and debugger.
[JDK] -> Contains [JRE] + Development Tools (javac, jdb)
[JRE] -> Contains [JVM] + Library Classes
[JVM] -> Executes the bytecode
Java Editions
| Edition | Description |
|---|---|
| Java SE (Standard Edition) | Core Java platform for general-purpose desktop and server environments. |
| Java EE (Enterprise Edition) | Built on top of Java SE, used for developing large-scale, distributed enterprise applications. |
| Java ME (Micro Edition) | Designed for resource-constrained devices like mobile phones and embedded systems. |
The Compilation Process & Bytecode
When you write Java code, it is saved in a .java file. The Java compiler (javac) compiles this file into a .class file, which contains bytecode. Bytecode is a highly optimized set of instructions designed to be executed by the JVM.
public class HelloWorld { public static void main(String[] args) { // Print Hello World to the console System.out.println("Hello, World!"); } }
Line-by-Line Breakdown:
public class HelloWorld: Defines a class named HelloWorld. In Java, all code must reside inside a class.public static void main(String[] args): The entry point of the application. The JVM looks for this exact method signature to start execution.System.out.println("Hello, World!");: A statement that prints the specified text to the standard output (console).