Table of Contents
How does Autoboxing work in Java?
Examples of Autoboxing in Java
Example #2
Advantages
Conclusion
Home Java javaTutorial Autoboxing in Java

Autoboxing in Java

Aug 30, 2024 pm 03:58 PM
java

Autoboxing is a process followed in JAVA wherein the conversion of primitive data is converted into the object type by the compiler. So, for example, if the variable is declared as “int,” then the variable is converted into object data type from primitive integer by the compiler, which is used in that format. There is one more process which is the reversal of autoboxing which is called “unboxing.”

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Definition: Conversion of any value of primitive data type into an object of a respective wrapper class is called autoboxing.

Syntax

The syntax used for autoboxing is as follows-

1

primitive_DataType variableName = 'VariableValue';

Copy after login

How does Autoboxing work in Java?

JAVA compiler takes the primitive data type and converts it into an object of the wrapper class. Then, the compiler works only if the below two points are satisfied:

  1. Variables are passed as a parameter to the function, which would expect object creation of wrapper class.
  2. Primitive values are assigned to variables so that the compiler can perform autoboxing.
  3. All the primitive data types are linked to their respective wrapper class (This is already done by JAVA stored in their libraries). The primitive data types and their linked wrapper classes are linked as:
Primitive data type Wrapper class
boolean Boolean
byte Byte
long Long
double Double
short Short
char Character
Int Integer
float Float

Examples of Autoboxing in Java

Below are the examples of autoboxing in JAVA which are helpful to understand this concept further.

Example #1

Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

import java.io.*;

class example1

{

public static void main (String[] args)

{

//here we are creating an integer "test1" having value of "100".

Integer test1 = new Integer(100);

System.out.println("Here is the value of integer test1 which is created into object of the wrapper class integer: " + test1);

//here we are autoboxing a character "test2" with a value of "a".

Character test2 = 'a';

System.out.println("Here is the value of test2 variable after autoboxing using character wrapper class: " + test2);

}

}

Copy after login

Output:

Autoboxing in Java

Explanation:

The JAVA library named “java.io” was imported so that JAVA prime functionalities can be used. First, the main class name “example1” was created containing the main function. The main function is the point where the execution of the program will start. Next, an integer named “test1” was created and assigned with the value. It is slightly different from the normal way of declaring and assigning the values; as you can see, the wrapper class “Integer” was instantiated, and the value to be assigned is passed as a parameter to the wrapper class. This process is called objectifying the primitive data type using the wrapper class. No doubt that “JAVA” is called a fully object-oriented language.

In the next section of code, the Character primitive data type is assigned to a variable named “test2” with a value “a.” This is a usual way of declaring and assigning the variable with the value. This is working because the JAVA compiler is smart enough to convert the primitive data type into an object of its wrapper class automatically in the back. This functionality is called autoboxing in JAVA.

Example #2

Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class BoxingWidening

{

// The function below is demonstrating the boxing functionality in JAVA.

static void testFunction(char i)

{

System.out.println("Program output for boxing:: ");

System.out.println("char in short");

}

// This function is demonstrating the widening functionality in JAVA.

static void testFunction(Character i)

{

System.out.println("Program output for widening: ");

System.out.println("Character in full");

}

public static void main(String args[])

{

char ch='a';

testFunction(ch);

}

}

Copy after login

Output:

Autoboxing in Java

Explanation:

In this example, the difference between “widening” and “boxing” is demonstrated. Widening is a functionality where the full class name is used, for example, Integer or character instead of int or char. Two functions are defined with the same name, “testFunction,” one with widening and the other with boxing syntax declarations and assignments. In the main function, a character named “ch” is declared and assigned with a value “a,” and a function is called with its function name. This function has two definitions on its name. Since JAVA prefers boxing, the function’s boxing definition is called, and the parameter “ch” is passed to the boxing definition. The final result is displayed using the print() function. This function shows boxing definition in the output screen rather than taking a widening definition. Certainly, autoboxing is much powerful in comparison to the widening functionality offered by the JAVA language.

Advantages

Some of the advantages derived from this useful functionality by JAVA are enlisted below:

  1. The developer has to write less code, and this reduces the burden on the coder so that the coder can concentrate on complex logic.
  2. Cleaner code as the complexity of creating an object and instantiating it is abstracted due to compiler intelligence.
  3. The best strategy for information is picked up by the compiler using this. For example, if we want the value of the integer to be picked up, then we should use code: valueOf (int) rather than new Integer (int). This improves turnaround time.
  4. Promotes abstraction.
  5. Reduces the possibility of bugs in the lengthy code/ project because of abstraction.

Conclusion

Autoboxing is one of the best strategies to abstract complex code from the developer by adding compiler intelligence. This reduces the overhead on the developer of writing small code snippets which are not logically related. This is used so extensively by developers that the instantiating of a wrapper class is not known to many developers. Autoboxing is certainly a powerful and cleaner way of declaring and assigning the values to the variables.

The above is the detailed content of Autoboxing in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles