☕ What is Java?
Java is a high-level, object-oriented, platform-independent programming language.
It follows the “Write Once, Run Anywhere” principle using the Java Virtual Machine (JVM).
⚙️ Java Architecture
Source Code (.java)
↓
Java Compiler (javac)
↓
Bytecode (.class)
↓
Java Virtual Machine (JVM)
↓
Machine Code (Platform Specific)
Key Components:
● JDK (Java Development Kit) – Tools for development (compiler, debugger, JRE).
● JRE (Java Runtime Environment) – Runs Java programs.
● JVM (Java Virtual Machine) – Executes bytecode.
🧱 Basic Structure of a Java Program
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
Explanation:
● class → Blueprint for objects.
● public static void main(String[] args) → Entry point of the program.
● [Link]() → Prints output to the console.
🧮 Data Types
🔹 Primitive Types
Type Size Example
byte 1 byte byte b = 10;
short 2 short s = 20;
bytes
int 4 int x = 100;
bytes
long 8 long l = 1000L;
bytes
float 4 float f = 10.5f;
bytes
double 8 double d = 99.99;
bytes
char 2 char c = 'A';
bytes
boolea 1 bit boolean flag = true;
n
🔢 Variables and Constants
int age = 25; // variable
final double PI = 3.14159; // constant
🔄 Operators
Type Examples
Arithmetic +, -, *, /, %
Relational ==, !=, >, <, >=, <=
Logical &&, ||, !
Assignment =, +=, -=, *=, /=
Increment/Decremen ++, --
t
🔁 Control Statements
if-else
if(age >= 18)
[Link]("Adult");
else
[Link]("Minor");
switch
switch(day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid");
}
Loops
for(int i=1; i<=5; i++)
[Link](i);
int j = 1;
while(j <= 5) {
[Link](j);
j++;
}
int k = 1;
do {
[Link](k);
k++;
} while(k <= 5);
🧰 Arrays
int[] numbers = {1, 2, 3, 4, 5};
[Link](numbers[2]); // Output: 3
🧩 OOP Concepts
1. Class – Blueprint for creating objects.
2. Object – Instance of a class.
3. Encapsulation – Hiding data using private fields and public getters/setters.
4. Inheritance – Reusing code from another class using extends.
5. Polymorphism – Method Overloading & Overriding.
6. Abstraction – Hiding implementation using abstract classes or interfaces.
🧮 Example: Class and Object
class Car {
String brand;
void start() {
[Link](brand + " is starting...");
}
}
public class Main {
public static void main(String[] args) {
Car c1 = new Car();
[Link] = "Tesla";
[Link](); // Output: Tesla is starting...
}
}
⚙️ Input/Output Example
import [Link];
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
}
}
📦 Packages and Imports
package [Link]; // defines package
import [Link].*; // imports utility classes
🧾 Exception Handling
try {
int result = 10 / 0;