What are Arrays in Java?
An Array in Java is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Each item in an array is called an element, and each element is accessed by its numerical index (starting from 0).
Array Declaration & Initialization
// Different ways to declare and initialize arrays in Java // Method 1: Declaration then initialization int[] numbers; numbers = new int[5]; // Creates array of size 5 // Method 2: Declaration and initialization in one line int[] scores = new int[10]; // Method 3: Declaration with values (array literal) int[] values = {10, 20, 30, 40, 50}; // Method 4: Multi-dimensional array int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Accessing and modifying array elements scores[0] = 95; // Set first element int firstScore = scores[0]; // Get first element
Complete Array Example
public class ArrayExample { public static void main(String[] args) { // Declare and initialize an array int[] numbers = {10, 20, 30, 40, 50}; // Get array length System.out.println("Array length: " + numbers.length); // Print array using for-each loop System.out.print("Array elements: "); for (int num : numbers) { System.out.print(num + " "); } System.out.println(); // Calculate sum of array elements int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } System.out.println("Sum: " + sum); } }
✅ Advantages of Arrays
- Store multiple values efficiently in a single variable
- Fast access to elements using index numbers (O(1) time)
- Memory efficient as elements are stored contiguously
- Useful for iteration, sorting, and searching algorithms
- Ideal for storing fixed-size data sets
⚠️ Limitations of Arrays
- Fixed size - cannot grow or shrink dynamically
- Only store homogeneous (same type) data
- May waste memory if not fully utilized
- Insertion/deletion operations are costly (require shifting)
- No built-in methods for common operations (sorting, searching)
What are Wrapper Classes in Java?
Wrapper Classes in Java provide an object representation of primitive data types. They allow primitive types to be treated as objects, which is necessary when working with Java Collections Framework (like ArrayList, HashMap) that only work with objects.
Each primitive type has a corresponding wrapper class: int → Integer, double → Double, boolean → Boolean, char → Character, etc.
Primitive Types and Their Wrapper Classes
| Primitive Type | Wrapper Class | Example |
|---|---|---|
| byte | Byte | Byte b = 127; |
| short | Short | Short s = 1000; |
| int | Integer | Integer i = 100; |
| long | Long | Long l = 100000L; |
| float | Float | Float f = 3.14f; |
| double | Double | Double d = 3.14159; |
| char | Character | Character c = 'A'; |
| boolean | Boolean | Boolean b = true; |
Wrapper Classes Example with Autoboxing & Unboxing
public class WrapperExample { public static void main(String[] args) { // Manual Boxing (Primitive → Wrapper) int num = 50; Integer wrapperNum = Integer.valueOf(num); // Manual Unboxing (Wrapper → Primitive) int unboxedNum = wrapperNum.intValue(); // Autoboxing (Automatic conversion) Integer autoBoxed = 100; // Java automatically converts int to Integer // Auto-unboxing (Automatic conversion) int autoUnboxed = autoBoxed; // Java automatically converts Integer to int // Wrapper class methods String strNum = "123"; int parsedNum = Integer.parseInt(strNum); // Convert String to int System.out.println("Boxed: " + wrapperNum); System.out.println("Unboxed: " + unboxedNum); System.out.println("Auto-boxed: " + autoBoxed); System.out.println("Parsed from String: " + parsedNum); // Using wrapper in Collections ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); // Autoboxing in action! numbers.add(20); numbers.add(30); System.out.println("ArrayList: " + numbers); } }
Why Use Wrapper Classes?
- Collections Framework: Allow primitives to be used in Java Collections like ArrayList, HashMap, HashSet (which only work with objects)
- Utility Methods: Provide useful methods for data conversion (
parseInt(),toString(),valueOf()) and comparison (compareTo()) - Null Values: Wrapper classes can be null, while primitives cannot (useful for representing missing values)
- Generics Support: Generic types in Java only work with objects, not primitives
- Autoboxing/Unboxing: Automatic conversion between primitives and wrappers makes code cleaner and more intuitive
Best Practices for Arrays & Wrapper Classes
Use arrays for fixed-size, performance-critical data. Use ArrayList (with wrapper classes) when you need dynamic sizing and built-in methods.
Autoboxing/Unboxing has a small performance cost. For large loops or performance-critical code, prefer primitives over wrapper classes.
Wrapper classes can be null, so always check for null before using them to avoid NullPointerException.