> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/language/java/basic.md).

# Basic

**Java** is a **high-level**, **object-oriented**, and **platform-independent** programming language developed by **Sun Microsystems** (now owned by Oracle).

***

### **How Java Code Executes**

```
Java Source Code (.java)
        ↓
Java Compiler (javac)
        ↓
Bytecode (.class)
        ↓
Java Virtual Machine (JVM)
        ↓
Native Machine Code (Runs on OS)
```

**Explanation:**

1. You write your code in a file like `Main.java`.
2. The **compiler** (`javac`) converts it into **bytecode** (`Main.class`).
3. The **JVM** reads this bytecode and converts it into **machine code** that your computer understands.

This makes Java **“Write Once, Run Anywhere” (WORA)**.

***

### **Basic Components of Java**

#### 1. **Class**

A class is like a blueprint that defines **objects**.

```java
class Main {
    // This is a class
}
```

#### 2. **Function/Method**

A method is a block of code that performs a specific task.

```java
class Main {
    static void greet() {
        System.out.println("Hello, World!");
    }

    public static void main(String[] args) {
        greet();
    }
}
```

#### 3. **Main Method**

Every Java program starts running from the `main()` method.

```java
public static void main(String[] args) {
    // Program starts here
}
```

***

### Java Syntax & Basic Structure

Every Java program must have a **class**, and inside it, a **main()** method (the entry point).

```java
class Main {
    public static void main(String[] args) {
        System.out.println("Hello Java!");
    }
}
```

#### Explanation:

* `class Main` → defines a class named **Main**
* `public static void main(String[] args)` → main method (program starts here)
* `System.out.println()` → prints output **with a new line**

#### Output:

```
Hello Java!
```

***

### Variables & Data Types

Variables are containers that store data.

#### Example:

```java
class Main {
    public static void main(String[] args) {
        int age = 20;
        String name = "Rahul";
        double marks = 85.6;
        char grade = 'A';
        boolean pass = true;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + marks);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + pass);
    }
}
```

#### Output:

```
Name: Rahul
Age: 20
Marks: 85.6
Grade: A
Passed: true
```

***

#### **Java Data Types**

| Type      | Example      | Size    | Description                 |
| --------- | ------------ | ------- | --------------------------- |
| `byte`    | 10           | 1 byte  | small integer (-128 to 127) |
| `short`   | 1000         | 2 bytes | small integer               |
| `int`     | 50000        | 4 bytes | integer (default type)      |
| `long`    | 123456789L   | 8 bytes | large integer               |
| `float`   | 5.99f        | 4 bytes | decimal (less precision)    |
| `double`  | 19.99        | 8 bytes | decimal (more precision)    |
| `char`    | 'A'          | 2 bytes | single character            |
| `boolean` | true / false | 1 bit   | logical value               |
| `String`  | "Hello"      | varies  | text (not primitive)        |

***

### Input from User

To take input, we use the **Scanner class** (from `java.util` package).

#### Example:

```java
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = input.nextLine();

        System.out.print("Enter your age: ");
        int age = input.nextInt();

        System.out.println("Hello " + name + ", you are " + age + " years old.");
    }
}
```

#### Output:

```
Enter your name: Rahul
Enter your age: 20
Hello Rahul, you are 20 years old.
```

***

### Operators in Java

| Type                | Example                 | Description                  |
| ------------------- | ----------------------- | ---------------------------- |
| Arithmetic          | `+`, `-`, `*`, `/`, `%` | Math operations              |
| Assignment          | `=`, `+=`, `-=`, `*=`   | Assign values                |
| Comparison          | `==`, `!=`, `<`, `>`    | Compare values               |
| Logical             | `&&`, `\|\|`            | Combine conditions           |
| Increment/Decrement | `++`, `--`              | Increase/decrease value by 1 |

#### Example:

```java
int x = 10, y = 5;
System.out.println(x + y); // 15
System.out.println(x > y); // true
System.out.println(x == 10 && y == 5); // true
```

***

### Type Casting (Conversion)

#### **Widening** (automatic)

```java
int num = 10;
double val = num; // int → double
System.out.println(val); // 10.0
```

#### **Narrowing** (manual)

```java
double d = 10.5;
int i = (int) d; // double → int
System.out.println(i); // 10
```

***

### Comments in Java

Used to make code easier to understand.

```java
// Single-line comment

/* Multi-line
   comment block */
```
