I'm not used to throwing a primitive data type into an object. I saw some code:
public static int CompareAges(Person p1, Person p2) { Integer age1 = p1.getAge(); return age1.compareTo(p2.getAge()); }
The implementation of age1
seemed extraneous, so I tried to write code as:
public static int CompareAges(Person p1, Person p2) { return p1.getAge().compareTo(p2.getAge()); }
But this caused a compiler error because p1.getAge()
is a primitive int
data type, not an Integer
, which is an object.
Intuitively, I did:
public static int CompareAges(Person p1, Person p2) { return ((Integer) p1.getAge()).compareTo(p2.getAge()); }
and it worked!
Question: What did I miss? Since when did we start casting primitives as value types ?
source share