Thursday, 29 December 2016

Autoboxing and Unboxing in Java


Autoboxing and Unboxing feature are added from Java 5 onwards.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
·  Passed as a parameter to a method that expects an object of the corresponding wrapper class.
·  Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i: li)
        if (i % 2 == 0)
            sum += i;
        return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i : li)
        if (i.intValue() % 2 == 0)
            sum += i.intValue();
        return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
·  Passed as a parameter to a method that expects a value of the corresponding primitive type.
·  Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don't have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

Autoboxing and Unboxing

Autoboxing and Unboxing

Autoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

- See more at: http://learnerpoint.in/2016/12/29/autoboxing-unboxing-java/#sthash.I7v19ysf.dpufAutoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

- See more at: http://learnerpoint.in/2016/12/29/autoboxing-unboxing-java/#sthash.I7v19ysf.dpufAutoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:
dddddddddddddddddd

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...