# Java for beginners

## <mark>Let's take a look at Java</mark>

* Java is a versatile, powerful, and widely-used programming language. Its combination of object-oriented principles, portability, and strong ecosystem makes it a preferred choice for developers working on a wide range of applications.
    
* Whether you're building web applications, mobile apps, or enterprise-level software, Java continues to be an excellent choice due to its stability, performance, and large community of developers.
    

> **Here are the steps to install Java on your system.**

### **1\. What is Java?**

Java is a programming language used to create software, apps, and websites. It is **easy to learn** for beginners and can be run on any computer or device.

* **Write Once, Run Anywhere**: This means that you can write a program in Java, and it can run on any computer (like Windows, Mac, etc.) without any changes.
    
* **Object-Oriented**: Java organizes programs into **objects** (things that have properties and actions) and **classes** (blueprints for creating objects).
    

### **2\. Core Concepts of Java**

Let’s dive deeper into the basic building blocks of Java.

#### 2.1 **Variables**

A **variable** is like a box where you store data. Each box has a **type**, which tells the program what kind of data it holds. For example, numbers go into an **integer** type box, and text goes into a **string** type box.

* **Example Types**:
    
    * **int**: Used for whole numbers (like 10, 20, 50).
        
    * **double**: Used for numbers with decimals (like 3.14, 20.5).
        
    * **String**: Used for text (like "Hello", "Java").
        
    * **boolean**: Used for true/false values (like true, false).
        

You **declare** a variable by naming it and specifying its type.

#### 2.2 **Classes and Objects**

* A **class** is like a blueprint. It tells you what an object will be like and what it will do.
    
* An **object** is a specific instance of a class, created from the class blueprint. For example, the class could be "Car," and an object would be a red car or a blue car.
    

#### Key Ideas:

* **Class** = Blueprint
    
* **Object** = Instance of the class (a real thing)
    

For example, if you have a **Car** class, it might have properties like **color** and **speed**, and actions like **drive** or **stop**. From this blueprint, you can create different **Car objects**: a red car, a blue car, etc.

#### 2.3 **Methods**

**Methods** are instructions inside a class that tell objects what to do. Think of a method like a task or action.

* A **method** could be something like **drive**, which makes a car move, or **accelerate**, which increases the speed of the car.
    
* You call a method to perform an action. For example, to make the car **drive**, you call the **drive** method.
    

### **3\. Java Program Structure (What it looks like)**

Every Java program consists of **classes**, **methods**, and **statements** (commands for the program to follow). Let’s break it down.

1. **Class**: You start by defining a class. Every program has at least one class.
    
2. **Main Method**: Inside a class, there is a special method called the **main method**. This is the entry point of the program—where Java starts executing your code.
    
3. **Statements**: Inside the methods, you write statements to define what the program does. For example, telling the program to print something on the screen or to perform calculations.
    

### **4\. Why Use Java?**

Because it’s:-

* **Simple to Learn**: It has easy-to-understand syntax (rules for writing code).
    
* **Cross-Platform**: Java programs can run on any device.
    
* **Large Community**: There are plenty of resources to help you learn.
    
* **Widely Used**: Java is used for building big software systems, mobile apps, and websites.
    

### **5\. Basic Flow in Java**

1. **Create a Class**: Think of it as a template. You define what the object will have (properties like color, speed) and what it can do (like moving).
    
2. **Create an Object**: From the class, you make specific objects (like a red car or a blue car).
    
3. **Call Methods**: Once you have an object, you can call its methods to perform actions (like start the car).
    

### **6\. Main Things to Remember**:

* **Variables**: Store information like numbers or text.
    
* **Classes**: Define how objects work.
    
* **Methods**: Set of instructions that objects can perform.
    
* **Objects**: Instances of a class that can have properties and actions.
    

### **7\. How Control Works in Java**

In Java, you control the flow of your program using **decisions** and **loops**. This helps your program decide what to do at different points.

7.1 **If-Else Statements**

These are used when you want your program to make a decision, like:

* **If something is true**, do this.
    
* **Else**, do that.
    

For example:

* If the temperature is more than 30°C, wear shorts.
    
* Else, wear a jacket.
    

#### 7.2 **Loops**

A loop allows your program to repeat something multiple times without having to write the same code again. There are two main types:

* **For Loop**: Repeat a task a set number of times.
    
* **While Loop**: Repeat a task as long as a condition is true.
    

Example: If you want to print numbers from 1 to 5, you can use a loop to repeat the action of printing each number.

### 8\. How Does Java Work?

1. **Write the Code**:
    
    * You write the Java code using a text editor or a tool called an **IDE** (Integrated Development Environment). This is where you type the instructions (called "code") for your program.
        
2. **Compile the Code**:
    
    * Once you write your code, it gets **compiled** into **bytecode**. This is done by a special program called the **Java Compiler**. The bytecode is not understood by humans but can be run by the JVM.
        
3. **Run the Program**:
    
    * After compiling, the bytecode is run on any computer with a **JVM**. The JVM translates the bytecode into instructions that your computer can understand and execute.
        

### 9\. **Java Development Components**

1. **JDK (Java Development Kit)**:
    
    * The **JDK** is the complete package needed for developing Java applications. It includes the **Java Compiler**, **JVM**, libraries, and other tools for development.
        
2. **JVM (Java Virtual Machine)**:
    
    * The **JVM** is the engine that runs Java applications. It converts the compiled Java code (bytecode) into machine code that your computer can understand.
        
3. **JRE (Java Runtime Environment)**:
    
    * The **JRE** includes the **JVM** and libraries required to run Java programs. It doesn’t include tools for development like the JDK.
        

### **10\. Object-Oriented Programming (OOP) in Detail**

Java is an **object-oriented** programming language. This means everything is based around **objects** and **classes**. Here’s how it works:

#### 10.1 **Encapsulation**

Encapsulation means packing data and methods(like functions) in single unit (like a capsule). In other words Hiding the details of how data is stored and only showing necessary parts to interact with it.

And Only special methods can open the box to get or change the data.

So, encapsulation is nothing but **keeping data safe** inside an object and providing methods (functions) to access that data. You don’t directly change the data inside the object; you do it through methods.

For example, a **Car** object has a property like **speed**. You can’t directly change **speed** from outside, but you can use a method like **accelerate** to safely increase the speed.

#### 10.2 **Inheritance**

Inheritance is when one class (child class) can inherit (hold/takes) properties and behaviors (methods) from another class (parent class).

**Why do we use it?**: To avoid repeating code and reuse what’s already there.

For example:

* You can have a **Vehicle** class with common properties like **speed** and **color**.
    
* A **Car** class and a **Truck** class can inherit from **Vehicle**. So, both Car and Truck will have properties like **speed** and **color**, but they can also have their own unique behaviors (like **truckLoad()** for the truck).
    

#### 10.3 **Polymorphism**

Polymorphism allows the same method to behave differently based on the object that is calling it. For example:

* A **Car** object and a **Truck** object might both have a **honk()** method, but they might make different sounds when the method is called.
    
* **Example**: A car **honks** with "Beep beep!", and a **Truck** honks with "HONK HONK!" but both use the same ***honk()*** method.
    

### **11\. Error Handling (What Happens When Something Goes Wrong?)**

In any program, things can go wrong. **Error handling** helps the program continue running even when something unexpected happens.

In Java, you can use **try-catch** blocks to handle errors. This means:

* You **try** to run a piece of code.
    
* If there’s an error, you can **catch** it and show a message, instead of letting the program crash.
    

> **"That’s all for now." Thank you.!**

---
