What is autoboxing and unboxing in java ?

Java automatically convert primitive types in to corresponding wrapper class is called autoboxing and converting wrapper class in to primitive types called unboxing. This feature introduced in java 1.5
 
 
Example
 
package com.vinod.test;

import java.util.ArrayList;
import java.util.List;

public class AutoBoxing {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList();
        list.add(1); // Autoboxing
        list.add(2);
        int a = list.get(0);// unboxing
        int b = list.get(1);
        System.out.println("value of a " + a + "value of b" + b);

    }

}
 

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts