I found a curious SCJP question that at first looks as if it was answered correctly:
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(606,true, 630,true);
s.add(629);
s.add(630);
System.out.println(s + " " + subs);
I shared the opinion with the others that this would print [606, 608, 610, 612, 629, 630] [606, 630]. I tried compiling it and to my surprise found out that the code gives a compile error with a warning:
__[unchecked] unckecked conversion__
found: java.util.TreeSet
required: java.util.TreeSet(java.lang.Integer)
It does compile if I cast the s.subset like this:
subs = (TreeSet<Integer>)s.subSet(606,true, 630,true);
I found various examples similar to this on the net and no one says that this code doesn't compile. I am confused regarding the correct answer here
I'm compiling it with the compiler 1.6 > so it shouldn't be compiler version issue
EDIT: The short answer is YES, IT COMPILES and long one is: read the accepted answer :)