# Wrapper Classes and Java Annotations in Java

**Introduction**

Java provides **Wrapper Classes** to represent primitive data types as objects.

Java also provides **Annotations** to add metadata to classes, methods, variables, and other program elements.

In this article, we will learn the basic concepts of Wrapper Classes and Java Annotations.

* * *

**Part 1: Wrapper Classes in Java**

**What are Wrapper Classes?**

**Definition**

A **Wrapper Class** is a class that converts a primitive data type into an object.

Each primitive data type has a corresponding wrapper class.

| Primitive | Wrapper Class |
| --- | --- |
| `byte` | `Byte` |
| `short` | `Short` |
| `int` | `Integer` |
| `long` | `Long` |
| `float` | `Float` |
| `double` | `Double` |
| `char` | `Character` |
| `boolean` | `Boolean` |

* * *

**Wrapper Class Syntax**

```java
WrapperClass object = value;
```

Example:

```java
Integer number = 100;
```

* * *

**Example**

```java
public class Main {

    public static void main(String[] args) {

        Integer number = 100;
        Double price = 99.50;
        Character grade = 'A';
        Boolean status = true;

        System.out.println(number);
        System.out.println(price);
        System.out.println(grade);
        System.out.println(status);

    }
}
```

### Output

```text
100
99.5
A
true
```

* * *

**Autoboxing**

**Definition**

**Autoboxing** is the automatic conversion of a primitive value into its corresponding wrapper object.

**Example**

```java
public class Main {

    public static void main(String[] args) {

        int number = 100;

        Integer value = number;

        System.out.println(value);

    }
}
```

Here, Java automatically converts:

```text
int → Integer
```

* * *

**Unboxing**

**Definition**

**Unboxing** is the automatic conversion of a wrapper object into its corresponding primitive value.

**Example**

```java
public class Main {

    public static void main(String[] args) {

        Integer value = 100;

        int number = value;

        System.out.println(number);

    }
}
```

Here, Java automatically converts:

```text
Integer → int
```

* * *

**Why Wrapper Classes are Used?**

Wrapper classes are useful when:

*   An object is required instead of a primitive.
    
*   Working with Collections such as `ArrayList<Integer>`.
    
*   Converting strings into numbers.
    
*   Using utility methods provided by wrapper classes.
    

Example:

```java
ArrayList<Integer> numbers = new ArrayList<>();
```

Collections cannot directly store primitive types, so wrapper classes are used.

* * *

**Part 2: Java Annotations**

**What are Annotations?**

**Definition**

**Annotations** in Java are metadata that provide additional information about program elements such as classes, methods, fields, and parameters.

Annotations do not directly change the core logic of a method.

**Syntax**

```java
@AnnotationName
```

Example:

```java
@Override
```

* * *

**Common Java Annotations**

Some commonly used built-in annotations are:

| Annotation | Purpose |
| --- | --- |
| `@Override` | Indicates method overriding |
| `@Deprecated` | Indicates an outdated API |
| `@SuppressWarnings` | Suppresses compiler warnings |
| `@FunctionalInterface` | Indicates a functional interface |

* * *

**@Override**

**Definition**

`@Override` indicates that a method overrides a method from a parent class.

**Example**

```java
class Animal {

    void sound() {

        System.out.println("Animal sound");

    }
}

class Dog extends Animal {

    @Override
    void sound() {

        System.out.println("Dog barks");

    }
}

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.sound();

    }
}
```

**Output**

```text
Dog barks
```

* * *

**@Deprecated**

**Definition**

`@Deprecated` indicates that a class, method, or field should no longer be used.

**Example**

```java
class Calculator {

    @Deprecated
    void oldMethod() {

        System.out.println("Old method");

    }
}
```

* * *

**@SuppressWarnings**

**Definition**

`@SuppressWarnings` tells the compiler to suppress specific warnings.

**Example**

```java
@SuppressWarnings("unchecked")
public class Main {

    public static void main(String[] args) {

        System.out.println("Hello Java");

    }
}
```

* * *

**@FunctionalInterface**

**Definition**

`@FunctionalInterface` indicates that an interface is intended to have exactly one abstract method.

**Example**

```java
@FunctionalInterface
interface Calculator {

    int add(int a, int b);

}
```

* * *

**Custom Annotation**

**Definition**

A **custom annotation** is an annotation created by the developer for a specific purpose.

Syntax

```java
@interface AnnotationName {

}
```

Example

```java
@interface Developer {

    String name();

}

@Developer(name = "John")
class Employee {

}
```

Here, `@Developer` is a custom annotation.

* * *

**Wrapper Classes vs Annotations**

| Wrapper Classes | Annotations |
| --- | --- |
| Convert primitives into objects | Provide metadata |
| Examples: `Integer`, `Double` | Examples: `@Override`, `@Deprecated` |
| Used in Collections and object-based APIs | Used by compiler, tools, and frameworks |
| Support autoboxing and unboxing | Can provide configuration/metadata |

* * *

**Conclusion**

**Wrapper Classes** allow primitive data types to be represented as objects. Common examples include `Integer`, `Double`, `Character`, and `Boolean`. Java also provides **autoboxing** and **unboxing** for automatic conversion between primitives and wrapper objects.

**Java Annotations** provide metadata about program elements. Common annotations include `@Override`, `@Deprecated`, `@SuppressWarnings`, and `@FunctionalInterface`.

Understanding Wrapper Classes and Annotations is important for Java development, especially when working with **Collections, Java 8 features, Spring, Spring Boot, and other Java frameworks**.
