Which of the following code segments show examples of autoboxing

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.

Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.

Use of Wrapper classes in Java

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.

  • Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
  • Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
  • Synchronization: Java synchronization works with objects in Multithreading.
  • java.util package: The java.util package provides the utility classes to deal with objects.
  • Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes are given below:

Autoboxing

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.

Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects.

Wrapper class Example: Primitive to Wrapper

Output:

Unboxing

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.

Wrapper class Example: Wrapper to Primitive

Output:

Java Wrapper classes Example

Output:

---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a
boolean value: true

Custom Wrapper class in Java

Java Wrapper classes wrap the primitive data types, that is why it is known as wrapper classes. We can also create a class which wraps a primitive data type. So, we can create a custom wrapper class in Java.

Introduction to oops concept in Java: why the object-oriented program is useful in java? As we all know that java is an object-oriented programming language(oops) that is, java supports object-oriented concepts. As we all know that java is not purely object-oriented programming language(because it supports primitive data types) sometimes we have to include oops concept in java to fulfil our requirements by creating objects to the data created. We use object-oriented programming because it enables re-usability, easier to maintain software, define abstract data types.

Introduction to wrapper classes in Java: before going into auto-boxing and unboxing let us understand what are wrapper classes. Wrapper classes: As the name itself suggests that wrap means combining and these are known as wrapper classes because they wrap or combine primitive data types into their corresponding object of wrapper classes. Hence, this is a brief explanation of wrapper classes in java. So we get a now clear idea about oops and wrapper classes so far. You might have a question that why are we discussing oops concept and wrapper classes. Don’t worry! Here you will get a detailed explanation about why we have discussed oops concept and wrapper classes in java.

Introduction to auto-boxing and unboxing in Java: 

As we all know the data input in java is expected in two types of data format i.e., primitive data types and reference data types. To convert primitive data types to reference data type and vice-versa, we use the concept of auto-boxing and unboxing in java. As discussed earlier, wrapper classes in java added TWO important features:

  • AUTO-BOXING
  • UNBOXING

Before knowing the above features in detail, let us learn about primitive data types and their corresponding wrapper classes.

Primitive data types: byte, short, int, long, float, double, char, boolean.
Wrapper classes: Byte, Short, Integer, Long, Float, Double, Character, Boolean.

NOTE: From the above, we can say that the wrapper classes are same as primitive data types as they only differ from their naming convention i.e., all the wrapper classes must start with upper-case letters. You guys might get a question that why are we discussing primitive data types and their corresponding wrapper classes. Don’t worry! Here we will discuss in-detail about auto-boxing and unboxing.       

AUTO-BOXING: Auto-boxing is one of the features of wrapper classes. As the name itself suggests that auto-boxing helps in conversion of primitive data types into their corresponding wrapper classes automatically.

simple example 1: 

int a = (you can enter any number of your own choice);
Integer i = a; //auto-boxing using constructor

example 2: 

int i = (you can enter any number of your own choice);
Integer j = Integer.valueof(i); //auto-boxing using value of() method

NOTE: In the above, auto-boxing can be done in two ways i.e, using constructors and method(valueof())

UNBOXING: Unboxing is also one of the features of wrapper classes. This helps in the conversion of an object of the wrapper class into its primitive data type. It is exactly the opposite process of auto-boxing.

a simple example:

Integer obj1 = new Integer (enter number of your choice);
int c = obj1.intvalue();
int a = obj1; // 

unboxing using methods

NOTE: In the above; unboxing is only done by methods.

  • ADVANTAGES OF AUTO-BOXING AND UNBOXING :
    • It helps the developers to write cleaner code and easy to understand
    • It helps us to convert primitive data type into wrapper classes and vice-versa internally, so it is easier for conversion

      Which of the following is part of the signature of a method group of answer choices?

      The signature of a method is: Its name. The number and types of its parameters, in order.

      Which of the following statements about overloaded constructors is true?

      Which among the following is true for constructors overloading? Explanation: The constructors can be overloaded only if the definitions have different signatures. Constructors doesn't have any return type so can't be overloaded using return type.

      Which of the following is a call to a static method quizlet?

      A static method must be invoked (called) by the class. A static method call looks like:className. methodName(parameters);

      Which of the following correctly declares a new variable which can be used to point to an object of the String class type?

      Which of the following correctly declares a new variable which can be used to point to an object of the String class type? String str; Which of the following best describes the relationship between a class and an object? A class is a template or blueprint from which an object can be created.