Java – Notes
1. Introduction
Java is a high-level, object-oriented programming language.
Developed by Sun Microsystems (now Oracle) in 1995.
Platform independent → “Write Once, Run Anywhere” (WORA).
Runs on Java Virtual Machine (JVM).
2. Features of Java
Simple – Easy syntax similar to C/C++.
Object-Oriented – Supports classes, objects, inheritance, polymorphism.
Platform Independent – Code runs on any OS with JVM.
Secure – Bytecode runs in JVM sandbox.
Robust – Strong memory management, exception handling.
Multithreaded – Can execute multiple threads simultaneously.
Dynamic – Supports dynamic memory allocation, dynamic linking.
3. Java Architecture
Java Source Code (.java) → Written by developer
Compiler (javac) → Converts
.javato Bytecode (.class)JVM → Executes bytecode on any platform
Flow:
Source Code (.java) → Compiler → Bytecode (.class) → JVM → Run
4. Java Syntax
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
class– Blueprint of objectmain()– Entry point of programSystem.out.println()– Prints output
5. Data Types
Primitive Data Types
| Type | Size | Example |
|———–|————|———|
| byte | 1 byte | 10 |
| short | 2 bytes | 100 |
| int | 4 bytes | 1000 |
| long | 8 bytes | 10000L |
| float | 4 bytes | 10.5f |
| double | 8 bytes | 20.5 |
| char | 2 bytes | ‘A’ |
| boolean | 1 bit | true |Non-Primitive Data Types
Strings, Arrays, Classes, Interfaces, Objects
6. Variables
Declaration:
int age;Initialization:
age = 25;Types:
Local Variable → Inside method
Instance Variable → Inside class but outside method
Static/Global Variable → Declared with
statickeyword
7. Operators
Arithmetic:
+ - * / %Relational:
== != > < >= <=Logical:
&& || !Assignment:
= += -= *= /= %=Increment/Decrement:
++ --
8. Control Statements
1. Decision Making:
if(condition) { }
else if(condition) { }
else { }
switch(variable) { case: break; default: }
2. Loops:
for(initialization; condition; increment) { }
while(condition) { }
do { } while(condition);
3. Jump Statements:
break→ Exit loopcontinue→ Skip current iterationreturn→ Exit method
9. Classes & Objects
Class – Template for objects
class Car {
String color;
int speed;void drive() {
System.out.println(“Car is driving”);
}
}
Object – Instance of class
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
10. Methods
Function inside class.
returnType methodName(parameters) {
// code
}
Example:
int add(int a, int b) {
return a + b;
}
11. Constructors
Special method to initialize objects
class Car {
String color;
Car(String c) { color = c; }
}
Car myCar = new Car("Red");
12. Inheritance
Mechanism to inherit properties and methods from another class.
Syntax:
class Child extends Parent { }Types: Single, Multilevel, Hierarchical, Multiple (via interfaces)
13. Polymorphism
Ability of object to take multiple forms
Compile-time (Method Overloading) – Same method name, different parameters
Runtime (Method Overriding) – Subclass provides its own version of method
14. Encapsulation
Wrapping data and methods together
Use private variables + public getters/setters
class Student {
private int age;
public void setAge(int a) { age = a; }
public int getAge() { return age; }
}
15. Abstraction
Hiding internal details, showing only functionality
Abstract Class
abstract class Animal {
abstract void sound();
}
Interface
interface Animal {
void sound();
}
16. Exception Handling
Handling runtime errors
try {
int a = 5/0;
} catch(ArithmeticException e) {
System.out.println("Error: " + e);
} finally {
System.out.println("Always executed");
}
17. Packages
Group of related classes/interfaces
Example:
import java.util.Scanner;
18. Java Collections
ArrayList, HashMap, HashSet, LinkedList, etc.
Used for storing, managing, and manipulating objects
19. Threads (Multithreading)
Thread – Lightweight process
Creating Thread
class MyThread extends Thread {
public void run() { System.out.println("Thread running"); }
}
Runnable Interface
class MyThread implements Runnable { public void run(){} }
20. File I/O
Reading & Writing files
import java.io.*;
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
br.close();
21. Important Notes
Java is Case Sensitive
Java main method signature must be:
public static void main(String[] args)
JVM → JRE → JDK hierarchy:
JDK (Java Development Kit) → tools to write & compile code
JRE (Java Runtime Environment) → JVM + libraries
JVM (Java Virtual Machine) → executes bytecode
