1.
Main Features of Java
Java is a high-level, object-oriented, platform-independent programming language. Key
features include:
✅ Platform Independence – "Write Once, Run Anywhere" (WORA) because Java programs run
on the Java Virtual Machine (JVM).
✅ Object-Oriented – Everything in Java is based on objects and classes.
✅ Robust & Secure – Automatic memory management (Garbage Collection) and security
features.
✅ Multi-threaded – Supports concurrent programming with threads.
✅ High Performance – Uses the JIT compiler for better execution speed.
✅ Dynamic & Portable – Can run on different devices without modification.
2. How Does Java Achieve Platform Independence?
Java achieves platform independence through bytecode and the JVM:
1️⃣ Compilation: Java source code (.java files) is compiled into bytecode (.class files).
2️⃣ Execution: Bytecode is not machine-specific; instead, it runs on the JVM, which translates it
into machine code for the specific system.
3️⃣ Since JVMs exist for different operating systems, the same Java program can run on
Windows, macOS, or Linux without modification.
3. Difference Between JDK, JRE, and JVM
Component Description
JDK (Java Development Includes everything: compiler, JRE, and development tools for writing,
Kit) compiling, and debugging Java programs.
JRE (Java Runtime Contains JVM and libraries required to run Java applications but
Environment) doesn’t include the compiler.
JVM (Java Virtual Converts bytecode into machine code and executes Java programs. It is
Machine) part of the JRE.
4. What Are Bytecode and the Just-In-Time (JIT) Compiler?
🔹 Bytecode:
Intermediate code generated after Java compilation.
Platform-independent (.class files).
Executed by JVM, not directly by the OS.
🔹 JIT Compiler:
A part of the JVM that improves performance.
Instead of interpreting bytecode line-by-line, it compiles it into native machine code for
faster execution.
5. Data Types in Java
Java has two categories of data types:
1️⃣ Primitive Data Types (store values directly)
Type Size Example
byte 1 byte byte b = 100;
short 2 bytes short s = 5000;
int 4 bytes int num = 100000;
long 8 bytes long big = 100000L;
float 4 bytes float pi = 3.14f;
double 8 bytes double e = 2.718;
char 2 bytes char letter = 'A';
boolean 1 bit boolean isTrue = true;
2️⃣ Non-Primitive Data Types (store references to objects)
String → String name = "Java";
Arrays → int[] arr = {1, 2, 3};
Classes, Objects, Interfaces, Enums
What is a Wrapper Class in Java?
A wrapper class in Java is an object representation of a primitive data type. Java provides
wrapper classes for each primitive type, allowing them to be used as objects.
Why Do We Need Wrapper Classes?
1. Object Manipulation: Many Java APIs work with objects instead of primitives. Wrapper
classes enable primitive data types to be treated as objects.
2. Collection Framework Compatibility: Java’s Collection Framework (e.g., ArrayList,
HashMap) only works with objects, not primitive types.
3. Utility Methods: Wrapper classes provide useful utility methods like parseInt(),
valueOf(), etc.
4. Synchronization: Objects can be synchronized, but primitive types cannot.
5. Serialization: Only objects can be serialized (converted into a byte stream for storage or
transmission).
6. What is Boxing?
Boxing refers to the conversion of a primitive type into its corresponding wrapper
class object.
Manual Boxing (Before Java 5)
Before Java 5, developers had to explicitly create objects of wrapper classes.
What is Unboxing?
Unboxing is the reverse of boxing, meaning the conversion of a wrapper object back
to its primitive type.
Manual Unboxing (Before Java 5)
Before Java 5, explicit conversion was required.
Autoboxing occurs when performing operations where a wrapper class is expected.
java
CopyEdit
Integer a = 5; // Autoboxing (int → Integer)
Integer b = 10; // Autoboxing (int → Integer)
// Java automatically unboxes and performs addition
Integer sum = a + b; // Autoboxing in assignment
[Link](sum); // Output: 15
Unboxing in Conditional Statements
Java automatically unboxes objects in conditions.
java
Integer val = 100;
if (val > 50) { // Unboxing occurs here (Integer → int)
[Link]("Value is greater than 50");
}
Performance Overhead
Autoboxing and unboxing involve object creation, which can lead to performance
overhead.
Excessive boxing/unboxing can cause unnecessary object creation, impacting memory
and speed.
Therefore integer caching mechanism comes into picture
Implicit Type Casting (Widening)
Smaller data types are automatically converted to larger data types.
No data loss occurs.
Happens automatically because there is no risk of precision loss.
Example: Implicit Casting
public class Main {
public static void main(String[] args) {
int num = 100;
double d = num; // Implicit casting: int to double
[Link](d); // Output: 100.0
}
}
✅ Conversion Order (Widening)
byte → short → int → long → float → double
2️⃣ Explicit Type Casting (Narrowing)
Larger data types must be explicitly converted to smaller data types.
Data loss can occur due to precision reduction.
Requires manual casting using (dataType) syntax.
Example: Explicit Casting