• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Autoboxing and Unboxing in Java

Overview

In this tutorial, we will learn about Autoboxing and Unboxing in Java. These features were introduced in J2SE 5.

Autoboxing

Autoboxing is the automatic conversion by the Java compiler of the primitive type to the corresponding Wrapper class object.

Example:
Converting an int type to an Integer wrapper class object.

Integer i = 9; // autoboxing int to Integer

Character c = ‘a’;  // autoboxing char to Character

Table that shows the Java primitive types and the corresponding Wrapper classes.

Java Primitive Type Java Wrapper class
boolean Boolean
byte Byte
char Character
int Integer
float Float
double Double
short Short
long Long

Sample Code

public class AutoBoxingDemo {
 public static void main(String[] args) {
 ArrayList list = new ArrayList<>();
 for (int i = 1; i < 10; i++) { 
 // int to -> Integer 
 list.add(Integer.valueOf(i));
 }
 }
}

AutoBoxing Java Program

In the above code snippet list is a list of Integer objects. In the loop we have added int primitives type to the list. Notice that, Java complier does not complain about the type mismatch. The complier creates an Integer object from i and adds to the list. This conversion is called autoboxing. Autoboxing can be done during assignment, method invocation, etc.

Unboxing

Unboxing is the reverse conversion. It is the automatic conversion of Wrapper class object to their corresponding primitive type by the Java compiler.

Sample Code Example

import java.util.ArrayList;

//UnBoxing Demo Program
//Java Tutorials - www.TestingDocs.com
public class UnBoxingDemo {
 public static void main(String[] args) {
 int primInt = 0;
 Integer i = Integer.valueOf(9);

 // Unboxing through assignment
 ArrayList list = new ArrayList<>();
 list.add(3.14f);
 float f = list.get(0);

 // Unboxing through method call
 primInt = foo(i);
 System.out.println(primInt);
 }

 public static int foo(int i) {
 return i;
 }

}

Unboxing in Java Demo Program

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Java Performance

Java Tutorials /

Improving Java Performance with Multithreading

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

‹ Java Math Class› Java String format() method with Examples

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com