# Data Types

## Data Types

In Java, **data types** define the type of data that a variable can hold. They also determine the amount of memory allocated to the variable and the range of values it can store.

### **1\. Categories of Data Types**

Java data types are broadly classified into two categories:

1. **Primitive Data Types**
    
2. **Non-Primitive (Reference) Data Types**
    

This section focuses on **primitive data types**.

### **2\. Primitive Data Types**

Java has **eight primitive data types**, which are divided into four groups based on the kind of values they store:

#### **A. Integral Types**

Used to store whole numbers (integers).

* **byte**
    
* **short**
    
* **int**
    
* **long**
    

#### **B. Floating-Point Types**

Used to store numbers with decimal points.

* **float**
    
* **double**
    

#### **C. Character Type**

Used to store a single Unicode character.

* **char**
    

#### **D. Boolean Type**

Used to store logical values.

* **boolean**
    

### 3\. Primitive Data Type Details

| **Type** | **Size** | **Range** | **Default** |
| --- | --- | --- | --- |
| **byte** | 1 byte | \-128 to 127 | 0 |
| **short** | 2 byte | \-32768 to 32767 | 0 |
| **int** | 4 byte | \-2147483648 to 2147483647 | 0 |
| **long** | 8 byte | — | 0 |
| **float** | 4 byte | ±1.4E−45 to ±3.4E+38 | 0.0f |
| **double** | 8 byte | ±4.9E−324 to ±1.7E+308 | 0.0d |
| **char** | 2 byte | 0 to 65535 | \\u0000 |
| **boolean** | depends on JVM (1 bit or 1 byte) | true / false | false |

## What are Variables ?

A **variable** is a name given to a memory location that stores a value. In Java, variables are used to store data that can be used and modified during program execution.

Each variable has:

1. A **data type** — which defines the kind of value it can store (e.g., integer, float, character, etc.)
    
2. A **name** — which identifies the variable in the program
    
3. A **value** — which represents the actual data stored
    

The general syntax for declaring a variable is:

`dataType variableName = value;`

`int i = 175;` ⬜ ⬜ ⬜ ⬜

`float f = 25.3f;` ⬜ ⬜ ⬜ ⬜

`char c = ‘A’;` ⬜ ⬜

`byte b = 5;` ⬜

## Rules for Variable Names

In Java, variable names (also called *identifiers*) must follow specific rules to ensure that the code is valid and readable. The following are the key rules and best practices for naming variables:

#### **1\. Case Sensitivity**

Variable names in Java are **case-sensitive**.  
This means `age`, `Age`, and `AGE` are considered three different variables.

#### **2\. Allowed Characters**

A variable name can contain:

* Alphabets (`A–Z`, `a–z`)
    
* Digits (`0–9`)
    
* The underscore (`_`)
    
* The dollar sign (`$`)
    

#### **3\. Starting Character**

A variable name **must start** with:

* A letter (A–Z or a–z)
    
* An underscore (`_`)
    
* Or a dollar sign (`$`)
    

It **cannot start with a digit**.

#### **4\. Not a Keyword**

A variable name **cannot be a Java keyword** (like `class`, `int`, `if`, `return`, etc.).

#### **5\. Avoid Class Names in Use**

If a class with a certain name already exists, avoid using that same name for a variable to prevent confusion and ambiguity.

#### **6\. No Length Limit**

There is **no limit** to the length of a variable name.  
However, names should be kept **meaningful and concise** for readability.

#### **7\. Use Camel Case Convention**

Java developers commonly follow the **camelCase** naming convention for variables:

* The first word starts with a lowercase letter.
    
* Each subsequent word begins with an uppercase letter.
    

## What are Literals

A **literal** is a constant value that is directly written in the code. It represents a fixed value that does not change during program execution. In Java, literals are used to assign values to variables of various data types.

### **1\. Integer Literals**

Integer literals represent whole numbers (without a fractional part). For example:

`z = 2 x + 17 y;`

Here, `2` and `17` are **integer literals**.  
Variables of type `byte`, `short`, and `int` can be initialized using integer literals (of type `int`), as long as the value fits within the range of the target type.

A **long** variable is initialized using a long literal by appending `L` or `l` at the end of the number:

`long distance = 4124L;`

### **2\. Floating-Point Literals**

Floating-point literals represent real numbers that contain a decimal point.  
A **float** literal is written with an `f` or `F` suffix:

`float rate = 2.34f;`

A **double** literal can be written either with or without a `d` or `D` suffix:

`double price = 456.41;`

`double tax = 2.34D;`

### **3\. Character Literals**

A **character literal** represents a single character enclosed within single quotes:

`char c = 'A';`

### **4\. String Literals**

A **string literal** represents a sequence of characters enclosed within double quotes:

`String s = "Java";`

### **5\. Boolean Literals**

A **boolean literal** has only two possible values: `true` or `false`.

`boolean isActive = true;`

`boolean isComplete = false;`

### **6\. Number System Literals**

Java supports several types of number system literals:

* **Binary literals** (prefix `0b` or `0B`):  
    `int binaryNum = 0b1010;` // equivalent to 10 in decimal
    
* **Octal literals** (prefix `0`):  
    `int octalNum = 012;` // equivalent to 10 in decimal
    
* **Hexadecimal literals** (prefix `0x` or `0X`):  
    `int hexNum = 0xA;` // equivalent to 10 in decimal
    

## Integral Data Type

When Java was first introduced, **32-bit systems** were the standard in computing. Because of this, Java designed the `int` data type to occupy **4 bytes (32 bits)**.  
If integers had been stored in only 2 bytes (16 bits), it would have underutilized the processor’s capabilities, leading to slower performance.  
Hence, `int` in Java is always **32 bits**, regardless of the underlying machine architecture, ensuring platform independence.

### **Understanding How Integers Are Stored**

To understand how **integral data types** (like `byte`, `short`, `int`, `long`) store numbers, we need to look at **binary representation**.

Let’s take the example of the `byte` data type.

* A `byte` is **1 byte = 8 bits**.
    
* It can store values from **–128 to 127**.
    
* The **most significant bit (MSB)** — the **7th bit** (counting from 0 to 7, right to left) — is used as the **sign bit**.
    
    * `0` → positive number
        
    * `1` → negative number
        

**Example: Representing 127**  
`01111111`

* The first bit (0) indicates it’s **positive**.
    
* The remaining 7 bits represent the value `1111111`, which equals **127** in decimal.
    

#### **Example: Representing -5**

To store a negative number like **\-5**, Java (and most programming languages) uses a system called **Two’s Complement** representation.

Here’s how it works step by step:

1. **Find the binary of 5:**  
    `00000101`
    
2. **Find the One’s Complement:**  
    (Invert all bits: 0 → 1 and 1 → 0)  
    `11111010`
    
3. **Find the Two’s Complement:**  
    (Add 1 to the One’s Complement)  
    `11111010 + 1 = 11111011`
    
4. **Result:**  
    `11111011 → represents -5`
    

## Float Data Type

In Java, **floating-point data types** are used to represent numbers that have a fractional part — that is, numbers with **decimal points**. Examples include `3.14`, `0.001`, or `-98.6`.

Java provides two floating-point types:

* **float** (4 bytes, single precision)
    
* **double** (8 bytes, double precision)
    

Both types follow the **IEEE 754 standard** for representing floating-point numbers in binary form.

### **Understanding Floating-Point Representation**

A floating-point number is represented internally using **scientific notation**.  
In this format, a number is expressed as:

Number = Mantissa × 10^(Exponent)

* The **mantissa (or significand)** represents the actual digits of the number.
    
* The **exponent** represents the power of 10 (or 2 in binary form) that determines where the decimal point is placed.
    

This allows very large or very small numbers to be stored efficiently.

### **Example: Representing 163.52**

Let’s understand how `163.52` can be expressed in floating-point form.

163.52 = 16352 / 100 = 16352 × 10^−2

* **Mantissa:** 16352
    
* **Exponent:** -2
    

This can be written as: `16352E-2`

In Java notation, the letter `E` (or `e`) is used to represent “×10 to the power of”.  
So, `16352E-2` means: 163.52

### **How Float Is Stored Internally**

A `float` in Java is a **32-bit (4-byte)** value and is divided into three parts as per the **IEEE 754 single-precision** format:

| **Part** | **Bits** | **Description** |
| --- | --- | --- |
| **Sign Bit** | 1 | Indicates whether the number is positive (`0`) or negative (`1`). |
| **Exponent** | 8 | Stores the exponent (power of 2), adjusted by a bias of 127. |
| **Mantissa (Fraction)** | 23 | Stores the fractional part of the number. |

So, a 32-bit float looks like this: \[ Sign (1 bit) \]\[ Exponent (8 bits) \]\[ Mantissa (23 bits) \]

For a deeper look at **how these bits actually represent floating-point numbers in memory** (with binary breakdowns and examples), check out this detailed explanation:

👉 [Understanding IEEE 754 Floating-Point Representation (external link)](https://en.wikipedia.org/wiki/IEEE_754)

👉 [How float or double values are stored in memory? (external link)](https://www.log2base2.com/storage/how-float-values-are-stored-in-memory.html)
