Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Package of Existence
3.
Class Hierarchy
4.
Immutability
5.
Declaration
6.
Use
7.
Code Demonstration
7.1.
util.Arrays
7.2.
reflect.Array
8.
FAQs
9.
Conclusion
Last Updated: Mar 27, 2024

util.Arrays vs reflect.Array

Author ANKIT KUMAR
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

The Array class in the java.lang.reflect package is a Java reflection class. The Array class includes static methods that can be used to dynamically create and access Java arrays. This class is final, which means that it cannot be altered or even instantiated. The methods contained within this class can be accessed by using the class name.

The java.lang.reflect.Array class provides various static methods for dynamically creating and accessing Java arrays. This Array class ensures that the array is type-safe.

The Arrays class in the java.util package is a Java Collection Framework component. This class provides static methods for creating and accessing Java arrays dynamically. It only has static methods and methods from the Object class. The methods of this class can be accessed via the class name.

We shall compare util.Arrays and reflect.Array based on various parameters. These are discussed below.

Recommended Topic-  Iteration Statements in Java, and Duck Number in Java.

Package of Existence

The Array class is present in the java.lang.reflect packagewhereas the Arrays class is present in the java.util package.

In order to use the Array class, we must import the java.lang.reflect package. Similarly, in order to use the Arrays class in Java we must import the java.util package.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Class Hierarchy

The Array class is present in the java.lang.reflect package, which is part of the java.lang.Object package. The below image represents the hierarchy of the Array class.

java.lang.Object
↳ java.lang.reflect
   ↳ Class Array

The Arrays class is present in the java.util package, which is part of the java.lang.Object package. The below image represents the hierarchy of the Array class.

java.lang.Object
↳ java.util
   ↳ Class Arrays

Immutability

The Array class is final, which means that it cannot be altered or even instantiated. The methods contained within this class can be accessed by using the class name.

The Array class cannot be extended or inherited therefore, it is said to be immutable.

The reason for the Array class being immutable is that it is declared final.

The java.util.Arrays class, on the other hand, is not immutable.

Also see, Hashcode Method in Java and Swap Function in Java

Declaration

The class declaration of Array is as follows:

public final class Array extends Object

The class declaration of Arrays is as follows:

public final class Arrays extends Object

Both the Array and Arrays class extend the Object class.

Use

We mostly use the Arrays class in the java.util package when we have to manipulate the array. One of the most common use cases of the Arrays class is when we have to search an element in an array or sort the array.

E.g.: Arrays.sort(array_name);

The Array class in the java.lang.reflect package is a Java reflection class. The Array class includes static methods that can be used to dynamically create and access Java arrays.

E.g: int[] reflectArray = (int[])Array.newInstance(

int.class, arraySize);

One of the biggest advantages of the Array class in the java.lang.reflect package is that it keeps the array to be type-safe.

You can also read about the topic of Java Destructor

Check out this array problem - Merge 2 Sorted Arrays

Code Demonstration

util.Arrays

// Program to illustrate java.util.Arrays

import java.util.Arrays;

class CodingNinjas {
    public static void main(String[] args) {

        // create an array
        int arr[] = {145, 876, 34, 123, 90, -12, 76};

        // using the sort function from the util.Arrays class
        Arrays.sort(arr);

        // using the binarySearch function from the util.Arrays
        // it returns the index of the element which is passed
        int idx = Arrays.binarySearch(arr, 34);

        // print the sorted array
        for (int item : arr) {
            System.out.print(item + " ");
        }

        // for new line
        System.out.println();

        // print the index of 34
        System.out.println("index of 34 in the array is: " + idx);
    }
}

Output:

-12 34 76 90 123 145 876 
index of 34 in the array is: 1

reflect.Array

// program to demonstrate reflect.Array class

import java.lang.reflect.Array;

public class CodingNinjas {
    public static void main(String[] args) {


        // we set the size of the array to be 5
        int arraySize = 5;


        // Create an integer array
        // using reflect.Array class
        // This is done using the newInstance() method


        int[] reflectArray = (int[]) Array.newInstance(
                int.class, arraySize);

        // using the setInt() method of the reflect.Array
        Array.setInt(reflectArray, 0, 10);
        Array.setInt(reflectArray, 1, 20);
        Array.setInt(reflectArray, 2, 30);
        Array.setInt(reflectArray, 3, 40);
        Array.setInt(reflectArray, 4, 50);

        // right now the array is empty
        // by default all the values are zero
        for (int item : reflectArray) {
            System.out.print(item + " ");
        }
    }
}

Output:

10 20 30 40 50

 

Practice it on online java compiler.

FAQs

1.Where is the Arrays class present in Java?

It is present in the java.util package.

2. Where is the Array class present in Java?

It is present in the java.lang.reflect package.

3. Why is the Array class immutable?

The reason for Array class being immutable is that it is declared final.

4. Name some commonly used methods of the Array class in the java.lang.reflect package.

Some commonly used methods of the Array class are Array.newInstance(), Array.set(), Array.get()

5. Name some commonly used methods of the Arrays class in the java.util package.

Some commonly used methods of the Arrays class are Arrays.sort(), Arrays.binarySearch().

Conclusion

In this article, we have extensively discussed util.Arrays vs reflect.Array with code demonstration in Java programming language.

  • The Array class in the java.lang.reflect package is a Java reflection class. 
  • The Arrays class in the java.util package is a Java Collection Framework component. 
  • Both the Array and Arrays class extend the Object class.

We hope that this blog has helped you enhance your knowledge regarding the reflection array class in Java and if you would like to learn more, check out our articles here

Recommended problems -

 

Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Reflection Array Class
Next article
Jagged array in Java
Live masterclass