Step-by-Step Java Tutorial for Complete Beginners

 ☕ Complete Java Tutorial for Beginners (With Examples & Code)

🌟 Java Notes for Beginners

Topics: Operators | Loops | Conditional Statements | Functions | OOP Concepts

🔹 1. Java Operators

What are Operators? Operators are symbols that perform operations on variables and values. They are essential for mathematical calculations, comparisons, and logic-based programming.

✨ 1.1 Arithmetic Operators

OperatorFunctionExampleOutput
+Addition5 + 38
-Subtraction5 - 23
*Multiplication4 * 28
/Division10 / 25
%Modulus10 % 31

✨ 1.2 Assignment Operators

Used to assign values to variables.

  • a = 5; assigns 5 to a
  • a += 3; is equivalent to a = a + 3;

✨ 1.3 Comparison Operators

Used to compare two values and return a boolean (true/false).

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)

✨ 1.4 Logical Operators

  • && (AND)
  • || (OR)
  • ! (NOT)

🔹 2. Conditional Statements

Why use conditional statements? They allow your program to make decisions and execute code based on conditions.

✨ 2.1 if Statement

if (score > 60) {
   System.out.println("Pass");
}

✨ 2.2 if-else Statement

if (age >= 18) {
   System.out.println("Eligible to vote");
} else {
   System.out.println("Not eligible");
}

✨ 2.3 else-if Ladder

if (marks >= 90) {
   System.out.println("Grade A");
} else if (marks >= 75) {
   System.out.println("Grade B");
} else {
   System.out.println("Grade C");
}

✨ 2.4 switch Statement

int day = 2;
switch(day) {
   case 1: System.out.println("Monday"); break;
   case 2: System.out.println("Tuesday"); break;
   default: System.out.println("Invalid day");
}

🔹 3. Java Loops

Why use loops? Loops execute a block of code repeatedly, saving time and reducing code repetition.

✨ 3.1 for Loop

for (int i = 1; i <= 5; i++) {
   System.out.println(i);
}

✨ 3.2 while Loop

int i = 1;
while (i <= 5) {
   System.out.println(i);
   i++;
}

✨ 3.3 do-while Loop

int i = 1;
do {
   System.out.println(i);
   i++;
} while (i <= 5);

✨ 3.4 for-each Loop

int[] arr = {10, 20, 30};
for (int num : arr) {
   System.out.println(num);
}

🔹 4. Java Functions (Methods)

What is a method? A method is a reusable block of code that performs a task. It improves code organization and reuse.

✨ Syntax:

returnType methodName(parameters) {
   // code
   return value;
}

✨ Example:

public class Main {
   static int add(int a, int b) {
       return a + b;
   }
   public static void main(String[] args) {
       System.out.println(add(5, 7));
   }
}

🔹 5. OOP Concepts (Object-Oriented Programming)

OOP models real-world entities as objects. It provides better structure, reuse, and security to the code.

✨ 5.1 Class and Object

class Student {
   String name;
   void display() {
       System.out.println(name);
   }
}
public class Main {
   public static void main(String[] args) {
       Student s = new Student();
       s.name = "John";
       s.display();
   }
}

✨ 5.2 Encapsulation

class Account {
   private int balance;
   public void setBalance(int b) { balance = b; }
   public int getBalance() { return balance; }
}

✨ 5.3 Inheritance

class Animal {
   void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {
   void bark() { System.out.println("Barking"); }
}

✨ 5.4 Polymorphism

Method Overloading

void show(int a) {}
void show(String b) {}

Method Overriding

class A { void show() { System.out.println("A"); } }
class B extends A {
   void show() { System.out.println("B"); }
}

✨ 5.5 Abstraction

abstract class Shape {
   abstract void draw();
}
class Circle extends Shape {
   void draw() {
       System.out.println("Drawing Circle");
   }
}

🧠 Object-Oriented Programming (OOP) in Java

What is OOP?
OOP stands for Object-Oriented Programming. It is a programming paradigm based on the concept of “objects”, which contain data (fields) and code (methods).

Java is a fully object-oriented language (except for primitive data types). OOP helps you build modular, reusable, and flexible code.

✨ Four Pillars of OOP

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

🔐 1. Encapsulation

Wrapping data (variables) and methods into a single unit (class) and restricting direct access using private access modifiers.

Benefits: Data hiding, secure code, modular structure.

class Account {
   private int balance;

   public void setBalance(int b) {
       balance = b;
   }

   public int getBalance() {
       return balance;
   }
}

🧬 2. Inheritance

Inheritance allows one class to acquire properties and behaviors (fields and methods) of another class.
Keywords: extends

Benefits: Code reusability and method overriding.

class Animal {
   void sound() {
       System.out.println("Animal sound");
   }
}

class Dog extends Animal {
   void bark() {
       System.out.println("Dog barks");
   }
}

🔄 3. Polymorphism

Poly means many, and morphism means forms.
It allows one method to perform different tasks based on context.

✔️ Types of Polymorphism:

  • Compile-Time Polymorphism (Method Overloading)
  • Run-Time Polymorphism (Method Overriding)

🔸 Method Overloading:

class Math {
   int add(int a, int b) {
       return a + b;
   }

   double add(double a, double b) {
       return a + b;
   }
}

🔸 Method Overriding:

class Animal {
   void sound() {
       System.out.println("Animal sound");
   }
}

class Dog extends Animal {
   @Override
   void sound() {
       System.out.println("Dog barks");
   }
}

🎭 4. Abstraction

Abstraction is the process of hiding internal details and showing only functionality. In Java, abstraction is achieved using:

  • Abstract Classes
  • Interfaces

🔸 Using Abstract Class:

abstract class Shape {
   abstract void draw();
}

class Circle extends Shape {
   void draw() {
       System.out.println("Drawing Circle");
   }
}

🔸 Using Interface:

interface Drawable {
   void draw();
}

class Rectangle implements Drawable {
   public void draw() {
       System.out.println("Drawing Rectangle");
   }
}

🧩 Advantages of OOP

  • Improved code reusability
  • Real-world modeling
  • Better data protection and security
  • Easier debugging and maintenance
  • Modular design for complex software

Post a Comment

0 Comments