Knowee
Questions
Features
Study Tools

What is the output of the following Java code?public class ArrayComparison { public static void main(String[] args) { int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; int[] arr3 = arr1; System.out.println(arr1 == arr2); System.out.println(arr2 == arr3); }}

Question

What is the output of the following Java code?public class ArrayComparison { public static void main(String[] args) { int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; int[] arr3 = arr1; System.out.println(arr1 == arr2); System.out.println(arr2 == arr3); }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the Java code will be:

false
true

Here's why:

In Java, when you compare two arrays using the == operator, it doesn't compare the contents of the arrays. Instead, it checks if the two references (arr1, arr2, arr3) point to the same array in memory.

  1. arr1 == arr2 returns false because arr1 and arr2 are two different objects in memory, even though their contents are identical.

  2. arr2 == arr3 returns true because arr3 was assigned to arr1, which means they are pointing to the same object in memory.

This problem has been solved

Similar Questions

What is the output of the following Java code?public class array{public static void main(String args[]){int []arr = {1,2,3,4,5};System.out.println(arr[5]);}}a.5b.4c.ArrayIndexOutOfBoundsExceptiond.InavlidInputException

What is the output of the below Java code snippet? int[] balls = {}; System.out.print(balls.length);

What will be the output of the following Java code? public class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } }

What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}

e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.