JAVA PROGRAMMING
Complete Notes — Core to Advanced Java Concepts
Java is a class-based, object-oriented, statically-typed language designed to have as few
implementation dependencies as possible (WORA — Write Once, Run Anywhere). It runs on the Java
Virtual Machine (JVM).
1. Java Fundamentals
1.1 Program Structure
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
1.2 Data Types
• byte (8-bit), short (16-bit), int (32-bit), long (64-bit)
• float (32-bit), double (64-bit)
• char (16-bit Unicode), boolean (true/false)
• String — reference type (not primitive)
int age = 25;
double salary = 75_000.00;
boolean active = true;
String name = "Alice";
1.3 Type Casting
int x = (int) 3.99; // 3 — narrowing
double d = 5; // 5.0 — widening (auto)
Note: Java is strongly typed. Narrowing casts must be explicit to prevent data loss.
2. Control Flow
// if-else
if (score >= 90) {
[Link]("A");
} else if (score >= 75) {
[Link]("B");
} else {
[Link]("C");
}
// switch
switch (day) {
case 1: [Link]("Mon"); break;
case 2: [Link]("Tue"); break;
default: [Link]("Other");
}
2.1 Loops
// for
for (int i = 0; i < 5; i++) [Link](i);
// while
int n = 10;
while (n > 0) { [Link](n--); }
// enhanced for
int[] arr = {1,2,3};
for (int val : arr) [Link](val);
3. Object-Oriented Programming
public class Animal {
private String name;
protected int age;
public Animal(String name, int age) {
[Link] = name;
[Link] = age;
}
public String getName() { return name; }
public String speak() {
return name + " makes a sound.";
}
}
public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
@Override
public String speak() {
return getName() + " barks!";
}
}
3.1 OOP Pillars
• Encapsulation — hide data with private fields & getters/setters
• Inheritance — extend classes to reuse behaviour
• Polymorphism — same interface, different implementations
• Abstraction — abstract classes and interfaces
4. Interfaces & Abstract Classes
interface Shape {
double area();
default String describe() {
return "I am a shape with area " + area();
}
}
abstract class Polygon implements Shape {
int sides;
Polygon(int sides) { [Link] = sides; }
}
class Circle implements Shape {
double radius;
Circle(double r) { radius = r; }
public double area() { return [Link] * radius * radius; }
}
Note: Interfaces support multiple implementation (a class can implement many interfaces).
5. Collections Framework
import [Link].*;
// ArrayList
List list = new ArrayList<>();
[Link]("Alice");
[Link]("Bob");
[Link](list);
// HashMap
Map scores = new HashMap<>();
[Link]("Alice", 95);
[Link]("Bob", 87);
for ([Link] e : [Link]())
[Link]([Link]()+": "+[Link]());
// HashSet
Set set = new HashSet<>([Link](1,2,3,2,1));
6. Exception Handling
try {
int[] arr = new int[5];
arr[10] = 50; // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Index error: " + [Link]());
} catch (Exception e) {
[Link]("General error");
} finally {
[Link]("Always runs");
}
// Custom exception
class InsufficientFundsException extends Exception {
InsufficientFundsException(String msg) { super(msg); }
}
7. Generics & Streams (Java 8+)
7.1 Generics
public class Box {
private T value;
public void set(T val) { value = val; }
public T get() { return value; }
}
Box intBox = new Box<>();
[Link](42);
7.2 Stream API
import [Link].*;
List nums = [Link](1,2,3,4,5,6,7,8,9,10);
int sumOfEvens = [Link]()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum(); // 30
List names = [Link]("Bob","Alice","Charlie");
[Link]()
.sorted()
.forEach([Link]::println);
Java powers enterprise systems, Android apps, and large-scale backend services. Its type safety, rich
ecosystem (Maven/Gradle, Spring, Hibernate), and JVM portability make it one of the most in-demand
languages globally.