MVP (Model–View–Presenter) is a software architectural pattern that divides an application into three components—Model, View, and Presenter—to separate data, user interface, and business logic.
- Represents data and business logic
- Handles data processing, database operations, and rules
- Independent of UI and Presenter
- Notifies Presenter when data changes
Example:
Student records, database access classes
- Represents user interface (UI)
- Displays data received from Presenter
- Sends user actions (clicks, inputs) to Presenter
- Does not contain business logic
Example:
Java Swing forms, buttons, text fields
- Acts as a mediator between Model and View
- Receives input from View
- Processes logic and updates Model
- Retrieves data from Model and updates View
- Contains core application logic
- User interacts with View (e.g., button click)
- View forwards request to Presenter
- Presenter processes request and interacts with Model
- Model performs operations and returns result
- Presenter updates View with processed data
- Separation of concerns (UI, logic, data separated)
- Loose coupling between components
- High testability (Presenter can be tested independently)
- Better code organization
- Easier debugging and maintenance
- Reusable components
- Improved scalability
- Clear responsibility of each component
- Increased number of classes
- More complex structure for small applications
- Requires proper design understanding
- Desktop GUI applications (Java Swing, JavaFX)
- Android applications
- Enterprise-level applications
- Systems requiring clear UI and logic separation
A String in Java is a sequence of characters represented using the String class from the java.lang package and is used to store and manipulate textual data.
- Created using double quotes
- Stored in String pool
- Memory efficient
Example:
- Created using new keyword
- Stored in heap memory
- Creates a new object every time
Example:
- Immutable (cannot be modified after creation)
- Stored in String pool
- Provides various built-in methods
Common Methods:
- length() → returns length
- charAt() → returns character
- substring() → extracts part of string
- equals() → compares strings
- Mutable (can be modified)
- Thread-safe (synchronized)
- Slower than StringBuilder
Methods:
- append()
- insert()
- delete()
- Mutable and faster than StringBuffer
- Not thread-safe
- Used for high-performance string operations
- Special memory area inside heap
- Stores string literals
- Avoids duplicate objects
- Improves memory efficiency
- Concatenation → + operator or concat()
- Comparison → equals()
- Searching → indexOf()
- Conversion → toUpperCase(), toLowerCase()
A package in Java is a collection of related classes, interfaces, and sub-packages grouped together to organize code and avoid naming conflicts.
- Organizes large programs into logical groups
- Avoids class name conflicts
- Provides better code maintainability
- Supports access protection (security)
- Helps in code reuse and modularity
- Predefined packages provided by Java API
- Contain standard classes and interfaces
Examples:
- java.lang → basic classes (String, System)
- java.util → utilities (ArrayList, Scanner)
- java.io → input/output operations
- java.awt, javax.swing → GUI components
- Created by programmers to group related classes
- Improves project structure
Example:
- Use package keyword at the beginning of the program
- Compile using javac -d . ClassName.java
- Directory structure will be created automatically
Using import statement
Import entire package
- Prevents naming conflicts
- Provides access control using modifiers
- Improves readability and organization
- Supports reusability of code
Thread life cycle represents the different states through which a thread passes from its creation to termination during execution.
- Thread is created but not yet started
- Created using new Thread()
- No execution at this stage
- Thread is ready for execution
- After calling start() method
- Waiting for CPU allocation
- Thread is actively executing
- CPU is assigned to the thread
- Performs task defined in run() method
- Thread is temporarily inactive
- Waiting for resources or another thread
- Methods causing this state:
- sleep()
- wait()
- join()
- Thread completes execution or is stopped
- Cannot be restarted again
- New → Runnable → using start()
- Runnable → Running → CPU scheduler selects thread
- Running → Waiting/Blocked → due to sleep(), wait()
- Waiting → Runnable → after condition is satisfied
- Running → Terminated → after execution completes
- start() → starts thread
- run() → contains execution code
- sleep() → pauses thread
- join() → waits for another thread
- yield() → gives chance to other threads
- 5 states: New, Runnable, Running, Waiting/Blocked, Terminated
- start() method begins execution
- Thread cannot restart after termination
- Life cycle controlled by JVM scheduler
Exception handling is a mechanism in Java used to handle runtime errors so that the normal flow of the program can be maintained without termination.
- An unwanted event that occurs during program execution
- Disrupts normal flow of program
- Example: division by zero, null reference
- Checked Exceptions → occur at compile time (e.g., IOException)
- Unchecked Exceptions → occur at runtime (e.g., ArithmeticException)
- Contains code that may cause exception
- Must be followed by catch or finally
- Handles the exception thrown in try block
- Multiple catch blocks can be used
- Executes always, whether exception occurs or not
- Used for cleanup operations
- Used to explicitly throw an exception
- Declares exceptions in method signature
- Passes exception handling responsibility to caller
- Allows handling different exceptions separately
- Order should be from specific to general
- Custom exception created by extending Exception class
- Used for specific application needs
- Prevents program crash
- Maintains normal program flow
- Improves reliability and robustness
- Helps in debugging and error tracking
- Core keywords: try, catch, finally, throw, throws
- Two types: checked and unchecked
- Multiple catch improves handling
- User-defined exceptions extend Exception class
A String in Java is a sequence of characters represented by the String class in java.lang, used to store and manipulate textual data.
- Immutable (cannot be changed after creation)
- Stored in String pool (memory efficient)
- Provides many built-in methods
Returns number of characters
Returns character at given index
Extracts part of string
Compares two strings
Compares ignoring case
Joins two strings
Converts case
Finds position of character
Replaces characters
Removes extra spaces
| Feature | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Nature | Immutable | Mutable | Mutable |
| Thread Safety | Yes | Yes | No |
| Performance | Slow | Moderate | Fast |
- String is immutable
- Stored in String pool
- Many built-in functions available
- StringBuffer & StringBuilder used for modification
Inheritance is an OOP concept where one class (subclass) acquires the properties and methods of another class (superclass) using the extends keyword, promoting code reuse.
- Superclass (Base Class) → Class whose properties are inherited
- Subclass (Derived Class) → Class that inherits properties
- extends keyword → Used to implement inheritance
One subclass inherits from one superclass
A class inherits from another class, forming a chain
Multiple subclasses inherit from one superclass
Java does not support multiple inheritance using classes
Achieved using interfaces
- Code reusability
- Reduces redundancy
- Improves readability
- Supports polymorphism
- Inheritance uses extends keyword
- Promotes reuse and hierarchy
- Types: Single, Multilevel, Hierarchical, Multiple (via interface)
- Java avoids multiple inheritance with classes to prevent ambiguity
Control statements are used to control the flow of execution of a program based on conditions, loops, and branching.
Executes block if condition is true
Chooses between two conditions
Multiple conditions checked sequentially
Selects one case from many options
Executes while condition is true
Executes at least once
Used when number of iterations is known
Terminates loop or switch
Skips current iteration
Exits from method
- Control statements guide program execution
- Three types: Conditional, Looping, Jumping
- Used for decision-making and repetition
- Essential for logic building in programs
Operators in Java are special symbols used to perform operations on variables and values such as arithmetic, comparison, and logical operations.
Used for mathematical calculations
| Operator | Operation |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example:
Used to compare two values (returns boolean)
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Used to combine conditions
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Used to assign values
| Operator | Example |
|---|---|
| = | a = 10 |
| += | a += 5 |
| -= | a -= 2 |
| *= | a *= 3 |
| /= | a /= 2 |
Operate on single operand
| Operator | Operation |
|---|---|
| ++ | Increment |
| -- | Decrement |
| ! | Logical NOT |
Operate on bits
| Operator | Meaning |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | Complement |
Shortcut for if-else
- Operators perform operations on data
- Types: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, Ternary
- Used in calculations and decision-making
- Important for building program logic
- Easy to learn and use
- Syntax is similar to C++
- Avoids complex features like pointers and operator overloading
- Based on OOP concepts: encapsulation, inheritance, polymorphism
- Everything is treated as an object
- Follows Write Once, Run Anywhere (WORA)
- Java code is compiled into bytecode that runs on any JVM
- Java programs can run on different platforms without modification
- Fixed size of data types ensures portability
- No direct memory access (no pointers)
- Uses bytecode verification and security manager
- Provides safe execution environment
- Strong memory management
- Automatic garbage collection
- Exception handling reduces errors
- Not dependent on hardware architecture
- Bytecode can run on any machine with JVM
- Java bytecode is interpreted by JVM
- Supports dynamic execution
- Uses Just-In-Time (JIT) compiler
- Optimizes execution speed
- Supports multiple threads running simultaneously
- Improves efficiency and performance
- Supports network-based applications
- Provides APIs like RMI and networking packages
- Supports dynamic class loading at runtime
- Allows flexible and extensible programs
Method Overloading: Defining multiple methods with the same name but different parameters in the same class.
Method Overriding: Redefining a method of a superclass in a subclass with the same method signature.
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name with different parameters | Same method name and parameters in subclass |
| Polymorphism | Compile-time polymorphism | Runtime polymorphism |
| Class Requirement | Occurs within same class | Requires inheritance |
| Parameters | Must be different (number/type/order) | Must be same |
| Return Type | Can be same or different | Must be same or covariant |
| Method Signature | Must differ | Must be identical |
| Binding | Early binding (static binding) | Late binding (dynamic binding) |
| Access Modifier | No restriction | Cannot reduce access level |
| Keyword | No special keyword | @Override annotation used |
| Execution Time | Decided at compile time | Decided at runtime |
- Method name must be same
- Parameters must be different
- Return type alone is not sufficient
- Can occur in same class
- Method name and parameters must be same
- Requires inheritance (extends)
- Cannot override static, final, or private methods
- Access modifier cannot be more restrictive
- Runtime decision (dynamic binding)
- Overloading → compile-time, same class, different parameters
- Overriding → runtime, subclass, same method signature
- Overloading increases readability
- Overriding enables runtime polymorphism
Java Swing is a GUI (Graphical User Interface) toolkit in Java used to build window-based desktop applications and is part of the javax.swing package.
- Platform independent (runs on any system with JVM)
- Rich set of components (JButton, JLabel, JTextField, JTable)
- Lightweight components (not dependent on OS)
- Highly customizable (color, font, size)
- Event-driven programming support
- Follows MVC architecture
- Top-level containers → JFrame, JDialog, JApplet
- Intermediate containers → JPanel, JScrollPane
- Components → JButton, JLabel, JTextField, JTextArea
- Main window container
- Used to hold other components
- Represents a clickable button
- Generates action events
- Displays text or images
- Single-line text input
- Multi-line text input
- Container to group components
- FlowLayout → arranges components in a row
- BorderLayout → divides into North, South, East, West, Center
- GridLayout → arranges in grid form
- BoxLayout → arranges in row/column
- Handles user actions like button clicks
- Uses listeners such as ActionListener
- Event → Action → Response mechanism
- Desktop applications
- Banking systems
- Text editors
- Management systems
- GUI-based tools
- Swing is part of javax.swing
- Used for GUI development
- Contains rich components and layouts
- Event-driven and platform independent
0 Comments