Posts

GUI Programming with Swing

Image
Chapter 13: GUI Programming with Swing in Java Chapter 13: GUI Programming with Swing in Java Java Swing is a part of Java Foundation Classes (JFC) that allows you to create rich graphical user interfaces (GUIs). It is built on top of AWT (Abstract Window Toolkit) but provides a more flexible and modern approach. ๐ŸŒŸ Why Use Swing? Lightweight and platform-independent Highly customizable Supports pluggable look and feel ๐Ÿ“Œ Common Swing Components JFrame – Main window JPanel – Container to group components JButton – Button JLabel – Text display JTextField – Single-line text input JTextArea – Multi-line text input ๐Ÿ”ณ Example: Simple GUI import javax.swing.*; public class SimpleGUI { public static void main(String[] args) { JFrame frame = new JFrame("My First Swing App"); JButton button = new JButton("Click Me"); frame.add(button); ...

File Handling in Java

Image
Chapter 10: File Handling in Java | Read, Write, Create Files ๐Ÿ“ Chapter 10: File Handling in Java File handling in Java is essential when dealing with data storage and retrieval in the form of text or binary files. Java offers various classes and methods in the java.io and java.nio packages to handle files. ๐Ÿ”น Why File Handling is Important? To store data persistently To read configuration or input data from external files To generate logs and reports To handle user uploads/downloads ๐Ÿ“ฆ Java File Handling Classes File – Represents a file or directory FileWriter – For writing text to files FileReader – For reading text files BufferedReader – Reads text efficiently, line by line BufferedWriter – Writes text efficiently Scanner – Reads file content easily with parsing capabilities ๐Ÿ“‚ 1. Creating a File import java.io.File; import java.io.IOException; public class CreateFil...

Object-Oriented Programming (OOP) in Java

Image
Chapter 7: Object-Oriented Programming (OOP) in Java Chapter 7: Object-Oriented Programming (OOP) in Java Java is a powerful object-oriented programming language. It emphasizes designing programs using real-world entities like objects and classes. OOP helps create modular, maintainable, reusable, and scalable software systems. ๐Ÿ”น Four Main Principles of OOP Encapsulation Inheritance Polymorphism Abstraction ๐Ÿ›ก️ 1. Encapsulation in Java Encapsulation is the concept of wrapping data (variables) and methods (code) into a single unit called a class. It helps hide the internal implementation of the object from the outside world. Example: class Person { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } } Benefits: Data hiding, code maintainability, security, and flexibility. ๐Ÿงฌ 2. Inheritance in Java ...

Control Flow in Java

Image
Chapter 5: Control Flow in Java Chapter 5: Control Flow in Java Control flow in Java refers to the order in which instructions, statements, and function calls are executed or evaluated. Java provides several mechanisms to control the flow of execution through the program: ๐Ÿงฉ 1. Conditional Statements Conditional statements execute different blocks of code based on certain conditions. ๐Ÿ”ธ if Statement int number = 10; if (number > 5) { System.out.println("Number is greater than 5"); } ๐Ÿ”ธ if-else Statement int number = 4; if (number > 5) { System.out.println("Greater than 5"); } else { System.out.println("Less than or equal to 5"); } ๐Ÿ”ธ if-else-if Ladder int score = 85; if (score >= 90) { System.out.println("Grade A"); } else if (score >= 80) { System.out.println("Grade B"); } else { System.out.println("Grade C"); } ๐Ÿงฉ 2. sw...

Exception Handling in Java

Image
Chapter 9: Exception Handling in Java Chapter 9: Exception Handling in Java In Java, exception handling is a powerful mechanism to handle runtime errors, allowing the normal flow of the program to continue. Without proper exception handling, errors can lead to unexpected termination of the program. ๐Ÿ“Œ What is an Exception? An exception is an event that disrupts the normal flow of a program’s execution. It is an object which is thrown at runtime. Common Exception Types: ArithmeticException – Divide by zero NullPointerException – Accessing object with null reference ArrayIndexOutOfBoundsException – Invalid index access NumberFormatException – Invalid number parsing ๐Ÿงช Java Exception Hierarchy All exceptions are subclasses of Throwable : Throwable Error Exception RuntimeException ๐Ÿ”ง Java Exception Handling Keyword...

Multithreading in Java

Image
Chapter 12: Multithreading in Java Chapter 12: Multithreading in Java Multithreading is one of Java's powerful features, allowing concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread . ๐Ÿ”„ 1. What is a Thread? A thread is a lightweight process. It's a path of execution within a program. Java supports multithreaded programming using built-in Thread class and Runnable interface. ๐Ÿ› ️ 2. Creating Threads in Java Method 1: By extending Thread class class MyThread extends Thread { public void run() { System.out.println("Thread is running..."); } } public class TestThread { public static void main(String args[]) { MyThread t1 = new MyThread(); t1.start(); } } Method 2: By implementing Runnable interface class MyRunnable implements Runnable { public void run() { ...

Java Collections Framework

Image
Chapter 11: Java Collections Framework Chapter 11: Java Collections Framework ๐Ÿ”ฐ Introduction The Java Collections Framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. It provides algorithms and utilities to manage groups of objects effectively. ๐ŸŒฒ Core Interfaces in Collections Collection: Root interface of the collection hierarchy. List: An ordered collection that allows duplicate elements (e.g., ArrayList, LinkedList). Set: A collection that does not allow duplicate elements (e.g., HashSet, TreeSet). Map: Stores key-value pairs (e.g., HashMap, TreeMap). Queue: Designed for holding elements prior to processing (e.g., LinkedList, PriorityQueue). ๐Ÿ”„ List Interface Ordered, allows duplicate elements. Access by index. List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); ๐Ÿงฑ Set Interfac...