What will be the output of following code?

class Main {
    public static void main(String[] args) {
        int[] array1 = {1, 2};
        for (long x : array1) {
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.
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback