So I have a new job. One where I have to occasionally use java. Horrifying, I know.

Not even 2 weeks in and I have started running into the "wait, what?" moments javascript used to be famous for. I guess I'll document a few of them here.

what to heck, ArrayList.clone()?

Quick quiz.

Given that you have ArrayList<String> someArray, and you were to call .clone() on this, what would you expect the returned type to be?

  • A: ArrayList<String>
  • B: ArrayList<SomeGenericTypeIDK>
  • C: Object

If you answered A, congratulations, you are sane! However, you are wrong. The answer is C. Of course it is.

what to heck, Collections.sort()?

Collections.sort() can only work in place.

I expected to be able to use

ArrayList<String> someArray = Collections.sort(something);

But this is naturally wrong. You must do

ArrayList<String> someArray = something;
Collections.sort(someArray);

What to heck, comparator?

So, strings are comparable. In any other language I am able to do:

>>> "a" < "b"
true

In java? Absolutely not.

"a" < "b"
^
error: bad operand types for binary operator '<'

I have to do

"a".compareTo("b") < 0

why!?