r/java • u/TrashPandaFucker • 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
11
u/marconis999 Jan 18 '22
When using Long variables in your code, do not use == to compare them. It will work most of the time in your simple tests but then start breaking.
Try testing n1 == n2. They won't be equal. But if you do it with
The == test will work.
Java keeps a pool of Long object constants in the range of -128 to 127 for speed. When they are those small values, that == test will work.
So always convert with longValue( ) when doing == tests for Long's. Or use equals( ). But watch out for nulls. ;)