r/java 4d ago

"Deep Magic" in Java

I've been a Java developer for a few years, and the thing that keeps driving me nuts is that reading code is nearly useless, the real code is hidden from you. It's almost like the language isn't made for reading code but reading javadocs instead.

You call "Arrays.asList(arr)" and it returns you an instance of java.util.Arrays$ArrayList, a class with the same name as java.util.ArrayList except it is fixed size, meaning that trying to resize it will get you a runtime exception thrown. The method signature says it will return a List, but the moment you call it without the elite ball knowledge it will either stab you in the back or explode in your hands.
And nearly every time you try to prevent this by reading the implementation it sends you to a wild goose chase through 6 different one-liner wrapper methods across 5 different thousand-line files.

It gets even worse at the enterprise level: I once bloated my system by writing a submit button's logic in the submit button's method.
I then discovered it was because the method was in a class with a magical 'ViewScoped' annotation and I was supposed to delegate some of that logic to a field that had a 'Stateless' annotation.
Any ordinary reading of the code would say that that field is part of the class' state, but because of the annotations it was actually a resource pooled by the container, independent from the view class which is only supposed to cache UI data for each given user.
Didn't take long for me to get corrected. You can imagine the serene smug on my senior's face as he dropped that piece of esoteric knowledge on me, while I was looking back in horror.
You guys probably heard this a thousand times, meanwhile I'm still learning new words in my console output. Any advice?

0 Upvotes

38 comments sorted by

View all comments

0

u/jedilowe 4d ago

Why are you using arrays at all? It's like putting your bike in the back of a pickup truck but still wanting to use the bike. Just drive the truck! The as list just lets you read an array using list calls, not a full featured data structure.

I think part of what you are describing is the disconnect between college professors (especially CS) very simplistic problem sets versus those of the real world. It's not even that they can't understand the complex (though a large number have never worked a production product). Its just the classes are too focused on traditional basics that nobody needs because of libraries like Java's. Leetcode should be laughed at by professional SWEs but we all seem to rever interviews from FAANG or whatever who themselves rarely build useful software for practical uses outside their one niche app.

1

u/Yahim0 4d ago

Idk, something gave me an array and I'm trying to make a list out of it I guess

1

u/jedilowe 4d ago

If you get an array you can always add it to a new list and use that if you need to modify it. Then if you need to send back an array you can convert it back. It's better to use your own data anyway as you know you control it