@codewithtanvir_ @OfficialCodewithTanvir
Comprehensive Java Notes for Interview and Exam Preparation
1. Introduction
Brief History of Java
Java was developed by Sun Microsystems and released in 1995.
Designed with the principle: "Write Once, Run Anywhere" (WORA).
Java is an object-oriented, platform-independent, and secure
programming language.
Importance and Usage
Widely used in enterprise-level applications, web servers, Android
app development, embedded devices, and big data technologies.
Popular for its robustness, security features, and cross-platform
capabilities.
2. Beginner Topics
Java Basics: Syntax, Keywords, Variables, Data Types
Syntax: Case-sensitive; statements end with a semicolon ;.
Keywords: Reserved words like class, public, static, int, if, else.
Variables:
o Declaration: int num;
o Initialization: int num = 5;
Data Types:
o Primitive: int, byte, short, long, float, double, char, boolean
o Non-primitive: Classes, Interfaces, Arrays
Input/Output
java
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
int num = sc.nextInt();
System.out.println("You entered " + num);
Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Conditional Statements
java
if (num > 0) {
System.out.println("Positive");
} else if (num == 0) {
System.out.println("Zero");
} else {
System.out.println("Negative");
Loops
For loop
java
for (int i = 0; i < 5; i++) {
System.out.println(i);
While loop
java
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
Do-while loop
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
java
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
3. Intermediate Topics
Functions and Methods
Define methods to perform tasks:
java
public int sum(int a, int b) {
return a + b;
Calling methods:
java
int result = sum(5, 10);
Arrays and Strings
Array Declaration and Initialization
java
int[] arr = new int[5];
int[] nums = {1, 2, 3, 4, 5};
Manipulating Strings
java
String str = "Hello";
int length = str.length();
String substr = str.substring(1, 3);
Object-Oriented Programming (OOP)
Class and Object
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
java
class Car {
String model;
int year;
void display() {
System.out.println(model + " - " + year);
Car myCar = new Car();
myCar.model = "Honda";
myCar.year = 2022;
myCar.display();
Inheritance
java
class Vehicle {
void run() {
System.out.println("Vehicle is running");
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running");
Polymorphism (Method Overloading & Overriding)
java
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
// Overloading
void display(int a) { }
void display(String s) { }
// Overriding
class Parent {
void show() { System.out.println("Parent"); }
class Child extends Parent {
void show() { System.out.println("Child"); }
Encapsulation
java
class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
Abstraction
java
abstract class Animal {
abstract void sound();
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
Exception Handling
Using try-catch
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
java
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
throw and throws
java
void checkAge(int age) throws Exception {
if (age < 18) throw new Exception("Age must be 18 or above");
Custom Exception
java
class MyException extends Exception {
MyException(String msg) {
super(msg);
File Handling
Reading and writing files
java
import java.io.*;
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
bw.write("Hello World");
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
bw.close();
Serialization
java
import java.io.Serializable;
class Student implements Serializable {
int id;
String name;
4. Advanced Topics
Advanced Data Structures
Linked List, Stack, Queue using Java Collections Framework (JCF):
java
import java.util.*;
LinkedList<Integer> ll = new LinkedList<>();
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new LinkedList<>();
HashMap and Set
java
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
Set<String> set = new HashSet<>();
Multithreading & Concurrency
Creating Threads
java
class MyThread extends Thread {
public void run() {
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
System.out.println("Thread running");
MyThread t = new MyThread();
t.start();
Synchronization
java
synchronized(this) {
// synchronized block
Executors Framework
java
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> System.out.println("Task executed"));
executor.shutdown();
Memory Management & Garbage Collection
Automatic memory management by JVM.
Objects no longer referenced are eligible for garbage collection.
finalize() method (deprecated).
JVM provides tools like VisualVM to monitor memory.
Frameworks / Libraries
Spring Boot - Rapid application development framework.
Hibernate - ORM (Object-Relational Mapping) tool.
Maven/Gradle - Build tools for dependencies and project
management.
Design Patterns
Singleton, Factory, Observer, Decorator, MVC are common in Java
projects.
Helps in writing reusable, maintainable code.
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
5. Practical Examples
Example: Class and Object
java
class Student {
String name;
int rollNo;
void display() {
System.out.println(name + " - " + rollNo);
Student s1 = new Student();
s1.name = "Alice";
s1.rollNo = 101;
s1.display();
Example: Exception Handling
java
public class Test {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: Divide by zero");
Mini Project Ideas
Beginner: Calculator console app, To-do list CLI.
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
Intermediate: Student management system with file storage.
Advanced: REST API for book inventory using Spring Boot.
6. Interview Preparation
Top 20 Frequently Asked Questions
1. What are the main features of Java?
2. Difference between JDK, JRE, and JVM.
3. Explain OOP concepts with examples.
4. What is method overloading vs overriding?
5. Explain Java Collections Framework.
6. How does HashMap work internally?
7. What is the difference between == and .equals()?
8. Describe exception handling mechanism.
9. What are checked and unchecked exceptions?
10. How do you create a thread in Java?
11. What is synchronization and why is it important?
12. Explain garbage collection in Java.
13. What is a Lambda expression in Java 8?
14. What are functional interfaces?
15. Differences between ArrayList and LinkedList.
16. How to handle file input/output in Java?
17. What are design patterns? Name a few.
18. What is JDBC?
19. How does Spring Boot simplify Java development?
20. Explain MVC architecture.
Common Mistakes & Pitfalls
Forgetting to close resources (files, connections).
Mixing == and .equals() for object comparison.
Not handling exceptions properly.
@Copyright content – https:// codewithtanvir.infinityfreeapp.com
@codewithtanvir_ @OfficialCodewithTanvir
Overusing synchronization causing performance bottlenecks.
Ignoring immutability principles.
7. Cheat Sheet / Quick Revision
Topic Key Points Syntax Example
Variable
type name = value; int x = 10;
Declaration
Conditional if, else if, else if (x > 0) {...}
Loop for, while, do-while for (int i=0; i<5; i++) {...}
Return type, Name,
Method int sum(int a, int b) { return a+b;}
Parameters, Body
Class & class Name {}, new
Car car = new Car();
Object keyword
class Sub extends
Inheritance class Dog extends Animal {}
Super
Exception try-catch, throw,
try { } catch (Exception e) {}
Handling throws
BufferedReader br = new
FileReader/Writer,
File I/O BufferedReader(new
BufferedReader
FileReader("f.txt"));
Extend Thread or class T extends Thread { public void
Thread
implement Runnable run() {...} }
list.forEach(n ->
Lambda (params) -> expression
System.out.println(n));
List<String> list = new
Collections List, Set, Map, Iterator
ArrayList<>();
Would you like me to provide detailed code examples for any specific topic
or help with sample interview questions?
@Copyright content – https:// codewithtanvir.infinityfreeapp.com