unit 1
unit 1
Types of Streams
1. Byte Streams:
o Handles raw binary data (like images, audio files).
o Classes: InputStream (for input) and OutputStream (for output).
2. Character Streams:
o Handles text (characters) efficiently, supporting Unicode.
o Classes: Reader (for input) and Writer (for output).
Using Streams
Here’s how to use Java I/O streams in basic tasks:
1. Reading from a File
2. Writing to a File
Buffering Streams
Reading and writing one byte/character at a time can be slow.
Buffered streams (e.g., BufferedReader, BufferedWriter) make it faster by
handling chunks of data.
Example of Buffered Reader
Advanced Streams
1. Data Streams: Handle primitive data types (e.g., DataInputStream,
DataOutputStream).
2. Object Streams: Read/write objects to files (e.g., ObjectInputStream,
ObjectOutputStream).
3. Piped Streams: Connect two threads for communication.
Comparison: Byte vs Character Streams
Feature Byte Streams Character Streams
Data type handled Binary data (bytes) Text data (characters)
Classes used InputStream, OutputStream Reader, Writer
Example use cases Images, audio files Text files, documents
Everyday Analogy
Think of making tea:
Filters: The teabag filters the tea leaves, and a sieve removes particles.
Pipes: Water flows through the filters to create the final tea.
In programming, filters and pipes make it easier to process and transform data
step by step!
What is Bytecode?
Bytecode is a special, machine-independent code that Java programs are
converted into during compilation. Think of it as a set of instructions that are
understood by the Java Virtual Machine (JVM), not by your computer's
processor.
Human Code (Java Source Code): What you write (.java file).
Bytecode: What Java compiles your code into (.class file).
Machine Code: The actual instructions that your computer can execute.
Why Bytecode?
1. Portability: Bytecode can run on any system with a JVM, whether it’s
Windows, macOS, or Linux.
o This makes Java write once, run anywhere (WORA).
2. Efficiency: Bytecode is compact and optimized for the JVM.
Bytecode Example
Here's a simple Java program:
Each instruction (e.g., getstatic, ldc) is part of the bytecode that the JVM
interprets.
Summary
Bytecode is a middle step between your Java code and machine code.
The JVM interprets or compiles the bytecode to run your program.
This system makes Java portable, secure, and efficient!
What is a Thread?
A thread is a small unit of a process.
Process: A running program (e.g., your Java application).
Thread: A part of the program that can execute tasks independently.
By default, every Java program runs in a single thread called the main thread.
Thread States
A thread can be in one of these states:
1. New: Created but not yet started (e.g., new Thread()).
2. Runnable: Ready to run but waiting for the CPU.
3. Running: Actively executing.
4. Blocked/Waiting: Paused, waiting for resources or signals.
5. Terminated: Finished execution.
Thread Synchronization
When multiple threads access shared data, there can be conflicts.
Synchronization ensures that only one thread accesses the shared resource at a
time.
Example: Synchronized Block
class Counter {
private int count = 0;
Thread Priority
Threads can have priorities (low, medium, high), which the JVM considers when
scheduling threads. Use setPriority() to change it.
Advantages of Threads
1. Parallelism: Perform multiple tasks simultaneously.
2. Resource Sharing: Threads share memory and resources of the parent
process.
3. Better Performance: Tasks complete faster with multiple threads.
Challenges of Threads
1. Complexity: Managing multiple threads can be tricky.
2. Data Issues: Without synchronization, threads might interfere with each
other.
3. Deadlocks: Threads waiting indefinitely for resources can freeze the
program.
Analogy
Imagine a pizza restaurant:
Single-threaded: One chef makes pizzas, one at a time.
Multi-threaded: Multiple chefs work in parallel on different pizzas,
making the process faster.
Threads in Java allow programs to "hire" multiple workers to handle tasks
simultaneously!
What is Swing?
Swing is a part of the Java Foundation Classes (JFC).
It’s a library for building modern, interactive GUI applications in Java.
Swing is built on top of the older Abstract Window Toolkit (AWT) but is
more powerful and flexible.
Swing vs AWT
Feature Swing AWT
Components Lightweight (Java-rendered) Heavyweight (OS-rendered)
Features Rich and customizable Basic and limited
Portability Fully portable May vary by OS
Analogy
Think of Swing as a box of LEGOs:
Each LEGO piece (component) can be put together to build a larger
structure (GUI).
You can choose where and how to place the pieces (using layouts).
You can even paint and reshape the pieces (customization).
Summary
Swing is a toolkit for building GUI applications in Java.
It provides components like buttons, windows, and text fields.
You can customize layouts, add interactivity, and make responsive
applications.
While Swing is not the latest (JavaFX is newer), it remains widely used for
lightweight desktop applications.