Java Important Topics - Intermediate & Advanced
This document covers the most important intermediate and advanced Java topics with concise
explanations
and example snippets. It's designed to help students build a strong understanding of Java for
exams,
projects, and real-world development.
1. Introduction to Java
Java is a high-level, object-oriented, platform-independent programming language developed by
Sun Microsystems.
It follows the principle of "Write Once, Run Anywhere" using the JVM (Java Virtual Machine).
Key Features:
- Object-Oriented
- Platform Independent
- Robust and Secure
- Multithreaded
- Rich Standard Library
2. OOP Concepts
OOP (Object-Oriented Programming) organizes code around objects and data.
- Class & Object: Class is a blueprint; object is an instance.
- Inheritance: Acquire properties of another class.
- Polymorphism: Many forms - method overloading & overriding.
- Encapsulation: Bundling data with methods.
- Abstraction: Hiding internal details.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
3. Core Java Concepts
- Constructors: Special methods to initialize objects.
- 'this' and 'super': 'this' refers to current object; 'super' refers to parent class.
- Static & Final: 'static' for class-level members; 'final' for constants or preventing inheritance.
- Packages: Organize classes into namespaces.
- Interfaces: Abstract types for multiple inheritance.
- Inner Classes: Classes inside classes.
Example:
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() { System.out.println("Drawing Circle"); }
}
4. Exception Handling
Exceptions handle runtime errors gracefully.
Syntax:
try {
int a = 10/0;
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Always executed");
}
- throw: Used to throw an exception.
- throws: Declares exceptions.
- Custom Exceptions: Extend Exception class.
5. Java Collections Framework
Collections store and manipulate groups of objects efficiently.
Interfaces:
- List (ArrayList, LinkedList)
- Set (HashSet, TreeSet)
- Map (HashMap, TreeMap)
Example:
import java.util.*;
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
for(String s : list) {
System.out.println(s);
}
6. Multithreading
Multithreading allows concurrent execution of tasks.
Creating a Thread:
class MyThread extends Thread {
public void run() {
System.out.println("Running...");
}
}
MyThread t = new MyThread();
t.start();
- Synchronization ensures thread-safe access.
- Inter-thread communication: wait(), notify(), notifyAll().
7. File Handling & I/O Streams
I/O streams handle reading and writing of data.
Example:
import java.io.*;
class FileExample {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("output.txt");
fw.write("Hello Java");
fw.close();
}
}
8. JDBC - Database Connectivity
JDBC (Java Database Connectivity) connects Java apps to databases.
Steps:
1. Load Driver
2. Establish Connection
3. Create Statement
4. Execute Query
5. Close Connection
Example:
import java.sql.*;
class DBExample {
public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root",
"pass");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM students");
while(rs.next()) {
System.out.println(rs.getString(1));
}
con.close();
}
}