TRENDING NEWS

POPULAR NEWS

Java Set And Get Methods And Constructors Syntext

Explain me the syntax in java?

Yes, the syntax is completely fine. However, no one would do this. I believe it was done this way to show various language features, rather than be the best way to do something.

Line 2 is three parts:
- creating a new array of integers: new int[]
- filling that array with three values: 1, 2, and 3.

Together, these two creates an array of three integers, with the values of 1, 2, and 3.

Since all arrays are Objects, an Object reference can be set to an array. Therefore:

- the variable "obj" of type "Object" is set to point to the new array.

This line demonstrates: the creation of a new array, array initialization, and assignment to a supertype (Object is a supertype of int[]).

Line 3:
This line demonstrates that even though "obj" is of type Object, the value it points to is still an integer array. Therefore, you can use a cast to refer to what "obj" points as an int[], then assign that value to "someArray".

This line demonstrates: Casting and that object references might point be a subclass of its own type.


In reality, lines 2 and 3 would be combined into:
int[] someArray = new int[] { 1, 2, 3 };

The use of "obj" is completely superfluous.

TRENDING NEWS