Posts

Showing posts with the label Core Java Concepts

Application Programming Interface (API): Definition, Types, and Examples in Java

📘 Application Programming Interface (API) An Application Programming Interface (API) is a collection of interfaces, implementation classes, and helper classes that enables communication between two different applications. APIs act as a bridge that allows one software application to interact with another without knowing its internal implementation. An API is generally provided in the form of a JAR (Java Archive) file. Most APIs are developed and maintained by software vendors to allow developers to use their services or features. 🔍 Example of API ✅ JDBC (Java Database Connectivity) JDBC is a standard Java API Used to connect Java applications with a database server Allows execution of SQL queries from Java programs 🧩 Categories of API APIs can be classified into three main categories based on how interfaces and implementations are packaged. 🔹 Category 1 API Interface, implementation classes, and helper classes are present in a single JAR file Easy to use and deploy 📌 Example: Core...

Design Patterns in Java: JavaBean and Singleton Design Pattern Explained with Examples

📘 Design Pattern in Java A Design Pattern is a reusable solution to commonly occurring problems in software design and application development. Design patterns help developers write clean, maintainable, scalable, and efficient code. They do not provide code directly, but they provide best practices and standard approaches to solve design issues. 🔷 JavaBean Design Pattern A JavaBean Design Pattern is used to create simple, reusable, and encapsulated Java classes mainly for data transfer within an application. ✅ Rules of JavaBean Class A JavaBean class must follow these rules: The class must be declared as public It may implement the Serializable marker interface All instance variables must be private It must contain public getter (accessor) and setter (mutator) methods It must have a public or protected constructor It should not contain business logic (recommended) 🎯 Purpose of JavaBean Design Pattern Used for data transportation inside an application The object of a JavaBean class ...

Properties File in Java: Configuration, Reading Data, and Methods Explained

📘 Properties File in Java A Properties file is a configuration file used to store application settings—most commonly database credentials—in the form of key-value pairs. It helps separate configuration data from application logic, making the application easier to maintain and modify. 🔑 Format of a Properties File Properties db.url=jdbc:mysql://localhost:3306/testdb db.username=root db.password=admin 📦 Reading a Properties File in Java To read data from a properties file, Java provides the Properties class, which is available in the java.util package. Required Packages: java.util java.io 🧩 Steps to Read a Properties File Create an object of the Properties class Open the file using FileReader Load the key-value pairs into the Properties object Retrieve values using keys 🔧 Important Methods of Properties Class 🔹 load() Method Used to load key-value pairs from a file into the Properties object. void load(FileReader fr) throws IOException 📌 This method belongs to the Properties cla...

Static Import in Java: Definition, Syntax, Advantages, and Examples

📘 Static Import in Java Static Import is a feature introduced in JDK 1.5 that allows programmers to access static members (variables and methods) of a class, abstract class, or interface without using the class name. It helps make the code shorter, cleaner, and more readable, especially when static members are used frequently. 🔹 Why Use Static Import? Normally, static members are accessed using the class name. With static import, the class name is not required. ✔️ Benefits : Reduces code verbosity Improves readability Makes frequently used static members easy to access 🧩 Syntax of Static Import import static packageName.ClassName.staticMember; OR (to import all static members) import static packageName.ClassName.*; 🧠 What Can Be Imported Using Static Import? Static variables Static methods Static constants 📌 Applicable to: Class Abstract class Interface 🌍 Real-Life Example Think of static import like saving a contact number: Without static import → You dial the full number every...

Database Configuration in Java: Definition, Components, and Best Practices

📘 Database Configuration in Java Database Configuration refers to a set of credentials and settings used by an application to establish communication with a database server. These details allow a Java application to connect securely and perform database operations such as insert, update, delete, and retrieve data. 🔑 Components of Database Configuration A typical database configuration includes the following details: Database URL – Specifies the database location and type Port Number – Identifies the port on which the database server is running Username – Authorized user to access the database Password – Authentication credential for the user 📌 Note: Database credentials are unique for every database server and should be handled securely. 📂 Where Database Configuration Can Be Written Database configuration details can be stored in different places depending on the application design: Interface Properties file (.properties) web.xml file (for web applications) 🧩 Database Configurati...

File Reading in Java Using FileReader: Step-by-Step Guide with Examples

📘 File Reading in Java (Using FileReader Class) What is File Reading in Java? File Reading in Java is the process of reading data from a file stored on the system. Java provides the FileReader class (not “File Reading”) inside the java.io package to read character-based data from files. 📦 FileReader Class The FileReader class is used to read text data from a file character by character. 📌 Package java.io 🧱 Constructor of FileReader FileReader(String filePath) throws FileNotFoundException Opens a file located at the given file path. Internally creates an input stream to read data from the file. Throws FileNotFoundException if the file does not exist. 🛠️ Methods of FileReader Method.               Description int read().    Reads one character at a time and returns its ASCII (Unicode) value void close().   Closes the input stream and releases system resources 🔄 How File Reading Works (Step by Step) 1. Create a FileReader ob...