Data Type
Variable
Feature Local Variable Instance Variable Static Variable
Declared In Inside a method, Inside a class, but outside Inside a class, with static
constructor, or block any method keyword
Scope Only within the Accessible by all non- Accessible via class name,
method/block where static methods of the shared across all objects
declared class
Lifetime Exists while the Exists as long as the Exists for the entire program
method/block executes object exists (till class is unloaded)
Default No (must be initialized Yes (default values: 0, Yes (default values: 0, false,
Value before use) false, null, etc.) null, etc.)
Memory Stored in stack memory Stored in heap (per Stored in method area
object) (common memory)
Access Cannot use access Can use access modifiers Can use access modifiers
modifiers (public, etc.)
Shared? Not shared Not shared (each object Shared by all objects of the
has its own copy) class
Operators, Literals & Keywords
Identifiers in Java
What Is Java || History Of Java
What is Java?
Java is a high-level, object-oriented, platform-independent
programming language developed by Sun Microsystems (now
owned by Oracle).
It follows the principle “Write Once, Run Anywhere” (WORA) –
meaning compiled Java code can run on any platform with a Java
Virtual Machine (JVM).
Used for web applications, desktop apps, mobile apps
(Android), enterprise software, and more.
Key Features of Java:
Object-Oriented – Uses classes and objects for modular, reusable
code.
Platform-Independent (JVM) – Code runs on any system with
JVM, ensuring portability.
Simple & Secure – Easy to learn (similar to C/C++) with strong
security features like bytecode verification and no explicit pointers.
Multithreaded – Supports concurrent execution of multiple tasks.
Robust & Portable – Handles errors well, automatic memory
management (Garbage Collection), and runs consistently across
platforms.
History of Java:
1991 – Project started by James Gosling and team at Sun
Microsystems. Initially called Oak (named after an oak tree outside
Gosling’s office).
1995 – Renamed to Java (inspired by Java coffee).
1996 – Java 1.0 released – focused on applets for web browsers.
2004 – Java 5 introduced major features (Generics, Annotations,
Enum).
2010 – Oracle acquired Sun Microsystems, becoming the owner of
Java.
Present – Java continues to evolve (latest long-term support
version is Java 17, newer versions like 21 also released with modern
features).
JDK, JRE, and JVM
1. JVM (Java Virtual Machine)
What it is: A virtual machine that runs Java bytecode.
Purpose: Makes Java platform-independent (same code runs on
any OS with a JVM).
Functions:
o Loads, verifies, and executes bytecode.
o Manages memory (Garbage Collection).
o Provides security and exception handling.
Type: Software-based specification; different OS have different JVM
implementations.
2. JRE (Java Runtime Environment)
What it is: A package that provides everything needed to run Java
programs.
Includes:
o JVM
o Core libraries (like java.lang, java.util)
o Other supporting files.
Does not include: Tools for development (compiler, debugger,
etc.).
Use: End-users who just want to run Java applications install JRE.
3. JDK (Java Development Kit)
What it is: A full package to develop and run Java applications.
Includes:
o JRE (and thus JVM)
o Development tools like javac (compiler), javadoc, jar,
debuggers.
Use: Required by developers to write, compile, and run Java
programs.
Flow of execution of Java file
JVM Architecture in Java
Basic Topic
Control statement
1. Conditional (Decision-Making) Statements
2. Looping Statements (Iteration)
3. Jump Statements
Arrays
Array is the collection of homogeneous(or similar) data type, It is stored in
contiguous memory locations and has a fixed size.
In Java array is object | arr = new int[5]; // Creates an array of size 5
Declaration, Creation & Initialization
(a) Declaration
int[] arr; // Preferred way
int arr[]; // Also valid
int []arr; // Also valid
int[] arr; // Also valid
(b) Creation
arr = new int[5]; // Creates an array of size 5
(c) Initialization
arr[0] = 10;
arr[1] = 20;
(d) Declaration + Creation + Initialization
int[] arr = {10, 20, 30, 40, 50};
Accessing Elements
System.out.println(arr[2]); // Output: 30
which is correct
Find Duplicate Elements in an Array
Brute force method
Set interface
Advantages of Using Set
Removes Duplicates Automatically – No need for manual
comparison.
Faster Search Operations – HashSet operations (add, contains
Simple to Implement – Code is cleaner compared to nested loops.
3. Disadvantages of Using Set
No Order Guarantee – HashSet does not maintain insertion order
(use LinkedHashSet if needed).
Extra Memory Usage – Requires additional space for storing
unique elements.
Only Works for Objects – For primitive arrays, values are auto-
boxed to objects (slight overhead).
Java OOPs Concepts - Classes, Objects
and Method
Constructor in Java
A constructor is a special method used to initialize objects in Java. It is
called automatically when an object is created.
Key Points:
1. Name must be same as class name.
2. No return type, not even void.
3. Called automatically when new keyword is used.
4. Can be default, parameterized, or copy constructor (Java
doesn't have an explicit copy constructor, but you can define one).
Types of Constructors:
1. Default Constructor – No parameters.
2. Parameterized Constructor – Takes parameters to initialize object
values.
Inheritance (IS-A) in Java
Definition:
Inheritance is a mechanism in Java where one class (child/subclass)
acquires the properties and behaviors (fields and methods) of another
class (parent/superclass). It promotes code reusability and method
overriding.
Key Points:
1. Implemented using the extends keyword.
2. Supports single, multilevel, and hierarchical inheritance.
3. Multiple inheritance with classes is not supported (but can be
achieved with interfaces).
4. The super keyword is used to call the parent class constructor or
methods.
Relationship in Classes Java
Association (HAS-A) in Java
Definition:
Association in Java defines a relationship between two classes where one
class uses/has another class as a field. It represents a HAS-A
relationship.
Types of Association:
1. Aggregation (Weak Association) –
o Represents "Has-A" but both objects can exist
independently.
o Example: A Library has Books, but even if the library closes,
books can still exist.
2. Composition (Strong Association) –
o Represents "Has-A" but one object cannot exist without
the other.
o Example: A House has Rooms, and if the house is destroyed,
rooms don’t exist.
Polymorphism In Java
Polymorphism in Java means one entity (method or object) behaves
differently in different situations. It allows the same method or object
to take many forms.
Types of Polymorphism
1. Compile-Time Polymorphism (Static Polymorphism)
2. Run-Time Polymorphism (Dynamic Polymorphism)
1. Compile-Time Polymorphism (Method Overloading)
Achieved using method overloading (same method name,
different parameter list).
Decision is made at compile time.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls 2-arg method
System.out.println(calc.add(5, 10, 15)); // Calls 3-arg method
}
2. Run-Time Polymorphism (Method Overriding)
Achieved using method overriding (same method in parent and
child classes).
Decision is made at runtime.
Requires inheritance.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
public class Main {
public static void main(String[] args) {
Animal obj = new Dog(); // Parent reference, Child object
obj.sound(); // Calls Dog's method (Runtime decision)
Key Points
Polymorphism increases code reusability and flexibility.
Overloading → Compile-time, Overriding → Runtime.
In overriding, method signature must be the same.
Abstraction in Java
Abstraction in Java is the process of hiding implementation details
and showing only the essential features of an object.
It focuses on what an object does rather than how it does it.
How to Achieve Abstraction in Java
1. Abstract Classes (0–100% abstraction)
2. Interfaces (100% abstraction in Java 7, can include default & static
methods in Java 8+)
1. Abstract Class
Declared using abstract keyword.
Can have abstract methods (without body) and non-abstract
methods (with body).
Cannot be instantiated.
2. Interface
All methods are abstract by default (till Java 7).
Supports multiple inheritance.
Cannot have constructors or instance fields (before Java 8).
From Java 8+, can have default and static methods.
detailed difference between Abstract Class and
Interface in Java with version changes:
example showing Abstract Class vs Interface with version
changes:
1. Abstract Class Example (No Major Changes Across Versions)
abstract class Animal { // Abstract Class
String name;
Animal(String name) { // Constructor allowed
this.name = name;
abstract void sound(); // Abstract Method
void sleep() { // Concrete Method
System.out.println(name + " is sleeping");
class Dog extends Animal {
Dog(String name) {
super(name);
@Override
void sound() {
System.out.println(name + " says Woof!");
public class Main {
public static void main(String[] args) {
Dog d = new Dog("Buddy");
d.sound(); // Buddy says Woof!
d.sleep(); // Buddy is sleeping
Key Points:
Can have constructor, instance variables, abstract & concrete
methods.
Single inheritance only.
2. Interface Example (Java 7, Java 8, Java 9 Changes)
Java 7 and Below – Only Abstract Methods:
interface Vehicle {
void start(); // Always public and abstract
Java 8 – Added default and static methods:
interface Vehicle {
void start();
default void fuel() { // Default method
System.out.println("Filling fuel");
static void service() { // Static method
System.out.println("Servicing vehicle");
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start(); // Car started
c.fuel(); // Filling fuel
Vehicle.service(); // Servicing vehicle
}}
Java 9 – Added private methods:
interface Vehicle {
void start();
default void check() {
log(); // Using private helper method
System.out.println("Check complete");
private void log() { // Private method
System.out.println("Logging check...");
}
Key Points:
Java 8+: Interfaces can have default and static methods.
Java 9+: Interfaces can also have private methods.
Supports multiple inheritance (a class can implement many
interfaces).
Encapsulation in Java
Definition:
Encapsulation is the process of hiding the internal details of a class
and exposing only necessary functionality. It binds data (variables)
and methods (functions) together into a single unit (class) and controls
access using access modifiers.
Key Points
1. Use private variables to hide data.
2. Provide public getter and setter methods to access and update
data.
3. Improves security and data integrity.
4. Makes the code modular, maintainable, and flexible.
Exception Handling