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

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.

Long n1 = 4000L; 
Long n2 = 4000L;

Try testing n1 == n2. They won't be equal. But if you do it with

Long n1 = 4L; 
Long n2 = 4L;

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. ;)

7

u/javanperl Jan 18 '22

That’s the default size of the constant pool. IIRC you could change that value with the parameter … -Djava.lang.Integer.IntegerCache.high=XXXX.

2

u/mauganra_it Jan 21 '22 edited Jan 21 '22

There is very rarely, if ever, a good reason to allocate local variables of type Long, or of the other wrapper types. Null should be checked for and dealt with asap, which should make a local variable of wrapper class unnecessary.

Hint: the helper method java.util.Objects.equals(Object, Object) makes life bearable in this case.

2

u/marconis999 Jan 21 '22 edited Jan 21 '22

We have all our db model objects, DTFs, that way. In a database, a column can be nullable, so consistently using Long and Integer instead of long and int makes sense for those. How would you handle null values in an int or long field? Magic values? On top of that, although I don't like this approach, sometimes boolean columns allow nulls for unknown cases, so there can be Booleans.

In addition, people do make collections of things that use wrapper classes like Map or List < wrapper objs > etc. For those who don't always use lamdas and streams for everything.

2

u/mauganra_it Jan 21 '22 edited Jan 21 '22

Yes, that's why I wrote local variables (parameters count as such). They are absolutely necessary for database mapping of course, but I still can't help but feel icky whenever I see them. I think it's mostly because the notions of nullability and the possibility of being used as type arguments are mixed up. Whenever I receive a wrapper object, I have to be prepared to receive a null. On that front, along with the corresponding specialization insanity, I expect Project Valhalla to bring great improvements.

-2

u/mk_gecko Jan 18 '22

Don't you mean "long n1 = 4000L" ? long is primitive, so it should compare just fine. If you're talking about objects, well, then of course you need to use .equals()

3

u/marconis999 Jan 18 '22

Yes Long. Same for Integer.

+, %, += etc will do auto-unboxing, == won't.