Java Programming – University Level – Pak Notes Hub
☕ University Level — BS CS / BS IT

Java Programming
Complete Notes

Basics to Advanced · OOP · Collections · Multithreading — All in Easy English

☕ 15 Units 🎓 University Level 💻 Code Examples 📝 Practice Tasks 🚀 Final Project
Unit 1

Introduction to Java

What is Java and why is it everywhere?

What is Java?

Java is a high-level, object-oriented programming language developed by James Gosling at Sun Microsystems (now Oracle) in 1995. It's designed to be platform-independent and follows the principle of "Write Once, Run Anywhere" (WORA).

💡 Java is named after Java coffee! The developers loved coffee and chose the name while brainstorming at a local coffee shop.

Key Features of Java

✅ Platform Independent
🎯 Object-Oriented
🔒 Secure
⚡ Robust & Reliable
🧵 Multithreaded
🌐 Distributed

How Java Works — JVM, JRE, JDK

ComponentFull NamePurpose
JVMJava Virtual MachineExecutes Java bytecode, provides platform independence
JREJava Runtime EnvironmentJVM + libraries needed to run Java programs
JDKJava Development KitJRE + development tools (compiler, debugger)

Java Program Execution Flow

Source Code (.java)
    ↓
javac compiler
    ↓
Bytecode (.class)
    ↓
JVM
    ↓
Machine Code (platform-specific)

Your First Java Program

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

// Output: Hello, World!

Java vs Other Languages

FeatureJavaC++Python
PlatformPlatform IndependentPlatform DependentPlatform Independent
MemoryAutomatic (Garbage Collection)ManualAutomatic
SpeedFastFasterSlower
SyntaxModerateComplexSimple
Use CaseEnterprise, AndroidSystem software, gamesAI, web, scripting
✏️ Practice: Install JDK on your computer. Write and compile a program that prints your name and university. Run it using java command.
Unit 2

Variables & Data Types

Storing and managing data in Java.

Variables in Java

A variable is a named container that stores data. In Java, you must declare the variable type before using it.

int age = 20;
double price = 99.99;
String name = "Ali";
boolean isStudent = true;

Data Types in Java

TypeSizeRange/ValuesExample
byte1 byte-128 to 127byte b = 100;
short2 bytes-32,768 to 32,767short s = 5000;
int4 bytes-2³¹ to 2³¹-1int num = 12345;
long8 bytes-2⁶³ to 2⁶³-1long l = 100000L;
float4 bytes~7 decimal digitsfloat f = 3.14f;
double8 bytes~15 decimal digitsdouble d = 3.14159;
char2 bytesUnicode characterchar c = 'A';
boolean1 bittrue or falseboolean b = true;

Type Casting

Widening (Automatic): byte → short → int → long → float → double

int i = 100;
double d = i;  // Automatic casting

Narrowing (Manual): double → float → long → int → short → byte

double d = 99.99;
int i = (int) d;  // i = 99 (decimal part lost)

Constants (final keyword)

final int MAX_AGE = 100;
// MAX_AGE = 200;  // Error! Cannot modify
✏️ Practice: Declare variables for: student name (String), roll number (int), CGPA (double), and pass status (boolean). Print all values.
Unit 3

Operators in Java

Performing operations on data — arithmetic, logical, and more.

Types of Operators

TypeOperatorsExample
Arithmetic+, -, *, /, %5 + 3 = 8
Relational==, !=, >, <, >=, <=5 > 3 → true
Logical&& (AND), || (OR), ! (NOT)true && false → false
Assignment=, +=, -=, *=, /=x += 5
Unary++, --, +, -, !x++
Ternary? :x > 5 ? "yes" : "no"

Increment & Decrement

int x = 5;
x++;     // Post-increment: use then increment → x = 6
++x;     // Pre-increment: increment then use → x = 7

int a = 10;
int b = a++;  // b = 10, then a = 11
int c = ++a;  // a = 12, then c = 12
✏️ Practice: Write a program to swap two numbers without using a third variable using arithmetic operators.

🎉 Congratulations!

You've completed the Java Programming course! Keep practicing and building projects. Best of luck! ☕🌟