Java is a high-level, object-oriented, platform-independent programming language used for developing web, desktop, mobile, and automation applications.

  • Platform independent
  • Object-oriented
  • Secure
  • Robust
  • Multithreaded
  • Portable

Variables are containers used to store data values.

Example:

int age = 25;

Two types:

  • Primitive → int, char, boolean
  • Non-primitive → String, Array, Class
PrimitiveNon-Primitive
Stores valueStores reference
Fixed sizeDynamic size

Converting one datatype into another datatype.

Example:

double d = 10.5;
int i = (int)d;

Operators perform operations on variables.

Examples:

  • Arithmetic (+)
  • Logical (&&)
  • Relational (>)
==.equals()
Compares memory referenceCompares values
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation means wrapping data and methods together in a single class and restricting direct access using private variables.

Inheritance allows one class to acquire properties and methods of another class using extends keyword.

Polymorphism means one method can perform different actions based on the object.
Types:

  • Overloading
  • Overriding

Abstraction means hiding implementation details and showing only required functionality.

AbstractionEncapsulation
Hides implementationHides data

Method overloading means creating multiple methods with same name but different parameters.

Method overriding means redefining parent class method in child class with same signature.

OverloadingOverriding
Same classParent-child class
Different parametersSame parameters
Compile-time polymorphismRuntime polymorphism

An interface is a blueprint containing abstract methods that must be implemented by classes.

An abstract class is a class declared using abstract keyword. It can contain abstract and non-abstract methods

InterfaceAbstract Class
Supports multiple inheritancePartial abstraction
Mostly abstract methodsCan contain concrete methods

Constructor initializes object.

A constructor is a special method used to initialize objects.

Creating multiple constructors with different parameters is called constructor overloading

ConstructorMethod
Initializes objectPerforms operations
Same name as classAny valid name

Refers to current class object.

super refers to parent class object or constructor.

static belongs to class instead of object.

Used to:

  • Prevent inheritance
  • Prevent overriding
  • Declare constants

No, static and final methods cannot be overridden.

String is a sequence of characters used to store text.

StringStringBufferStringBuilder
ImmutableMutableMutable
SlowThread-safeFaster

For security, thread safety, and performance.

Java stores string literals in common memory called String Pool.

equals()equalsIgnoreCase()
Case-sensitiveIgnores case

Using StringBuilder:

String rev = new StringBuilder(str).reverse().toString();

Duplicates can be removed using loops, Set collection, or HashSet.

Using length() method.

str.length();

An array is a collection of elements of the same datatype stored in a single variable. It is used to store multiple values together using index positions.

ArrayList is a class in Java Collection Framework used to store dynamic collections of elements. Unlike arrays, its size can grow or shrink automatically.

ArrayArrayList
Fixed sizeDynamic size
Stores primitivesStores objects

Stores key-value pairs

HashMapHashtable
Not synchronizedSynchronized
HashSetTreeSet
UnorderedSorted

Used to traverse collection elements.

IteratorListIterator
Forward onlyForward & backward

Framework containing classes/interfaces for storing data.

ComparableComparator
Natural sortingCustom sorting
ArrayListLinkedList
Faster retrievalFaster insertion/deletion

Mechanism to handle runtime errors.

CheckedUnchecked
Compile-timeRuntime

Used to handle exceptions.

Always executes whether exception occurs or not.

throwthrows
Throws exceptionDeclares exception

Yes.

Occurs when accessing a null object.

Passing an exception from one method to another.

Reading and writing files using Java classes.

Using:

  • BufferedReader
  • Scanner
  • FileReader

Using FileWriter or BufferedWriter.

Used to read text efficiently.

Used to read/write byte data.

Executing multiple threads simultaneously.

Smallest unit of execution.

ProcessThread
Independent executionPart of process

Using:

  • Thread class
  • Runnable interface

Prevents multiple threads from accessing shared resources simultaneously.

Pauses thread execution temporarily.