0

So I was doing review, and came across this question that I'm not very sure about.

Consider the following code segment:

int[] A = {1,2,3};

int[] B = {1,2,3};

int[] C = A;

After this code executes, which of the following expressions would evaluate to true?

I.  A.equals (B)

II. A == B

III. A ==C

I only

II only

III only

I and III only

I, II, and III

I thought it was I only, but one of my classmates said that it was III only.

Could some please explain this?

Thanks for the help.

akash
  • 22,664
  • 11
  • 59
  • 87
user3002761
  • 49
  • 1
  • 3

4 Answers4

1

The answer is in the above linked question by @J L:

Arrays.equals(array1, array2) works as you would expect (i.e. compares content), array1.equals(array2) falls back to Object.equals implementation, which in turn compares identity, and thus better replaced by == (for purists: yes I know about null).

Simply put, you are setting C equal to A, so that's why A == C is true. You performing an Object.equals essentially. While it appears on the surface that A.equals(B) would be true, they are not because of the internals behind Object.equals.

So your friend is right.

Engineer2021
  • 3,288
  • 6
  • 29
  • 51
1

The third has a and c pointing the same object. The others not.

AndreaTaroni86
  • 549
  • 12
  • 27
1

Your friend is correct.

In the code you are saying C = A.

This means that C is the same exact object as A.

In other cases, they are not the same object, although they have the same content.

You might want to have a look at Arrays.deepEquals which returns true (your code is slightly modified):

package arrays;

import java.util.Arrays;

public class ArraysTest {
    public static void main(String[] args) {
      Integer[] A = { 1, 2, 3 };
      Integer[] B = { 1, 2, 3 };
      Integer[] C = A;
      System.out.println("A.equals(B) is " + (A.equals(B)));
      System.out.println("Arrays.deepEquals(A, B) is "
   + (Arrays.deepEquals(A, B)));
      System.out.println("A == B is " + (A == B));
      System.out.println("A == C is " + (A == C));
    }
}

The output will be:

A.equals(B) is false
Arrays.deepEquals(a, b) is true
A == B is false
A == C is true
wavicle
  • 1,284
  • 1
  • 10
  • 12
1

only the third one would be true.

take the first

int[] A = {1,2,3};

witch means A has position 0:1, position 1:2, position 2:3.

then take the second

int[] B = {1,2,3};

witch means B has position 0:1, position 1:2, position 2:3.

so they have the same values in each position but they are two different int arrays

but then take the third

int[] C = A;

their you are saying `int[] C is equal to int[] A,

just like saying

String a = "LOL";
String b = a;

Also just a few notes, Only strings are compared by using .equalsanything else is compared using ==, and always use lower case letters for variables, it makes it easier to code.

you can always test methods with this

if(A.equals(B)
{
    System.out.println("A.equals(B) is true");
}
else
{
    System.out.println("A.equals(B) is false";
}

same for

if(A == B)
{
    System.out.println("A == B is true");
}
else
{
    System.out.println("A == B is false");
}

Hope this helps, Luke.

Luke Melaia
  • 1,470
  • 14
  • 22