r/badcode • • May 15 '18

java == is just too mainstream

Post image
720 Upvotes

32 comments sorted by

239

u/BLourenco May 15 '18

This honestly just looks like a joke, and it's pretty funny.

164

u/Wirespawn May 15 '18

Could be an answer to one of those ass-retarded interviewer questions (“Do X without doing Y”)

59

u/sprechen_deutsch May 15 '18

Or it's intentionally bad code made for this sub.

28

u/redditsoaddicting May 15 '18

Or thinking that == on ints does reference equality.

6

u/Jonno_FTW shameless May 15 '18

Pretty sure equality checks on boxed and unboxed types in java are overloaded. So it doesn't matter either way.

3

u/XplittR May 15 '18

I think I read somewhere that Integer was behaving different for small values compared to bigger values

4

u/TheUnlocked May 15 '18

I believe that's true in some cases, but not ==. It has to do with a range of ints that are cached before they're needed.

2

u/XplittR May 15 '18

2

u/Jonno_FTW shameless May 16 '18

Woops, my bad. Probably the kind of behaviour someone would pull out in an interview to trip you up.

4

u/myusernameisokay May 16 '18

Why not bitset XOR?

2

u/SomeRandomGuydotdot Sep 03 '18

Found the C programmer.

32

u/[deleted] May 15 '18

I'm in love with this.

16

u/pekkhum May 15 '18

Appropriate flair...

4

u/skulgnome May 15 '18

So am I.

1

u/[deleted] Jul 28 '18

happy cake day x

55

u/xhable May 15 '18

Don't see a problem here, this is genius.

6

u/fukitol- May 15 '18

It's clever. Awful, but clever.

9

u/ShamelessC May 15 '18 edited May 15 '18

Bastardizing exception handling this way is A-okay in the python community. For instance, to determine whether something is a float or an int, just try int and catch the ValueError! From the second answer in https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python

def parse_num_str(s):
    try: 
        return int(s) 
    except ValueError: 
        return float(s)

3

u/hexane360 May 15 '18

Except it's not really bastardized "like this". Using try/except to catch natural computational errors is miles away from using the fact that computations fail on certain inputs to verify inputs.

2

u/ShamelessC May 16 '18

Yeah I'm enlightened now to the "ask for forgiveness" mentality mentioned in the other comment. I regret saying "bastardizing". Although, coming from another language, it may feel a bit "wrong" until you understand python better.

10

u/Pure_Awesomeness May 15 '18

Perhaps they discovered a glitch in the matrix and this is 1000x faster!

4

u/TheUnlocked May 15 '18

It's a shame this is Java. I was gonna make a joke about underflows.

3

u/kera2k May 15 '18

If it ain't broken, don't fix it...

-15

u/PM_ME_YOUR_HIGHFIVE May 15 '18

I guess 1 and 0 are equal

28

u/[deleted] May 15 '18

I don't see how 1/(1-0) will throw an ArithmeticException.

26

u/PM_ME_YOUR_HIGHFIVE May 15 '18

yeah, I messed that up

5

u/[deleted] May 15 '18

[removed] — view removed comment

5

u/ericl666 May 15 '18

Java doesn't have operator overloading.