BASIC JAVA
☕ Java Core & Advanced Programming Notes
1️⃣ Java Architecture & First Program
Definition: Java ek high-level, robust, strongly typed aur pure Object-Oriented Programming (OOP) language hai jise Sun Microsystems (ab Oracle) ne banaya tha.
Java source code (
.java) compile hokar direct machine code nahi banta, balki intermediate Bytecode (.class) banta hai. Yeh Bytecode kisi bhi OS par chal sakta hai jahan JVM (Java Virtual Machine) installed ho.
First Java Program Structure
public class HelloWorld {
// Execution starting point
public static void main(String[] args) {
System.out.println("Hello, Java Enterprise World!");
}
}
public class HelloWorld: File ka naam aur class ka naam exact match hona mandatory hai (HelloWorld.java).static: JVM is method ko bina class ka object create kiye direct execute kar sakti hai.void: Yeh method execution complete hone par kuch return nahi karta.String[] args: Command-line arguments pass karne ke liye array.
2️⃣ Variables, Primitive Data Types & Operators
Java ek strongly typed language hai; har variable ko use karne se pehle uska type explicitly declare karna padta hai.
| Data Type | Memory Size | Default Value | Example |
|---|---|---|---|
int |
4 Bytes (32-bit) | 0 |
int age = 20; |
double |
8 Bytes (64-bit) | 0.0d |
double pi = 3.14159; |
char |
2 Bytes (Unicode) | '\u0000' |
char grade = 'A'; |
boolean |
1 Bit (logical) | false |
boolean isEnrolled = true; |
String (Non-Primitive) |
Object reference | null |
String name = "Rahul"; |
Operators Overview
| Category | Operators | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, % |
Mathematical calculations (Remainder calculation with %). |
| Assignment | =, +=, -=, *= |
Value calculate karke variable me assign karna. |
| Relational | ==, !=, >, <, >= |
Values compare karna (Returns boolean true/false). |
| Logical | && (AND), || (OR), ! (NOT) |
Multiple conditions ko combine karna. |
3️⃣ Control Statements & Methods
Code execution flow ko control karne ke liye branching aur loops ka upyog hota hai.
// 1. Conditionals (if-else)
int score = 85;
if (score >= 90) {
System.out.println("Grade: A+");
} else if (score >= 75) {
System.out.println("Grade: A");
} else {
System.out.println("Grade: B");
}
// 2. For Loop
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
// 3. Reusable Static Method
public static int multiply(int a, int b) {
return a * b;
}
4️⃣ The 4 Pillars of OOPs & Interfaces
Object-Oriented Programming real-world entities ko code structure me map karta hai:
| Pillar | Concept | Code Keyword |
|---|---|---|
| Encapsulation | Data (fields) aur methods ko single unit me band karna aur direct access hide karna. | private variables with getters/setters |
| Inheritance | Parent class ki properties aur methods ko child class me reuse karna. | extends |
| Polymorphism | Ek hi method ka multiple forms me kaam karna (Overloading & Overriding). | @Override, Method Overloading |
| Abstraction | Internal implementation hide karke sirf essential features show karna. | abstract class, interface |
// Interface (Pure Contract)
interface Animal {
void makeSound(); // abstract method
}
// Inheritance & Implementation
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Bark! Bark!");
}
}
5️⃣ Exception Handling Architecture
Runtime errors se application crash hone se bachane ke liye robust try-catch-finally blocks use hote hain:
try {
int result = 100 / 0; // Throws ArithmeticException
} catch (ArithmeticException e) {
System.err.println("Error: Zero se division allowed nahi hai! " + e.getMessage());
} catch (Exception e) {
System.err.println("Generic Exception caught: " + e.getMessage());
} finally {
// Har haal me execute hota hai (resource cleanup ke liye)
System.out.println("Database/File connections closed safely.");
}
6️⃣ Java Collections Framework
Dynamic objects ke groups ko store aur manipulate karne ka centralized framework:
| Collection Interface | Concrete Class | Ordering & Duplicate Behavior |
|---|---|---|
| List | ArrayList, LinkedList |
Ordered index, Duplicates allowed hote hain. |
| Set | HashSet, TreeSet |
Unordered, No duplicate elements. |
| Map | HashMap, TreeMap |
Key-Value pairs (Unique keys, Duplicate values). |
// List Example
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Spring Boot");
// Map Example (Key-Value)
Map<Integer, String> userMap = new HashMap<>();
userMap.put(101, "Rahul Sharma");
userMap.put(102, "Anita Verma");
System.out.println("User 101: " + userMap.get(101));
7️⃣ File I/O, Multithreading & JDBC Database Connectivity
1. Multithreading (Concurrent Execution)
class WorkerThread extends Thread {
public void run() {
System.out.println("Thread ID: " + Thread.currentThread().getId() + " is processing task.");
}
}
// Execution
WorkerThread t1 = new WorkerThread();
t1.start(); // Spawns a new independent thread
2. JDBC (Java Database Connectivity) Pipeline
import java.sql.*;
public class JdbcDemo {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/company_db";
String user = "root";
String pass = "secret";
// Try-with-resources automatically closes connections
try (Connection con = DriverManager.getConnection(url, user, pass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM employees")) {
while (rs.next()) {
System.out.println(rs.getInt("id") + " - " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}