Day-85 Understanding Wrapper Classes and Collections in Java



This content originally appeared on DEV Community and was authored by Tamilselvan K

Wrapper Classes in Java

Java is not purely object-oriented because it uses primitive data types (int, float, char, etc.). But sometimes, we need these primitives to act like objects—for example, when working with Collections (like ArrayList, HashMap) that only store objects.

This is where Wrapper Classes come in. They wrap primitive types into objects.

Available Wrapper Classes

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

Boxing and Unboxing

  • Boxing – Converting a primitive type into its corresponding wrapper object.
   int num = 10;
   Integer obj = Integer.valueOf(num);  // Boxing
  • Unboxing – Converting a wrapper object back to its primitive type.
   Integer obj = 20;
   int num = obj.intValue();  // Unboxing

Autoboxing and Auto-unboxing (Java 5+)

From Java 5 onwards, manual conversion is not always needed.

  • Autoboxing – Automatically converts primitive to wrapper
  int n = 100;
  Integer obj = n;   // Autoboxing
  • Auto-unboxing – Automatically converts wrapper to primitive
  Integer obj = 50;
  int n = obj;   // Auto-unboxing

Collections Framework in Java

In real-world applications, we often need to store, manage, and manipulate groups of objects. Java provides the Collections Framework (part of java.util package) for this purpose.

What is a Collection?

A Collection is an object that represents a group of objects.
The Collections Framework provides:

  • Interfaces (e.g., List, Set, Map, Queue)
  • Implementations (e.g., ArrayList, HashSet, HashMap, LinkedList)
  • Algorithms (e.g., sorting, searching)

Interfaces in Collections

1.List

  • Ordered
  • Allows duplicate elements
  • Maintains insertion order
  • Implementations: ArrayList, LinkedList

2.Set

  • Unordered
  • Does not allow duplicates
  • Implementation: HashSet,TreeSet,LinkedHashSet

3.Queue / Deque

  • Used for storing elements in FIFO/LIFO order

4.Map

  • Key-value pair storage
  • No duplicate keys
  • Implementation: HashMap,Treemap,LinkedHashMap

ArrayList (Most Commonly Used List)

  • A resizable array implementation of List
  • Maintains insertion order
  • Allows duplicates

Example: ArrayList without Generics

If we don’t use generics, an ArrayList can store different data types.

package collectiondemo;

import java.util.ArrayList;

public class ArrayListDemo {

    public static void main(String[] args) {

         ArrayList s1 = new ArrayList();   // without generics → can store any datatype
         s1.add(10);                       // add()
         s1.add(15);
         s1.add("hii");
         System.out.println(s1);           // [10, 15, hii]

         s1.add(0, 5);                     // add() with index
         System.out.println(s1);           // [5, 10, 15, hii]

         System.out.println(s1.get(0));    // get() → 5

         s1.set(2, 20);                    // set()
         System.out.println(s1);           // [5, 10, 20, hii]

         s1.remove(0);                     // remove()
         System.out.println(s1);           // [10, 20, hii]

         System.out.println(s1.size());    // size() → 3
    }
}

Explanation of Methods Used:

  • add() – Adds element to the list
  • add(index, element) – Inserts element at specific index
  • get(index) – Fetches element at given index
  • set(index, element) – Updates element at given index
  • remove(index) – Removes element at given index
  • size() – Returns number of elements


This content originally appeared on DEV Community and was authored by Tamilselvan K