While working with Kent Becks Book TDD by Example, I encountered some Java Code I did not understand.
public boolean equals(Object object) {
Dollar dollar= (Dollar) object;
return amount == dollar.amount;
}
Could someone please explain to me what the parenthesis in Dollar dollar= (Dollar) object; mean?
It’s an explicit typecast. Basically it’s saying that “although ‘object’ was declared with type
Object, I know that it’s actually of typeDollarso it’s okay to assign it to the variable ‘dollar'”.Without the brackets (actually, those are parenthesis, brackets look like
[]or<>depending if they are “square brackets” or “angle brackets”, respectively), the compiler would report an error on that line.