Java – why does the following code compile without errors?
•
Java
I am new to Java and try to understand the following The length of the array is different The code still executes without any errors I don't understand why If anyone can clarify
public class Practice { public static void main(String[] args){ int [][] a = {{1,2,3},{4,5}}; a[0] = a[1]; } }
Solution
A [0] and [1] are both int arrays (that is, their type is int []), so one can be assigned to another, regardless of the length of the current array they refer to
Your code is not much different from the following code:
int [] a = {1,3}; int [] b = {4,5} a = b;
Or from this Code:
Object a = ... Object b = ... a = b;
In both cases (as in the original code), you are changing the value of the reference type variable to reference other objects
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码