Auto-boxing and Auto-unboxing in java

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 7:- Auto-boxing and Auto-unboxing in java

Auto-boxing and auto-unboxing are features introduced in Java to automatically convert between primitive data types and their corresponding wrapper classes seamlessly. These features simplify the code and make it more convenient to work with primitives and their wrapper objects. Auto-boxing allows you to convert a primitive data type to its wrapper class, and auto-unboxing does the reverse, converting a wrapper object to its corresponding primitive data type.

  1. Auto-boxing: Auto-boxing is the process of automatically converting a primitive data type to its corresponding wrapper class when needed. For example, when you assign a primitive value to a variable of the wrapper class type, Java automatically creates an instance of the wrapper class and stores the primitive value in it.

Example of auto-boxing:

javaCopy code
int number = 42; // primitive data type Integer numberObj = number; // Auto-boxing: primitive to Integer (wrapper class)
  1. Auto-unboxing: Auto-unboxing is the process of automatically converting a wrapper class object to its corresponding primitive data type when needed. When you perform an operation that requires a primitive value, Java automatically extracts the primitive value from the wrapper object.

Example of auto-unboxing:

javaCopy code
Integer ageObj = 25; // wrapper class object int age = ageObj; // Auto-unboxing: Integer (wrapper class) to primitive int

Auto-boxing and auto-unboxing help simplify code and make it easier to work with collections that require objects (like ArrayList) or when using generics, which only work with reference types (objects).

Here's an example demonstrating the use of auto-boxing and auto-unboxing together:

javaCopy code
import java.util.ArrayList; public class AutoBoxingUnboxingExample { public static void main(String[] args) { ArrayList<Integer> numbersList = new ArrayList<>(); // Auto-boxing: Adding primitive int values to the ArrayList numbersList.add(10); numbersList.add(20); numbersList.add(30); // Auto-unboxing: Retrieving primitive int values from the ArrayList and using them in calculations int sum = numbersList.get(0) + numbersList.get(1) + numbersList.get(2); System.out.println("Sum of elements in the ArrayList: " + sum); } }

In the example above, the ArrayList holds Integer objects, but we can add primitive int values directly to it due to auto-boxing. Similarly, when retrieving values from the ArrayList, auto-unboxing allows us to use them in calculations without explicitly converting them back to primitives.


Disclaimer:-

Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.

 

2. Variable And Data Types

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?