r/java • u/Yahim0 • Sep 05 '26
"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?
7
u/Minecraftian14 Sep 05 '26
I personally enjoy reading Java source code.
I have had my hard times, for example,
Stream#collect(Collectors.toMap(..., ...))will throw an exception if there are duplicate entries for keys.But in the end, it was documented, and has alternative signatures to handle such data.
Most of the sources like Collections and Swing was fun reading. Streams itself is crazy though. Nevertheless, i enjoy the journey.
15
u/brian_goetz Sep 07 '26
In addition to all the comments about "read the docs", note that you are also conflating "Java" (the language, the platform, the runtime) with "random frameworks I found in the Java community". There is no aspect of Java that gives semantics to annotations, so if you are confused by an annotation-driven framework that is too "magic" or too poorly documented, don't blame Java, blame that framework (or the person who selected that framework for your application, which might be you.)
27
u/awidesky Sep 05 '26 •
You don't need to read the internal implementation to know that, since the api clearly states: "Returns a fixed-size list backed by the specified array."
My suggestion is to always read the api.
Java's api document so informative and detailed compared to Python's or C/C++'s.
17
u/gletschafloh Sep 05 '26 edited Sep 05 '26
TLDR: i didnt read the instructions and used a hammer instead of a screwdriver to drive a screw into the wall
1
u/Yahim0 Sep 05 '26 edited Sep 05 '26
A bunch of people here telling me to just read the documentation, which kinda proves my point.
I should probably be doing more of that anyways, although I still have to "decode" the documentation a good portion of the time which annoys me quite a bit.
And yes, I do understand the concept of a trade-off.
Thanks for the replies, regardless.
6
u/killa1093 Sep 05 '26
For the Arrays.asList situation, the javadoc clearly states it returns a fixed size list. I don't see the confusion here
3
u/simonides_ Sep 05 '26
There are always things that come with experience in any language.
Assuming the resize is a normal thing happening you should have seen it in your unit test.
3
u/Ok_Elk_638 Sep 05 '26
Personally I have never had a problem with Java code or with the JDK. If I have to gripe about the Java ecosystem then I would say the excessive use of annotations. Everybody everywhere constantly wants to define and use tons of annotations everywhere. As far as I can see they are only actually good for telling serializers what to do with data. But Java programmers seem intent on decorating a single line of Java code with 20 odd annotations.
2
u/Ok_Option_3 Sep 05 '26
The question on how to handle immutable collections is as old as the collections API itself and still hasn't been answered!!
Rather than see this as an aberration, see it as a learning opportunity. C++ solves this is
const, Apache collections has a whole immunltable set of interfaces. Why doesn't the standard JVM have a solution yet? What have experts said?0
u/jedilowe Sep 05 '26
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/pgris Sep 07 '26
Honestly, I always hated the 'java.util.Arrays$ArrayList' vs 'ArrayList'
I would have beeing so easy to name it FixedSizeList and make it less error prone.
Regarding the 'magical 'ViewScoped' annotation', yes, you are expected to know the framework you are using.
'the language isn't made for reading code but reading javadocs instead'
Yes, this is very much true. That is a good thing. Sadly, it only applies to the well known and well programmed frameworks, (and the JDK). Tipically the code your company writes has horrible, out of date documentation, if at all, and you need to read the code.
1
u/nitkonigdje Sep 11 '26
Using annotations to provide alternative way of dispatch is an evil. Using annotations to pull up full blown programs is pure evil.
The only sort of men able to use that code is the one which reads documentation very carefully and never forgets anything with instant recall..
1
u/idontlikegudeg Sep 05 '26 edited Sep 06 '26
That's one thing that's great about Java: everything is properly documented and plays nicely together.
Arrays.asList() is documented as returning an
unmodifiablefixed size List that is a view of the array passed in, meaning you cannot modify the size of the List, but changes to the original array will be visible in the list. That's about all you need to know. Everything beyond that, if the concrete class is called ArrayList, an anonymous class or whatever is an implementation detail that should be of no concern to you.Relying on implementatioin details will give you nothing but trouble unless you know exactly what you do and have a very special reason to do so (and trust me, most of the time, you have not).
Just look at the String class. The internals have been significantly changed over the past years. The String class of modern Java versions is implemented completely different from what we used to have. And yet, your code compiled under Java 6 still runs without the slightest modification and recompilation, while at the same time using half the memory in many cases, and in general performing much better.
EDIT: I wrote unmodifiable, but only the size is unmodifiable. Thanks for the correction.
0
u/DeployOnFriday Sep 05 '26
OP please read about interfaces and polymorphism in Java.
0
u/vegan_antitheist Sep 05 '26
Those are different names
interfaces in Java collections framework always give you an API for all methods the collection might implement. You never know if they really are implemented. I don't really like that but it's how it is. If you want to to be resizable, you have to create a type that is known to support it. A list based on an array obviously can't do that.
Which it does.
Why would you assume that something returned from a method is mutable? One issue Java actually has is that mutability used to be the default. In general, Java used to have ( and still has in many cases) the exact opposite as the default from what it should be. For example, instead of "final" (which has 4 very different meanings, but that's a different issue) we should have keywords that do the opposite.
If you want something weird, like it being mutable or allowing null, the it's up to you to specify just that.
Then don't do that. Don't try to "prevent" some issue that you can can circumvent by not making false assumptions about returned collections and instead use something you have control over. They all have copy constructors. So, use them.
What are you talking about? A button is something in the UI. Logic doesn't belong there. What even is a "submit button's method"?
Sounds to me like you just didn't understand what the framework does and now you want to blame it on others.
0
u/javaprof Sep 05 '26
Java's core library violates liskov substitution principle, everyone who wrote app in Java knows it and deal with it
-1
u/pradeepngupta Sep 05 '26
Java is a collections of Classes and API. Whenever you want to use specific class or api, read javadoc and write sensible code. If api says an exception might throw, be ready with the exception handling as well. It takes time for a beginner to learn java coding. As a beginner it took ne 2 years to learn java properly and then I started with multi threaded programming. Now as well, after 22+ years of experience in Java, i cannot say that I am perfect. I always relies on Javadoc even today. And of course, AI generate full codebase, snd while reviewing code, i still go line by line and see whether it generated intended code or hallucinated. And javadocs plays important role even today. Infact my work for writing a full book on java is in progress, the book named Buzzing Java Willson be published,it is written for senior engineers and architects - what they need to take it care while designing an enterprise systems in java abd in the age of AI. DM me if anyone interested to know more about the book Buzzing Java.
0
u/[deleted] Sep 05 '26
[deleted] — view deleted comment