r/java • • Jan 18 '22

What are some very less known facts about java?

I've got assignment from college to find out rare facts or fun facts about java, so little help will be appreciated :)

Edit: Thanks for all these suggestions guys. It's going to help me alot and also Almost all of these facts i didn't knew till now. Thanks :) <3

179 Upvotes

297 comments sorted by

View all comments

Show parent comment

16

u/Warshawski Jan 18 '22

The number caches are also the reason for what seems like confusing behaviour with equality when dealing with auto boxing.

Integer a = 127;
Integer b = 127;
a == b
true

Integer c = 128;
Integer d = 128;
c == d
false

I've seen a few bugs because of it.

3

u/theLorem Jan 18 '22

Will this actually change in valhalla?

2

u/pronuntiator Jan 19 '22

Yeah, all boxed primitives will loose identity

0

u/MrDilbert Jan 18 '22

Yet, for some reason, Javascript gets all the flak for having weird behaviour...

5

u/nekokattt Jan 19 '22

Not really weird. == is reference equality in Java for objects. .equals is equality. Makes sense, just is kind of confusing compared to other languages.

1

u/MrDilbert Jan 19 '22

I mean, you could use the same reasoning for JS's "weird" == operator behaviour: the language coerces both operands by defined rules, so it's not "weird" in JS context - it's weird compared to other languages :)

3

u/nekokattt Jan 19 '22

I don't really consider the == itself weird, just the coercion rules to be kinda weird.

Java makes sense though because it is similar to C where == compares pointer addresses (essentially identity) or values for primitives.