Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Final Arrays
3.
Assigning Values to Final Array
4.
FAQs
5.
Conclusion
Last Updated: Mar 27, 2024

Final Arrays

Author ANKIT KUMAR
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In arrays, you must have learned that the arrays in Java are objects. We can use all the methods of the class Object with the array. The concept of final arrays is very simple. However, it is not exactly the same as that of a final variable.

A final reference variable can never be reassigned to refer to a different object.

The data contained within the object, on the other hand, can be changed. As a result, the object's state can be changed but not its reference. Because an array is an object, it is referred to by a reference variable, which, if set to final, cannot be reassigned.

We will learn in detail what we discussed above, and it will make much more sense with the help of code examples.

Also see, Duck Number in Java and Hashcode Method in Java.

Final Arrays

Before we discuss final arrays, consider the following example:

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

        // create an array A[] and assign it 5 values.
        int A[] = {10, 20, 30, 40, 50};

        // create an array B[] and assign it 5 different values
        int B[] = {12, 13, 14, 15, 16};

        // A refers to B.
        // A and B both are object types
        A = B;

        // the values inside A[] are changed
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }

    }
}

Output: 

12
13
14
15
16

Explanation:

  • The original values inside the array A[] was 10,20,30,40,50.
  • The original values inside the array B[] were 12, 13, 14, 15, 16.
  • When we write A= B, it means that the array variable A, which is of a reference to the array Object, now refers to array B.
  • Therefore the contents inside that of A are now the same as that of B.

We saw that everything worked fine, just as we expected. However, consider the below code:

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

        // create a final array A[] and assign it 5 values.
        final int A[] = {10, 20, 30, 40, 50};

        // create an array B[] and assign it 5 different values
        int B[] = {12, 13, 14, 15, 16};

        // A refers to B.
        // A and B both are object types
        // compile time error
        // this gives error "cannot assign a value to final variable A"
        A = B;


        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }

    }
}

Output: 

Main.java:15: error: cannot assign a value to final variable A

Explanation:

  • We create a final array using the keyword final, just as we do in case of any final variable.
  • When we refer A to B, it gives a compile-time error "Main.java:15: error: cannot assign a value to final variable A."
  • It means that a final array cannot refer to any other array or object.

A final array means that the array variable, which is a reference to an object, cannot be changed to refer to anything else. However, the members of the array can be modified. 

Here the final Array variable was A. When we tried to refer it to B, we got a compile-time error. However, if we just remove the final keyword from array A[], everything will work fine, as we saw in the previous example.

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

Assigning Values to Final Array

Now that we have understood the final Array, the question arises can we assign values to the final array, and can we modify the values of the final array?

For any final variable, we cannot change its value at a later stage in the program. Assigning values to the final array is possible. We cannot change the reference of the array variable, but the members of the array can be modified.

In simple terms, if there is an array variable A that refers to the array, it cannot refer to some other array B. However, we can assign values to A or modify the data values of the array A.

Example: Modifying Values in a Final Array

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

        // create a final array A[] and assign it 5 values.
        final int A[] = {10, 20, 30, 40, 50};

        // create an array B[] and assign it 5 different values
        int B[] = {12, 13, 14, 15, 16};

        //A=B; this will give a compile-time error.

        // modifying the value of array A[]. 
        A[1] = 589;
        A[4] = 9876;

        // the output shows the modified values of the array A[].
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }

    }
}

Output: 

10
589
30
40
9876

Example: Assigning Values to a Final Array

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

        //declare the array of size 3
        final int A[] = new int[3];

        // assign values to the array 
        A[0] = 123;
        A[1] = 456;
        A[2] = 789;

        // output the members of the array
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }

    }
}

Output: 

123
456
789

 

Practice it on online java compiler.

Must Read Array of Objects in Java

FAQs

1.What is the final array in Java?

A final array means that the array variable, which is a reference to an object, cannot be changed to refer to anything else. However, the members of the array can be modified.

2. Can we use final in an array?

Yes, we can declare an array as final and still modify or assign values to it.

3. What happens when we try to refer a final array variable to some other array?

It gives a compile-time error.

4. What is the difference between a final variable and a final array?

The final variable's value cannot be modified or reassigned, whereas we can do both of these tasks in a final array.

Conclusion

In this article, we have extensively discussed the final Array in Java along with its code implementation.

  • A final array means that the array variable, which is a reference to an object, cannot be changed to refer to anything else. However, the members of the array can be modified.
  • The final variable's value cannot be modified or reassigned, whereas we can do both of these tasks in a final array.

We hope that this blog has helped you enhance your knowledge regarding final arrays 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
Arrays asList() in Java
Next article
Reflection Array Class
Live masterclass