Advance Java
Advance Java
✅ 1. What is Java?
Java is a high-level, object-oriented, and platform-independent programming language
developed by Sun Microsystems (now Oracle).
🔥 Why Java is Popular?
Hello, Java!
✅ 4. What is Bytecode?
When you compile Java code using javac, it turns into bytecode (.class file).
Bytecode is not machine code — it runs inside the JVM.
// Default constructor
Student() {
rollNo = 0;
name = "Unknown";
}
// Parameterized constructor
Student(int r, String n) {
rollNo = r;
name = n;
}
// Display method
void display() {
System.out.println(rollNo + " " + name);
}
}
✅ 7. Arrays in Java
(Concept of arrays of primitives and objects – 1D and 2D)
void display() {
System.out.println(id + " " + name);
}
}
java
CopyEdit
public class Test {
public static void main(String[] args) {
Student[] arr = new Student[2];
arr[0] = new Student(1, "Riya");
arr[1] = new Student(2, "Arya");
ElectricCar inherits those properties + adds its own: battery, charge time.
java
CopyEdit
class Car {
int speed = 100;
void drive() {
System.out.println("Driving at " + speed + " km/h");
}
}
class ElectricCar extends Car {
int battery = 80;
void charge() {
System.out.println("Charging with " + battery + "% battery");
}
}
public class Test {
public static void main(String[] args) {
ElectricCar e = new ElectricCar();
e.drive(); // inherited from Car
e.charge(); // own method
}
}
✅ Multilevel Inheritance:
java
CopyEdit
class Animal {
void eat() { System.out.println("Eating"); }
}class Dog extends Animal {
void bark() { System.out.println("Barking"); }
}class Puppy extends Dog {
void weep() { System.out.println("Weeping"); }
}
🧠 Diagram:
nginx
CopyEdit
Animal
↓
Dog
↓
Puppy
✅ Hierarchical Inheritance:
java
CopyEdit
class Animal {
void sound() { System.out.println("Animal sound"); }
}class Dog extends Animal {}class Cat extends Animal {}
🧠 Diagram:
markdown
CopyEdit
Animal
/ \
Dog Cat
Reusability
This "internet disconnection" is like an exception in your program that must be handled to
avoid crashing or data loss.
Not consistent
Ordered data
Duplicates
🔹 A. ArrayList
Resizable array
📘 Code:
java
CopyEdit
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Riya");
list.add("Arora");
list.add("Riya"); // Allows duplicate
System.out.println(list); // [Riya, Arora, Riya]
}
}
📌 Key Points:
Access by index
Duplicates allowed
🔹 B. LinkedList
📘 Code:
java
CopyEdit
import java.util.LinkedList;
public class Example {
public static void main(String[] args) {
LinkedList<Integer> nums = new LinkedList<>();
nums.add(10);
nums.addFirst(5); // adds at beginning
nums.addLast(20); // adds at end
System.out.println(nums); // [5, 10, 20]
}
}
📌 Key Points:
🔹 C. Vector
Synchronized (thread-safe)
📘 Code:
java
CopyEdit
import java.util.Vector;
public class Example {
public static void main(String[] args) {
Vector<String> v = new Vector<>();
v.add("A");
v.add("B");
System.out.println(v); // [A, B]
}
}
📌 Key Points:
Iterable is the top-level interface → anything that can be looped using a for-each loop.
Collection interface extends Iterable and provides methods to manipulate a group of objects.
java
CopyEdit
Collection<String> list = new ArrayList<>();
This allows use of polymorphism (you can switch implementation later).
📘 Code Example
java
CopyEdit
import java.util.*;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(10); // add
s.push(20);
s.pop(); // remove top
System.out.println(s.peek()); // view top
}
}
Method Use
push() Add element
pop() Remove top element
peek() See top element
isEmpty() Check if stack is empty
// FIFO
dq.addLast("Java");
dq.addLast("Python");
System.out.println(dq.removeFirst()); // Java
// LIFO
dq.addFirst("C++");
dq.addFirst("HTML");
System.out.println(dq.removeFirst()); // HTML
}
}
✅ Best Use: Browser history (back-forward), undo-redo systems
java
CopyEdit
Set<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("Java"); // Duplicate - ignored
java
CopyEdit
Set<String> set = new TreeSet<>();
set.add("Zebra");
set.add("Apple");
System.out.println(set); // Apple, Zebra
✅ Use TreeSet when sorting is important (like leaderboard, dictionary)
ListIterator<String> li = names.listIterator();
System.out.println("Forward:");
while (li.hasNext()) {
System.out.println(li.next());
}
System.out.println("Backward:");
while (li.hasPrevious()) {
System.out.println(li.previous());
}
}
}
✅ Use ListIterator when you want full control of direction and editing while iterating.
setLayout(new BorderLayout());
add(new Button("North"), BorderLayout.NORTH);
setLayout(new FlowLayout());
add(new Button("A"));
add(new Button("B"));
3. GridLayout
f.setSize(300, 200);
f.setVisible(true);
}
}
✅ 14. Swing in Java
📌 AWT vs Swing
Feature AWT Swing
Platform OS dependent Pure Java (cross-platform)
Weight Heavyweight Lightweight
Package java.awt javax.swing
Look & Feel Native Customizable
Set layout
🧾 Example Code:
java
CopyEdit
import javax.swing.*;
public class MySwingFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("My First Swing App");
frame.add(label);
frame.add(button);
frame.setSize(300, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
✅ 15. JComponent & Swing Components
JComponent is the base class for all Swing UI components like buttons, labels, etc.
Tooltips
Borders
Double-buffering
Custom painting
All Swing widgets like JButton, JLabel, etc. inherit from JComponent.
setText("...")
setEnabled(true/false)
frame.add(label); frame.add(tf);
frame.add(cb1); frame.add(cb2);
frame.add(rb1); frame.add(rb2);
frame.add(btn);
frame.setSize(300, 250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Listener: Object that listens and reacts (e.g., class implementing ActionListener)
🧩 Think of it like:
User clicks → Button creates event → Listener handles event
📌 Example:
java
CopyEdit
JButton b = new JButton("Click");
b.addActionListener(new MyListener());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Load driver
Connect to DB
Get results
Close everything
🔸 A. Using Statement
java
CopyEdit
Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("SELECT * FROM
users");
Code of:
INSERT
SELECT
java
CopyEdit
rs.close();
pstmt.close();
con.close();