<commit_before>---
date: 2020-09-05
linktitle: Wrapper Class
menu:
  main:
    parent: java
title: Wrapper Class in Java
weight: 3
url: /java/wrapper-class
description: Wrapper class is used to wrap a primitive data type like int, float, char. Wrapper class provides the functionality to encapsulate (wrap) a primitive data type to enable them to use as Objects.
keywords:
  - java
  - wrapper
tags: [Java]  
---
## What is a Wrapper Class?
As the name suggests, a wrapper class is used to wrap a primitive data type like `int`, `float`, `char` etc. The wrapper class provides the functionality to encapsulate (wrap) a primitive data type to enable them to use as Objects.  
Each primitive data type has a corresponding Wrapper class.  
Wrapper classes are provided by the `java.lang` package.

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

### Example
```java
void wrapperClassExample1(){
    Integer int1 = new Integer(1);  // Deprecated since, Java 9
    System.out.println(int1);
    Integer int2 = Integer.valueOf(2);
    System.out.println(int2);

    // Calculating Sum
    System.out.println(int1 + int2);

    // Example with another type
    Character another = Character.valueOf('a');
    System.out.println(another);
}
```
From Java 9, new `Integer()` format is deprecated and `Integer.valueOf()` method is preferred. Here `Integer` could be replaced by any Wrapper Class like `Boolean`, `Float` etc.

### Example
Deprecated: `Float deprecated = new Float(1.21);`  
Preferred: 	`Float preferred = Float.valueOf(1.21);`

Wrapper classes provide one more handy functionality which is to convert values from String to primitive data types. Like, if an integer is in String format like "2" we can use Parsing methods to get Integer value 2 or Float value 2.0.
### Example
```java
void wrapperClassExample2() {
    String value = "3";
    Integer intValue = Integer.parseInt(value);
    Float floatValue = Float.parseFloat(value);
    System.out.println("Actual Value  = " + value);
    System.out.println("Integer Value  = " + value);
    System.out.println("Float Value  = " + value);

    <commit_msg>wrapper<commit_after>---
date: 2020-09-05
linktitle: Wrapper Class
menu:
  main:
    parent: java
title: Wrapper Class in Java
weight: 3
url: /java/wrapper-class
description: Wrapper class is used to wrap a primitive data type like int, float, char. Wrapper class provides the functionality to encapsulate (wrap) a primitive data type to enable them to use as Objects.
keywords:
  - java
  - wrapper
tags: [Java]  
---
## What is a Wrapper Class?
As the name suggests, a wrapper class is used to wrap a primitive data type like `int`, `float`, `char` etc. The wrapper class provides the functionality to encapsulate (wrap) a primitive data type to enable them to use as Objects.  
Each primitive data type has a corresponding Wrapper class.  
Wrapper classes are provided by the `java.lang` package.

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

### Example
```java
void wrapperClassExample1(){
    Integer int1 = new Integer(1);  // Deprecated since, Java 9
    System.out.println(int1);
    Integer int2 = Integer.valueOf(2);
    System.out.println(int2);

    // Calculating Sum
    System.out.println(int1 + int2);

    // Example with another type
    Character another = Character.valueOf('a');
    System.out.println(another);
}
```
From Java 9, `new Integer()` format is deprecated and `Integer.valueOf()` method is preferred. Here `Integer` could be replaced by any Wrapper Class like `Boolean`, `Float` etc.

### Example
Deprecated: `Float deprecated = new Float(1.21);`  
Preferred: 	`Float preferred = Float.valueOf(1.21);`

Wrapper classes provide one more handy functionality which is to convert values from String to primitive data types. Like, if an integer is in String format like "2" we can use Parsing methods to get Integer value 2 or Float value 2.0.
### Example
```java
void wrapperClassExample2() {
    String value = "3";
    Integer intValue = Integer.parseInt(value);
    Float floatValue = Float.parseFloat(value);
    System.out.println("Actual Value  = " + value);
    System.out.println("Integer Value  = " + value);
    System.out.println("Float Value  = " + value);

    
{'fraction_non_alphanumeric': 0.12140833670578713, 'fraction_numerical': 0.012950222581950627, 'mean_word_length': 2.686055182699478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6174694', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1290, 'n_words': 546}