# Variables and Data Types in Java — Primitives, Casting and Wrappers

Source: https://skills101.in/learn/java/java-variables-and-data-types — Skills101 free tutorial (Java Programming). Updated 2026-11-09.

In Java you must declare the **type** of every variable. The compiler then checks that you use it correctly — that's why Java catches many mistakes before the program even runs.

```java
public class Main {
    public static void main(String[] args) {
        int age = 21;
        double cgpa = 8.75;
        char grade = 'A';
        boolean placed = true;
        String name = "Asha";      // String is a class, not a primitive
        System.out.println(name + " " + age + " " + cgpa + " " + grade + " " + placed);
    }
}
```

```text
Asha 21 8.75 A true
```

## 1. The 8 primitive types

![Table of Java primitive types: byte, short, int, long, float, double, char and boolean with sizes, examples and default values](https://skills101.in/learn-img/java/primitive-types.svg)

- Whole numbers: `byte` < `short` < **`int`** < `long` (add `L`: `9000000000L`)
- Decimals: `float` (add `f`: `3.14f`) < **`double`**
- `char` holds one character in single quotes: `'A'`
- `boolean` is `true` or `false`

## 2. Type casting

**Widening** (small → big) happens automatically. **Narrowing** (big → small) needs a cast `(type)` and can lose data.

```java
public class Main {
    public static void main(String[] args) {
        int marks = 85;
        double d = marks;              // widening: automatic
        double price = 99.99;
        int rupees = (int) price;      // narrowing: decimal part is cut
        char c = 'A';
        int code = c;                  // char → its number
        System.out.println(d + " " + rupees + " " + code + " " + (char) (code + 1));
    }
}
```

```text
85.0 99 65 B
```

## 3. Integer division and overflow

```java
public class Main {
    public static void main(String[] args) {
        System.out.println(7 / 2);
        System.out.println(7 / 2.0);
        System.out.println(7 % 2);
        int big = Integer.MAX_VALUE;
        System.out.println(big);
        System.out.println(big + 1);        // overflow wraps around!
        long safe = (long) big + 1;
        System.out.println(safe);
    }
}
```

```text
3
3.5
1
2147483647
-2147483648
2147483648
```

> **Two classic traps:** `int / int` drops the decimal, and an `int` silently **overflows** past about 2.1 billion. Use `long` for big values (like sums of large arrays).

## 4. var (Java 10+)

```java
var count = 10;        // compiler infers int
var name = "Ravi";     // infers String
```

The type is still fixed — `var` just saves typing. It only works for local variables with an initial value.

## 5. Constants with final

```java
final double GST_RATE = 0.18;   // cannot be changed later
```

By convention, constants use UPPER_CASE names.

## 6. Wrapper classes and autoboxing

Each primitive has a class version: `int` → **`Integer`**, `double` → `Double`, `char` → `Character`, `boolean` → `Boolean`. Collections (like `ArrayList`) can only store objects, so Java converts automatically (**autoboxing**).

```java
public class Main {
    public static void main(String[] args) {
        Integer x = 100;                       // autoboxing
        int y = x + 5;                         // unboxing
        int parsed = Integer.parseInt("250");  // text → int
        String text = String.valueOf(42);      // int → text
        System.out.println(y + " " + parsed + " " + text.length());
        Integer a = 1000, b = 1000;
        System.out.println(a.equals(b));
    }
}
```

```text
105 250 2
true
```

> Compare wrapper objects with `.equals()`, not `==` (which compares references and can be false for values above 127).

## Common mistakes

- `float f = 3.14;` → error; write `3.14f` (decimals are `double` by default).
- Expecting `5 / 2` to be 2.5.
- Ignoring overflow when multiplying or summing big ints.

## Quick recap

- 8 primitives; `int` and `double` are the defaults you'll use most.
- Widening is automatic; narrowing needs `(type)` and may lose data.
- `int / int` is integer division; watch for overflow → use `long`.
- Wrappers (`Integer`…) for collections; `Integer.parseInt` converts text.


Practice quiz and free certificate: https://skills101.in/learn/java/java-variables-and-data-types