Java Overview
Last updated
Was this helpful?
Last updated
Was this helpful?
Java is a high-level, object-oriented programming (OOP) language developed by Sun Microsystems (now owned by Oracle Corporation) in 1995. It was designed to be platform-independent, secure, and easy to use. Java has since become one of the most popular programming languages in the world, widely used for developing web, mobile, desktop, and enterprise applications. Java is case sensitive.
Java source code is written in plain text files with a .java
extension. The compilation process of Java involves several steps:
Writing Code: Developers write Java code using a text editor or an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, etc.
Compilation: The Java source code is compiled using the Java compiler (javac
) into bytecode. Bytecode is a platform-independent intermediate representation of the program. This bytecode is saved in files with a .class
extension.
Bytecode: The generated bytecode contains instructions that can be executed by the Java Virtual Machine (JVM).
The JVM is a crucial component of the Java platform. It's responsible for executing Java bytecode and providing various runtime services. Here's how it works:
Class Loader Subsystem: Loads classes and interfaces as they are referenced by the Java program. It consists of three subsystems: the Bootstrap Class Loader, the Extension Class Loader, and the Application Class Loader.
Runtime Data Area: JVM divides memory into several runtime data areas, including Method Area, Heap, Java Stack, PC Register, and Native Method Stack. These areas are used for different purposes such as storing class metadata, objects, and method invocation records.
Execution Engine: It interprets bytecode or, in some cases, compiles it into native machine code for better performance. The execution engine includes the Just-In-Time (JIT) compiler, which dynamically compiles frequently executed bytecode into native machine code to improve execution speed.
Native Interface: JVM provides a native interface to interact with native libraries and code written in other programming languages like C and C++.
Garbage Collector: Manages the memory used by Java objects. It automatically deallocates memory occupied by objects that are no longer in use, preventing memory leaks and optimizing memory usage.
The JVM is available for different platforms (Windows, Linux, macOS, etc.), allowing Java programs to run on any system that has a compatible JVM installed.
In a statically-typed language like Java:
The type of a variable is known at compile time.
We must explicitly declare the data type of a variable, and the type is checked by the compiler.
Once a variable is declared with a specific type, it cannot hold data of any other type without explicit conversion (casting).
Variable Declaration: Every variable must have a type specified during declaration.
Compile-Time Type Checking: The Java compiler ensures that operations on variables are type-safe during compilation.
Type Safety: Java ensures that you don't accidentally assign the wrong type of data to a variable, reducing bugs and runtime errors.
Method Signatures Method arguments and return types must have explicit types.
Early Error Detection: Errors are caught during compile-time rather than runtime, making the code safer.
Code Predictability: Knowing the types makes the code easier to understand and predict.
Improved Performance: Because types are known at compile time, the JVM can optimize performance better.
Enhanced Tooling Support: IDEs can provide better autocomplete, refactoring tools, and error detection.