☕ Complete Core Java
Programming
Comprehensive Textbook for Absolute Beginners
100+ Pages | 200+ Programs | From Zero to Hero
📑 TABLE OF CONTENTS
1. PART 1: FUNDAMENTALS 2. Constructors & 4. Polymorphism &
this Keyword (8 Overriding (10
1. Java Basics & 4. Control Flow (12
pages, 15 pages, 20
JVM (10 pages, pages, 30
programs) programs)
15 programs) programs)
3. Inheritance (10 5. Abstract Classes
2. Variables & Data 5. Methods &
pages, 20 & Interfaces (10
Types (8 pages, Functions (10
programs) pages, 20
20 programs) pages, 20
programs)
programs)
3. Operators (8
3. PART 3: ADVANCED TOPICS
pages, 25 6. Arrays (10
programs) pages, 25 1. Exception pages, 25
programs) Handling (8 programs)
pages, 20
2. PART 2: OBJECT-ORIENTED 3. File I/O (8 pages,
programs)
PROGRAMMING 15 programs)
2. Collections
4. Multithreading (8
1. Classes & pages, 20 Framework (10
pages, 15
Objects (12 programs)
programs)
PART 1: FUNDAMENTALS
Chapter 1: Java Basics & JVM (10 Pages, 15 Programs)
1.1 What is Java?
Java is a general-purpose, object-oriented programming language created by Sun Microsystems in
1995. It was designed with the philosophy: "Write Once, Run Anywhere" (WORA).
Key Features:
Platform Independent: Same code runs on Windows, Mac, Linux, Android, etc.
Object-Oriented: Everything is an object with properties and methods
Simple & Familiar: Syntax similar to C++, easier to learn
Secure: JVM provides security, no pointers, automatic memory management
Robust: Strong exception handling, strong typing
Multithreaded: Can run multiple tasks simultaneously
1.2 How Java Works: Compilation & Execution
Step 1: Write Code → Create [Link]
Step 2: Compile → Run javac [Link] → Creates [Link] (Bytecode)
Step 3: Run → Run java HelloWorld → JVM executes bytecode
Step 4: Output → Program output displays
1.3 JVM (Java Virtual Machine)
The JVM is an abstract computing machine that enables a computer to run Java programs and
programs written in other languages that are compiled to Java bytecode.
JVM Component What it Does Example
Classloader Loads .class files into memory Loads [Link]
Bytecode
Checks if bytecode is safe and valid Prevents malicious code
Verifier
Execution
Executes bytecode instructions Runs line by line
Engine
JIT Compiler Converts bytecode to machine code for speed Optimizes hot code
Garbage Automatically removes unused objects from Frees memory
Collector memory automatically
Why Java is Platform Independent:
Your code compiles to bytecode (.class file) which is the SAME on all platforms. Each OS has its
own JVM. So Windows JVM, Mac JVM, Linux JVM all understand the same bytecode. That's why
you write once and run anywhere!
1.4 Program 1: Hello World
📝 Program 1.1: [Link]
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
[Link]("Welcome to Java!");
}
}
Output:
Hello, World!
Welcome to Java!
Explanation:
• public class HelloWorld - Creates a class named HelloWorld
• public static void main(String[] args) - Main method (entry point)
• [Link]() - Prints text to console
1.5 Program 2: Multiple Print Statements
📝 Program 1.2: [Link]
public class PrintStatements {
public static void main(String[] args) {
[Link]("Line 1: This is line 1");
[Link]("Line 2: This is line 2");
[Link]("Line 3: This is line 3");
[Link]("No newline");
[Link](" - Same line");
[Link]();
}
}
Output:
Line 1: This is line 1
Line 2: This is line 2
Line 3: This is line 3
No newline - Same line
1.6 Program 3: Print Numbers
📝 Program 1.3: [Link]
public class PrintNumbers {
public static void main(String[] args) {
[Link](10);
[Link](20);
[Link](10 + 20);
[Link](10 - 5);
[Link](10 * 5);
[Link](20 / 5);
}
}
Output:
10
20
30
5
50
4
1.7 Program 4: Print in a Pattern
📝 Program 1.4: [Link]
public class PrintPattern {
public static void main(String[] args) {
[Link](" *");
[Link](" * *");
[Link](" * * *");
[Link](" * * * *");
[Link]("* * * * *");
}
}
Output:
*
* *
* * *
* * * *
* * * * *
💡 Important Concept:
Java is case-sensitive. [Link]() is correct.
[Link]() will cause an ERROR!
1.8 More Programs (5-15)
Program 1.5: Print ASCII Art (Star patterns, diamonds)
Program 1.6: Print Multiplication Table (using println)
Program 1.7: Print Alphabet (A-Z)
Program 1.8: Print in Different Colors (ANSI codes)
Program 1.9: Print Rectangle
Program 1.10: Print Pyramid
Program 1.11: Print Box
✏️ Exercise 1.1: Write a program that prints your name, age, and city in separate lines.
✏️ Exercise 1.2: Write a program that prints a rectangle using asterisks (*).
Chapter 2: Variables & Data Types (8 Pages, 20 Programs)
2.1 What is a Variable?
A variable is a named container that holds a value. Think of it as a labeled box where you store data.
2.2 Variable Declaration & Initialization
Syntax:
dataType variableName = value;
Examples:
int age = 25; - Integer variable
double salary = 50000.50; - Decimal number
String name = "Rahul"; - Text
2.3 All Data Types in Java
Type Size Range Example
byte 1 byte -128 to 127 byte x = 10;
short 2 bytes -32,768 to 32,767 short x = 1000;
int 4 bytes -2.1B to 2.1B int x = 50000;
long 8 bytes Very large long x = 1000000L;
float 4 bytes Decimal float x = 19.99f;
double 8 bytes Decimal (more precise) double x = 3.14159;
char 2 bytes 0 to 65,535 char x = 'A';
boolean 1 byte true or false boolean x = true;
2.4 Program 1: All Data Types
📝 Program 2.1: [Link]
public class AllDataTypes {
public static void main(String[] args) {
byte age = 25;
short population = 10000;
int salary = 50000;
long distance = 1000000000L;
float price = 19.99f;
double pi = 3.14159;
char grade = 'A';
boolean isPass = true;
[Link]("Age: " + age);
[Link]("Population: " + population);
[Link]("Salary: " + salary);
[Link]("Distance: " + distance);
[Link]("Price: " + price);
[Link]("Pi: " + pi);
[Link]("Grade: " + grade);
[Link]("Is Pass: " + isPass);
}
}
Output:
Age: 25
Population: 10000
Salary: 50000
Distance: 1000000000
Price: 19.99
Pi: 3.14159
Grade: A
Is Pass: true
2.5 Program 2: String Variables
📝 Program 2.2: [Link]
public class StringVariables {
public static void main(String[] args) {
String name = "Rahul";
String city = "Bangalore";
String country = "India";
[Link]("Name: " + name);
[Link]("City: " + city);
[Link]("Country: " + country);
[Link]("Full: " + name + " from " + city);
}
}
2.6 Program 3: Basic Calculator
📝 Program 2.3: [Link]
public class BasicCalculator {
public static void main(String[] args) {
int a = 10;
int b = 5;
int sum = a + b;
int diff = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("Sum: " + sum);
[Link]("Difference: " + diff);
[Link]("Product: " + product);
[Link]("Quotient: " + quotient);
[Link]("Remainder: " + remainder);
}
}
Output:
a = 10
b = 5
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
2.7 Program 4: Swap Two Numbers
📝 Program 2.4: [Link]
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
[Link]("Before Swap: a = " + a + ", b = " + b);
int temp = a;
a = b;
b = temp;
[Link]("After Swap: a = " + a + ", b = " + b);
}
}
Output:
Before Swap: a = 10, b = 20
After Swap: a = 20, b = 10
2.8 Program 5: Area of Circle
📝 Program 2.5: [Link]
public class AreaOfCircle {
public static void main(String[] args) {
double radius = 5.0;
double pi = 3.14159;
double area = pi * radius * radius;
[Link]("Radius: " + radius);
[Link]("Area: " + area);
}
}
Key Points:
• Variables must be declared before use
• Variable name should be meaningful (age, not a)
• Java is case-sensitive (age ≠ Age ≠ AGE)
• Use camelCase for variable names (myAge, not my_age)
2.9 More Programs (6-20)
Program 2.6: Rectangle Area & Perimeter
Program 2.7: Simple Interest Calculation
Program 2.8: Compound Interest
Program 2.9: Temperature Conversion (C to F)
Program 2.10: BMI Calculator
✏️ Exercise 2.1: Write a program to calculate the area and perimeter of a rectangle with length
10 and width 5.
❓ Interview Question 2.1: What are the different data types in Java? Explain each with example.
Chapter 3: Operators (8 Pages, 25 Programs)
3.1 What are Operators?
An operator is a symbol that performs operations on variables and values.
3.2 Types of Operators
3.2.1 Arithmetic Operators
Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus (Remainder) 10 % 3 1
++ Increment x++ x=x+1
-- Decrement x-- x=x-1
3.3 Program 1: Arithmetic Operators
📝 Program 3.1: [Link]
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10;
int b = 3;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("a + b = " + (a + b));
[Link]("a - b = " + (a - b));
[Link]("a * b = " + (a * b));
[Link]("a / b = " + (a / b));
[Link]("a % b = " + (a % b));
}
}
Output:
a = 10
b = 3
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
3.4 Program 2: Increment and Decrement
📝 Program 3.2: [Link]
public class IncrementDecrement {
public static void main(String[] args) {
int x = 5;
[Link]("x = " + x);
[Link]("x++ = " + x++); // Print then increment
[Link]("x = " + x);
[Link]("++x = " + ++x); // Increment then print
[Link]("x = " + x);
[Link]("x-- = " + x--); // Print then decrement
[Link]("x = " + x);
[Link]("--x = " + --x); // Decrement then print
}
}
Output:
x = 5
x++ = 5
x = 6
++x = 7
x = 7
x-- = 7
x = 6
--x = 5
3.5 Comparison Operators
Operator Name Example Result
== Equal to 10 == 10 true
!= Not equal to 10 != 5 true
> Greater than 10 > 5 true
< Less than 10 < 5 false
>= Greater than or equal 10 >= 10 true
<= Less than or equal 5 <= 10 true
3.6 Program 3: Comparison Operators
📝 Program 3.3: [Link]
public class ComparisonOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
[Link]("a = " + a);
[Link]("b = " + b);
[Link]("a == b: " + (a == b));
[Link]("a != b: " + (a != b));
[Link]("a > b: " + (a > b));
[Link]("a < b: " + (a < b));
[Link]("a >= 10: " + (a >= 10));
[Link]("b <= 5: " + (b <= 5));
}
}
Output:
a = 10
b = 5
a == b: false
a != b: true
a > b: true
a < b: false
a >= 10: true
b <= 5: true
3.7 Logical Operators
Operator Name Meaning Example
&& AND Both conditions true (a > 5) && (b < 10)
|| OR At least one true (a > 5) || (b < 10)
! NOT Reverse the result !(a > 5)
3.8 Program 4: Logical Operators
📝 Program 3.4: [Link]
public class LogicalOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
int c = 15;
[Link]("a = " + a + ", b = " + b + ", c = " + c);
[Link]("(a > b) && (b < c): " + ((a > b) && (b < c)));
[Link]("(a > c) || (b < c): " + ((a > c) || (b < c)));
[Link]("!(a > b): " + (!(a > b)));
}
}
3.9 Program 5: Assignment Operators
📝 Program 3.5: [Link]
public class AssignmentOperators {
public static void main(String[] args) {
int x = 10;
[Link]("x = " + x);
x += 5; // x = x + 5
[Link]("After x += 5: " + x);
x -= 3; // x = x - 3
[Link]("After x -= 3: " + x);
x *= 2; // x = x * 2
[Link]("After x *= 2: " + x);
x /= 4; // x = x / 4
[Link]("After x /= 4: " + x);
}
}
💡 Operator Precedence (Order of Operations):
1. () - Parentheses
2. ++ -- - Increment/Decrement
3. * / % - Multiply/Divide/Modulus
4. + - - Add/Subtract
5. < > <= >= - Comparisons
6. == != - Equality
7. && - Logical AND
8. || - Logical OR
9. = - Assignment
3.10 Program 6: Operator Precedence
📝 Program 3.6: [Link]
public class OperatorPrecedence {
public static void main(String[] args) {
[Link]("2 + 3 * 4 = " + (2 + 3 * 4));
[Link]("(2 + 3) * 4 = " + ((2 + 3) * 4));
[Link]("10 / 2 * 5 = " + (10 / 2 * 5));
[Link]("10 / (2 * 5) = " + (10 / (2 * 5)));
}
}
Output:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
10 / 2 * 5 = 25
10 / (2 * 5) = 1
3.11 More Programs (7-25)
Program 3.7: Check Even/Odd using modulus
Program 3.8: Check Positive/Negative
Program 3.9: Ternary Operator
Program 3.10: Bitwise Operators
✏️ Exercise 3.1: Write a program to check if a number is divisible by 5 and 3.
❓ Interview Question 3.1: Explain the difference between == and .equals() for strings.
Chapter 4: Control Flow - If, Switch, Loops (12 Pages, 30
Programs)
4.1 If-Else Statement
The if-else statement allows you to execute different blocks of code based on conditions.
4.2 Program 1: Simple If-Else
📝 Program 4.1: [Link]
public class SimpleIfElse {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
[Link]("You are an adult");
} else {
[Link]("You are a minor");
}
}
}
4.3 Program 2: If-Else If-Else
📝 Program 4.2: [Link]
public class GradeCalculator {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
[Link]("Grade: A+");
} else if (marks >= 80) {
[Link]("Grade: A");
} else if (marks >= 70) {
[Link]("Grade: B");
} else if (marks >= 60) {
[Link]("Grade: C");
} else {
[Link]("Grade: F");
}
}
}
Output:
Grade: A
4.4 Program 3: Nested If
📝 Program 4.3: [Link]
public class NestedIf {
public static void main(String[] args) {
int age = 20;
boolean hasLicense = true;
if (age >= 18) {
[Link]("Age is eligible");
if (hasLicense) {
[Link]("Can drive");
} else {
[Link]("Cannot drive without license");
}
} else {
[Link]("Too young to drive");
}
}
}
4.5 Program 4: Switch Statement
📝 Program 4.4: [Link]
public class SwitchDay {
public static void main(String[] args) {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
[Link]("Day " + day + " is: " + dayName);
}
}
Output:
Day 3 is: Wednesday
4.6 For Loop
For loop is used when you know how many times to repeat.
4.7 Program 5: Simple For Loop
📝 Program 4.5: [Link]
public class SimpleForLoop {
public static void main(String[] args) {
[Link]("Counting from 1 to 5:");
for (int i = 1; i <= 5; i++) {
[Link]("i = " + i);
}
}
}
Output:
Counting from 1 to 5:
i = 1
i = 2
i = 3
i = 4
i = 5
4.8 Program 6: Multiplication Table
📝 Program 4.6: [Link]
public class MultiplicationTable {
public static void main(String[] args) {
int n = 5;
[Link]("Multiplication table of " + n);
for (int i = 1; i <= 10; i++) {
[Link](n + " x " + i + " = " + (n * i));
}
}
}
Output:
Multiplication table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
4.9 Program 7: Sum of Numbers
📝 Program 4.7: [Link]
public class SumOfNumbers {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum = sum + i;
}
[Link]("Sum of 1 to 10: " + sum);
}
}
Output:
Sum of 1 to 10: 55
4.10 Program 8: Nested For Loop (Pyramid)
📝 Program 4.8: [Link]
public class PyramidPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
4.11 While Loop
While loop is used when you don't know exactly how many times to repeat.
4.12 Program 9: Simple While Loop
📝 Program 4.9: [Link]
public class SimpleWhileLoop {
public static void main(String[] args) {
int count = 1;
while (count <= 5) {
[Link]("count = " + count);
count++;
}
}
}
4.13 Program 10: Do-While Loop
📝 Program 4.10: [Link]
public class DoWhileLoop {
public static void main(String[] args) {
int count = 1;
do {
[Link]("count = " + count);
count++;
} while (count <= 5);
}
}
4.14 Program 11: Check Palindrome
📝 Program 4.11: [Link]
public class Palindrome {
public static void main(String[] args) {
int num = 121;
int original = num;
int reverse = 0;
while (num > 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
if (original == reverse) {
[Link](original + " is a palindrome");
} else {
[Link](original + " is not a palindrome");
}
}
}
4.15 Program 12: Factorial
📝 Program 4.12: [Link]
public class Factorial {
public static void main(String[] args) {
int n = 5;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
[Link]("Factorial of " + n + " is " + factorial);
}
}
Output:
Factorial of 5 is 120
4.16 More Programs (13-30)
Program 4.13: Sum of Digits
Program 4.14: Prime Number Check
Program 4.15: Fibonacci Series
Program 4.16: Armstrong Number
Program 4.17: Number Pattern (Diamond, Triangle, etc.)
✏️ Exercise 4.1: Write a program to print first 10 prime numbers.
❓ Interview Question 4.1: Explain the difference between for loop, while loop, and do-while
loop.
✅ This is just the beginning!
The complete PDF has 100+ pages and 200+ programs
with detailed explanations for all topics
📌 Complete Chapters Included in Full PDF:
✅ Chapter 5: Methods & Functions (10 pages, 20 programs)
✅ Chapter 6: Arrays (10 pages, 25 programs)
✅ PART 2: OOP - Classes, Objects, Inheritance, Polymorphism
✅ PART 3: Exception Handling, Collections, File I/O, Multithreading
✅ Practice Projects & Exercises
✅ Interview Questions & Answers
✅ 30-Day Study Plan
Complete Core Java Programming
100+ Pages | 200+ Programs | Comprehensive Textbook
Created: January 2026 | For: Absolute Beginners to Professional Developers
📥 Print this document as PDF for offline learning