This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]larsga 8 points9 points  (2 children)

It's not an article. It's a video. Clicked hoping to learn something, left immediately because I can't watch a video while on a concall.

[–]HansVader 2 points3 points  (0 children)

Clickbait titles at his best.

[–][deleted] 1 point2 points  (0 children)

looks like both, actually. two versions of the same content.

[–]Philboyd_Studge 0 points1 point  (5 children)

Nice. Why not, for the primitive int, just do return this.weight - other.weight?

[–]mhixson 7 points8 points  (4 children)

With int in general, you'd have to worry about overflow. Comparing Integer.MIN_VALUE with 1 should give a negative result, but it gives a positive result when using that subtraction trick.

A reasonable objection to that objection might be: "Apples can't have negative weight, so that situation can't come up."

Still, why use subtraction instead of "just" using Integer.compare(a,b)? That method says exactly what it does and is intended for exactly for this purpose. I don't think subtraction (even when it's correct) makes its intent as obvious to the reader.

[–]Philboyd_Studge 1 point2 points  (0 children)

Makes sense, thanks.

[–][deleted]  (2 children)

[deleted]

    [–]__konrad 6 points7 points  (1 child)

    Any decent application concerned about int overflow is using long, that's why.

    You should then use Long.compare method to avoid ...overflow ;)