Posts

Object-Oriented Programming in Java with Real-Life Examples

Object-Oriented Programming (OOP) in Java Object-Oriented Programming (OOP) is a programming approach that organizes software design around objects rather than functions or logic. Java is a pure object-oriented programming language, which means everything revolves around objects and classes. 1. What Is an Object?      An object is a real-world entity that has: State (properties or characteristics) Behavior (actions or functionality) In Java: State is represented by instance variables Behavior is represented by instance methods Java also supports a special member called an instance initializer block, which is used to initialize instance-level data. This block executes when an object is created and does not initialize static members of the class. Real-Life Example of an Object Car Object State (Properties): color, speed, brand, fuelType Behavior (Functions): start(), accelerate(), brake(), stop() Car  ├─ State → color, speed, brand  └─ Behavior → start(), drive(),...

Loop Statements in Java: While, Do-While, and For Loop Explained

Loop Statements in Java Loop statements in Java are used to execute a block of code repeatedly as long as a given condition is true. They help reduce code duplication and make programs efficient. Java provides three types of loop statements: While Loop Do-While Loop For Loop 1. While Loop ✅ Definition The while loop executes a block of code only if the condition is true. The condition is checked before executing the loop body. 🔹 Syntax        while (condition) {                // loop body       } 🌍 Real-Life Example       Checking mobile battery level       → While battery is not full, keep charging. 📊 Flowchart (While Loop)             Start                  |                 v       Check Condition              (tr...

Conditional Statements in Java Explained with Syntax

Conditional Statements in Java Conditional statements are used to control the flow of execution in a Java program based on certain conditions. They allow the program to make decisions and execute different blocks of code. Java provides the following conditional statements: if statement if-else statement if-else-if-else statement switch statement Real-Life Examples of Conditional Statements in Java 1. if Statement – Real-Life Example 📌 Example: Checking age for voting Real life: If a person’s age is 18 or more, they are eligible to vote. int age = 20; if (age >= 18) {     System.out.println("Eligible to vote"); } 👉 If the condition is true, the message is printed. 👉 If false, nothing happens. 2. if-else Statement – Real-Life Example 📌 Example: Pass or Fail result Real life: If marks are greater than or equal to 40, the student passes; otherwise, they fail. int marks = 35; if (marks >= 40) {     System.out.println("Pass"); } else {     System.out.pr...

Operators in Java Explained: Arithmetic, Relational, and Logical

Operators in Java Operators are special symbols used to perform operations on variables and values in Java. Java provides several types of operators to perform arithmetic, logical, relational, and other operations. 1. Arithmetic Operators       Used to perform basic mathematical operations. + Addition - Subtraction * Multiplication / Division % Modulus (remainder) 2. Relational Operators      Used to compare two values and return a boolean result (true or false). > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to 3. Compound Assignment Operators       Used to perform an operation and assign the result to the same variable. += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign %= Modulus and assign 4. Unary Operators      Used to operate on a single operand. ++ Increment -- Decrement ! Logical negation Increment Operators Post-inc...

Final Keyword in Java Explained with Examples

  Final Keyword in Java The final keyword in Java is used to make a variable’s value constant, meaning the value cannot be changed once assigned. Final Variables When a variable is declared as final: The variable must be initialized once Reinitialization is not allowed Any attempt to change the value results in a compile-time error Example : final int x = 10; // x = 20; // Compile-time error Final with Static and Instance Variables When a static variable or instance variable is declared as final, it does not support default values Such variables must be declared and initialized together The value must be assigned only once Valid syntax: class Example {     static final int A = 100;     final int B = 50; } Purpose of Final Keyword The final keyword is used to: Create constant values Prevent accidental modification of variables Improve code readability and safety

Variables in Java Explained: Local, Static, and Instance

Variables in Java A variable is a symbolic name associated with a value. The value of a variable can change during program execution. In Java, variables are classified into three types: Local Variable Static Variable Instance Variable 1. Local Variable A local variable is declared inside a method, constructor, or block. Key points: Also called a method-level variable Accessible only within the block {} where it is declared Does not support default values Must be initialized before use If declared but not initialized and used, it causes a compile-time error If declared but not used, no error is generated Example : void display() {     int x = 10; // local variable     System.out.println(x); } 2. Static Variable A static variable is declared inside a class but outside any method, using the static keyword. Key points: Also called a class-level variable Supports default values Memory is allocated once and shared among all objects Can be accessed:     By variabl...

Primitive Data Types in Java with Size and Default Values

Primitive Data Types in Java Primitive data types are basic data types in Java that determine the type and amount of memory required to store a value. Java provides a fixed set of primitive data types for efficient memory usage. There are two main categories of primitive data types: Numeric Non-Numeric 1. Numeric Data Types Numeric data types are used to store numerical values. They are further divided into two types: a) Integer Data Types Integer data types store whole numbers (without decimal points). byte- 8 bits short- 16 bits int- 32 bits long- 64 bits b) Floating-Point Data Types Floating-point data types store decimal numbers. float- 32 bits double- 64 bits 2. Non-Numeric Data Types Non-numeric data types are used to store non-numerical values. char – Stores a single character and occupies 16 bits of memory boolean – Stores true or false (Memory size is JVM-dependent) Default Values in Java A default value is a value automatically assigned by the compiler when a data type is d...