0% found this document useful (0 votes)
5 views4 pages

Java Notes Summary

The document provides a concise overview of Java concepts covered in Units 3 and 4, focusing on Threads, AWT, I/O, JDBC, Networking, and Collections. It explains thread creation, synchronization, AWT components, event handling, JDBC connectivity, and the Collections Framework. Key features such as input/output streams, object serialization, and remote method invocation (RMI) are also highlighted.

Uploaded by

khahapamnani080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Java Notes Summary

The document provides a concise overview of Java concepts covered in Units 3 and 4, focusing on Threads, AWT, I/O, JDBC, Networking, and Collections. It explains thread creation, synchronization, AWT components, event handling, JDBC connectivity, and the Collections Framework. Key features such as input/output streams, object serialization, and remote method invocation (RMI) are also highlighted.

Uploaded by

khahapamnani080
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Notes — Concise Summary

Units 3 & 4 | Threads, AWT, I/O, JDBC, Networking, Collections

UNIT 3 — Threads & AWT

1. Threads
A thread is a lightweight sub-process — the smallest unit of execution. Advantages: efficiency (parallel tasks)
and responsiveness (if one thread blocks, others continue).
Thread lifecycle states:
New → Active (Runnable / Running) → Waiting/Blocked → Timed Waiting → Terminated
Runnable: ready, waiting for CPU. Running: CPU allocated. Waiting/Blocked: waiting for a resource or
another thread. Timed Waiting: sleeping for a set duration via sleep(). Terminated: finished or killed by
exception.

2. Creating Threads
Method 1 — Extend Thread class:
class MyThread extends Thread { public void run() { ... } }
new MyThread().start();
Method 2 — Implement Runnable interface:
class MyTask implements Runnable { public void run() { ... } }
new Thread(new MyTask()).start();

3. Thread Synchronization
Prevents race conditions. Only one thread can hold the monitor lock at a time. Used via synchronized blocks or
methods.
synchronized(obj) { /* shared resource access */ }
Types: Process Sync (coordinate multiple processes) and Thread Sync — which includes Mutual Exclusive
(synchronized method/block/static) and Inter-thread communication.

4. Thread Communication (Inter-thread)


Avoids inefficient polling. Three methods of Object class, must be used inside synchronized blocks:
• wait() — releases lock, thread sleeps until notified.
• notify() — wakes one waiting thread (does NOT release lock immediately).
• notifyAll() — wakes all waiting threads.
wait() vs sleep(): wait() releases the lock; sleep() holds it. wait() is in Object class; sleep() is in Thread class.

5. AWT — Abstract Window Toolkit


API for GUI/window-based Java applications. Platform-independent through JVM and abstract APIs.
Heavyweight — uses OS native components.
Hierarchy: Object → Component → Container → Window/Panel → Frame/Dialog
Container types: Window (no border/menu), Panel (no title/border, holds components), Frame (full AWT
window with title/menu/border), Dialog (message display).
Components: Button, Label (read-only text), Checkbox (true/false), Choice (dropdown popup), List (scrollable
multi-select). All in [Link] package.
Key Component methods: add(Component c), setSize(w,h), setLayout(lm), setVisible(bool).
6. Layout Managers
Control the positioning and sizing of components inside containers.
• BorderLayout — 5 regions: NORTH, SOUTH, EAST, WEST, CENTER. Default for frames.
• FlowLayout — left-to-right row, wraps as needed.
• GridLayout — uniform grid of rows × columns.
• CardLayout — stacked components, one visible at a time (use next()/previous()).
• GridBagLayout — complex grid; components can span multiple rows/columns.
• GroupLayout — hierarchical groups; common in IDE GUI builders.

7. AWT Events
Foreground events: user-triggered (button click, keypress, mouse move).
Background events: OS-triggered (failures, interrupts, operation completion).
Java uses the Delegation Event Model: Sources generate events → registered Listeners handle them.
Register via addTypeListener() e.g. addActionListener(), addKeyListener().
Common event classes: ActionEvent, KeyEvent, MouseEvent, MouseWheelEvent, FocusEvent, ItemEvent,
WindowEvent, TextEvent, ComponentEvent, ContainerEvent, AdjustmentEvent.
3 approaches for handling: within class (implements ActionListener), other class (pass reference), anonymous
class (inline new ActionListener(){...}).

8. Adapter Classes
Provide default (empty) implementations of listener interfaces — so you only override the methods you need,
saving boilerplate code.
Key adapters: WindowAdapter, KeyAdapter, MouseAdapter, MouseMotionAdapter, FocusAdapter,
ComponentAdapter, ContainerAdapter ([Link]); MouseInputAdapter, InternalFrameAdapter
([Link]).

UNIT 4 — I/O, JDBC, Networking & Collections

1. Input/Output Streams
3 default streams: [Link] (keyboard), [Link] (screen output), [Link] (error output).
By operation: InputStream (read — FileInputStream, BufferedInputStream, ByteArrayInputStream) and
OutputStream (write — FileOutputStream, BufferedOutputStream, ByteArrayOutputStream).
By file type:
• ByteStream — byte-by-byte (8-bit). Key classes: FileInputStream, FileOutputStream, BufferedInputStream,
DataInputStream, PrintStream.
• CharacterStream — Unicode character-by-character. Key classes: FileReader, FileWriter, BufferedReader,
BufferedWriter, InputStreamReader, PrintWriter.

2. Stream Filter
Intermediate stream operation using a Predicate. Lazy — no execution until a terminal operation is reached.
stream().filter(p -> [Link] > 30000).map(p -> [Link]).forEach([Link]::println);

3. Buffered Streams
BufferedOutputStream — wraps an OutputStream with an internal buffer for better write performance.
Methods: write(int b), write(byte[] b, int off, int len), flush().
BufferedInputStream — wraps an InputStream; internal buffer auto-refills from the underlying stream.
Methods: read(), read(byte[], off, len), skip(), mark(), reset(), available().

4. Data Streams
DataInputStream — reads Java primitive types in machine-independent way: readInt(), readByte(), readChar(),
readDouble(), readBoolean(), readUTF(), skipBytes().
DataOutputStream — writes primitives: writeInt(), writeByte(), writeChar(), writeBoolean(), writeLong(),
writeUTF(), flush().

5. RandomAccessFile
Allows both read and write at any file position (like a byte array). Implements DataInput, DataOutput, Closeable.
Key methods: read(), read(byte[]), readBoolean(), readByte(), readChar(), readDouble(), readFloat(), readInt(),
readLong(), readFully().

6. JDBC — Java Database Connectivity


Standard API for Java apps to connect to relational databases (MySQL, Oracle, SQL Server, etc.).
4 components: JDBC API ([Link] package), Driver Manager (loads DB-specific drivers), Test Suite (tests
CRUD operations), JDBC-ODBC Bridge (translates JDBC to ODBC calls).
4 driver types: Type-1 (JDBC-ODBC bridge), Type-2 (Native-API, partly Java), Type-3 (Network Protocol, full
Java), Type-4 (Thin driver, full Java — most common).
2 architecture models: Two-tier (Java app ↔ DB directly) and Three-tier (Java app ↔ middleware ↔ DB).
Typical JDBC flow:
[Link](driverName); // load driver
Connection con = [Link](url, user, pass);
Statement st = [Link]();
int rows = [Link](query); // or executeQuery() for SELECT
[Link]();

7. Object Serialization
Serialization — converts object state to a byte stream for storage or network transfer. Deserialization —
recreates the object from the byte stream. Platform-independent.
Implement [Link] (marker interface — no methods). Use [Link]() to
serialize and [Link]() to deserialize.
Rules: Static and transient fields are NOT serialized. Constructor is NOT called during deserialization. Child
inherits serializable from parent but not vice versa. Associated objects must also implement Serializable.

8. Sockets
Used for communication between applications on different JVMs. Client needs the server's IP address and port
number.
• ServerSocket — server side. accept() blocks until client connects, then returns a Socket instance.
• Socket — both sides use for I/O. getInputStream() and getOutputStream() for data exchange.
• Connection-less alternative: DatagramSocket + DatagramPacket.
ServerSocket ss = new ServerSocket(6666);
Socket s = [Link](); // blocks until client connects

9. RMI — Remote Method Invocation


Lets a Java object call methods on an object in another JVM (same or remote machine). Uses two
intermediaries: Stub (client-side proxy — packages params and sends to server) and Skeleton (server-side —
unpacks params, calls real object).
6 steps: (1) Define remote interface extending Remote. (2) Implement it extending UnicastRemoteObject. (3)
Run rmic to generate Stub/Skeleton. (4) Start rmiregistry. (5) Run server (createRegistry + [Link]). (6)
Run client ([Link] + method call).

10. JNI — Java Native Interface


Bridge between Java and native code (C/C++). Declare methods with the native keyword in Java, implement in
C/C++, load library with [Link]("name"). Useful for performance-critical code or platform-specific
APIs.

11. Collections Framework


Root interface in [Link]. Not directly implemented — used via subinterfaces. Implements Iterable<E>.
3 main subinterfaces:
• List — ordered, duplicates allowed. Implementations: ArrayList, Vector, Stack.
• Set — unordered, no duplicates. Implementations: HashSet, TreeSet, LinkedHashSet.
• Queue — FIFO order. Implementations: PriorityQueue, ArrayDeque, LinkedList.
Key methods: add(e), addAll(c), remove(o), contains(o), size(), isEmpty(), clear(), iterator(), stream(), toArray(),
forEach().

End of Summary — Java Notes Units 3 & 4

You might also like