Saturday, August 28, 2010

Two different array objects can never be ==, even if their contents are the same

Courtesy of Sun Web Learning Center

Given:

1. import java.util.*;
2. class Pow {
3.   static String [] wow = {"Bamm", "Biff"};
4.   public static void main(String [] yikes) {
5.     if(Arrays.equals(yikes,wow))
6.       System.out.print("got a match?  ");
7.     if(yikes == wow)
8.       System.out.println("sure chief");
9.   }
10. }

And the command line: java Pow Bamm Biff What is the result?


A got a match?
B Compilation fails.
C No output is produced.
D got a match? sure chief
E An exception is thrown at runtime.

A is correct. Two different array objects can never be ==, even if their contents are the same.

No comments:

Post a Comment