UTIES Jamshedpur

Java
Complete Notes

From "Hello World" to advanced multithreading — everything you need to master Java.

🎬 Watch alongside! These notes complement the Java playlist on @utiesjsr YouTube channel. Every concept has a matching video explanation.
Introduction

What is Java?

☕ Java in Simple Words

Java is a high-level, object-oriented programming language developed by James Gosling at Sun Microsystems (now Oracle) and released in 1995. It follows the principle "Write Once, Run Anywhere" (WORA) — compiled Java code runs on any platform that has a Java Virtual Machine (JVM).

📱 Platform Independent

Same .class file runs on Windows, Mac, Linux, Android — no recompilation needed.

🏢 Most Popular Backend

Used in enterprise systems, Android apps, big data, banking, and cloud applications.

⚙️ How Java Works: Compilation & JVM

📝 .java (Source)
⚙️ javac (Compiler)
📦 .class (Bytecode)
🖥️ JVM → Runs!

JVM converts bytecode into machine code line by line (JIT compilation).

Architecture

JVM, JRE, JDK

The Three Pillars

ComponentWhat it does
JDKJava Development Kit — for developers (compiler + debugger + JRE)
JREJava Runtime Environment — runs Java programs (JVM + libraries)
JVMJava Virtual Machine — executes bytecode, manages memory (GC)
💡
JVM does: Class Loading · Bytecode Verification · Execution · Garbage Collection
Chapter 02

Syntax & Variables

First Java Program

public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
⚠️
File name must match class name: Main.javajavac Main.javajava Main

Variables & Naming

// Declaration + Initialization int age = 20; double price = 99.99; char grade = 'A'; boolean isJavaFun = true; String name = "Akarsh"; // String is a class
📖
Naming conventions: camelCase for variables/methods, PascalCase for classes, UPPER_SNAKE for constants.
Chapter 03

Data Types

Primitive vs Reference

TypeSizeExample
byte1 byte127
short2 bytes32767
int4 bytes2 billion
long8 bytes123456789L
float4 bytes3.14f
double8 bytes3.14159
char2 bytes'A'
boolean1 bittrue/false

Reference types: Arrays, Strings, Classes, Interfaces — stored in heap memory.

Chapter 04

Operators

Arithmetic, Logical, Bitwise

int a = 10, b = 3; a + b; a - b; a * b; a / b; a % b; // + - * / % a++; --b; // increment/decrement a == b; a != b; a > b; // relational → boolean (a > 5) && (b < 10); // logical AND (a > 5) || (b > 10); // logical OR int c = (a > b) ? a : b; // ternary
Chapter 05

Control Statements

if-else, switch

int score = 85; if (score >= 90) { System.out.println("A"); } else if (score >= 75) { System.out.println("B"); } else { System.out.println("C"); } // Switch (Java 14+ enhanced) switch (day) { case 1 -> System.out.println("Monday"); case 2 -> System.out.println("Tuesday"); default -> System.out.println("Invalid"); }
Chapter 06

Loops

for, while, do-while

// for loop for (int i = 0; i < 5; i++) { System.out.println(i); } // while int count = 0; while (count < 3) { System.out.println("Count: " + count); count++; } // for-each (enhanced) int[] nums = {1,2,3}; for (int n : nums) { System.out.print(n); }

📝 Loop Questions

Q1
Print multiplication table of N.
N=55,10,15...50
Q2
Check if number is prime.
17Prime ✅
Q3
Print Fibonacci series upto N terms.
70 1 1 2 3 5 8
Chapter 07

Arrays

One-D & Multi-Dimensional

// Declaration & initialization int[] numbers = new int[5]; int[] scores = {90, 85, 78}; System.out.println(scores[0]); // 90 // 2D array int[][] matrix = {{1,2},{3,4}}; for (int[] row : matrix) { for (int val : row) System.out.print(val); }
Chapter 08

Methods

Defining & Calling

public static int add(int a, int b) { return a + b; } // Method overloading (compile-time polymorphism) public static int add(int a, int b, int c) { return a + b + c; } // Call int sum = add(5, 3);
🔁
Java passes primitives by value, objects by reference value (reference copied).
Chapter 09

OOP Basics

Class & Object

class Car { String model; int year; // Constructor Car(String model, int year) { this.model = model; this.year = year; } void start() { System.out.println(model + " started!"); } } Car myCar = new Car("Tesla", 2024); myCar.start();
Chapter 10

Inheritance

extends keyword

class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } Dog d = new Dog(); d.eat(); d.bark();
🏷️
super() calls parent constructor. Java doesn't support multiple inheritance for classes — use interfaces instead.
Chapter 11

Polymorphism

Method Overriding (Runtime)

class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape { @Override void draw() { System.out.println("Drawing Circle ⚪"); } } Shape s = new Circle(); s.draw(); // Drawing Circle (runtime polymorphism)
Chapter 12

Abstraction

Abstract Classes

abstract class Vehicle { abstract void start(); // no body } class Bike extends Vehicle { void start() { System.out.println("Kick start"); } }
Chapter 13

Encapsulation

Getters & Setters

class BankAccount { private double balance; public void deposit(double amt) { if(amt > 0) balance += amt; } public double getBalance() { return balance; } }
Chapter 14

Interfaces

Contract for Classes

interface Drawable { void draw(); // public abstract by default } class Circle implements Drawable { public void draw() { System.out.println("Circle"); } } // Java 8+ default & static methods in interfaces interface Calculator { default int square(int x) { return x*x; } }
Chapter 15

Exception Handling

try-catch-finally

try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); } finally { System.out.println("Cleanup code"); } // throw & throws void validate(int age) throws IllegalArgumentException { if (age < 18) throw new IllegalArgumentException("Underage"); }
⚠️
Checked exceptions (IOException) must be handled; unchecked (RuntimeException) are optional.
Chapter 16

Collections Framework

List, Set, Map

import java.util.*; List list = new ArrayList<>(); list.add("Apple"); Set set = new HashSet<>(); set.add(10); Map map = new HashMap<>(); map.put("age", 25); // Iterate for (String s : list) System.out.println(s);
InterfaceImplementationsOrderUnique
ListArrayList, LinkedList✅ Insertion
SetHashSet, TreeSet❌ / ✅Sorted
MapHashMap, TreeMap❌ / ✅Keys ✅
Chapter 17

Multithreading

Thread & Runnable

// Extend Thread class MyThread extends Thread { public void run() { System.out.println("Thread running"); } } new MyThread().start(); // Implement Runnable Runnable task = () -> System.out.println("Lambda thread"); new Thread(task).start(); // Synchronization synchronized void increment() { count++; }
Chapter 18

File I/O

Reading & Writing Files

import java.nio.file.*; // Write Files.write(Paths.get("output.txt"), "Hello Java".getBytes()); // Read String content = Files.readString(Paths.get("output.txt")); // Try-with-resources (auto close) try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { String line = br.readLine(); }
Chapter 19

Advanced Topics

Lambda Expressions (Java 8+)

List names = Arrays.asList("A", "B", "C"); names.forEach(n -> System.out.println(n)); // Method reference names.forEach(System.out::println);

Stream API

List nums = Arrays.asList(1,2,3,4,5); int sum = nums.stream().filter(n -> n % 2 == 0).mapToInt(int::intValue).sum(); List squares = nums.stream().map(n -> n*n).collect(Collectors.toList());

Optional, Date/Time (java.time)

Optional opt = Optional.ofNullable(getValue()); opt.ifPresent(System.out::println); LocalDate today = LocalDate.now(); LocalDateTime dt = LocalDateTime.of(2025, 1, 15, 10, 30);

🎯 Advanced Practice

Q1 — Singleton
Implement a thread-safe Singleton pattern.
Q2 — Producer-Consumer
Use BlockingQueue to solve producer-consumer problem.
Q3 — Custom Annotations
Create a @NotNull annotation and process it via reflection.

Keep Coding

"Java is not just a language — it's an ecosystem. Master the fundamentals, and you'll build anything from Android apps to enterprise servers. We'll cover all of this in the UTIES Java series. Much love! 💙"

UTIES Coding School