What will be the output of following code?

class Main {
    public static void main(String[] args) {
        int[] array1 = {1, 2};
        for (long x : array1) {
            System.out.println(x);
        }
    }
}
Explanation
In this case array has int elements which are casted implicitly, as there is no precision lost with the cast and there is no compilation exception. The loop prints values in order.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback