r/javahelp • u/Full_School_7230 • 6d ago
How do developers study Java src code ?
Hey everyone,
I’m currently learning Java to build projects and grind Data Structures & Algorithms (DSA), but I am hitting a massive wall with how the code is structured.
Today, I was trying to implement a simple ServerSocket. To understand what was happening, I instinctively Ctrl+Clicked on it. The IDE immediately unzipped the internal source code and dragged me down a never-ending rabbit hole: from ServerSocket, to SocketImpl, all the way into NioSocketImpl and hidden factory classes.
It throws hundreds of lines of complex internal implementation code at me. It is incredibly time-consuming, frustrating, and completely breaks my momentum. I feel like I'm getting sucked into a black hole of code that I didn't even write, and if I keep this up, I am going to get stuck and fail to actually build anything.
I need a serious reality check and some practical advice:
How do developers actually read and reason about Java without constantly opening up the internal source files?
What is the fastest, cleanest way to look up how to use a class/method without getting bogged down by its underlying implementation code?
How do I force myself to treat things like ServerSocket as "black boxes" so I can just focus on building my app and solving DSA patterns?
Any strategies to help me break this habit and speed up my learning would be deeply appreciated. Thank you!
18
u/khooke Extreme Brewer 6d ago
You need to accept that it’s s ok to use an api or a library without needing to know how it works. Read the docs to understand what you need to use/invoke it … and that’s it.
Think about everything around you that you use on a daily basis without needing to know how it works - e.g. you don’t need to know how an internal combustion engine works to drive a car.
3
u/Full_School_7230 6d ago
And how to search for docs sorry to ask I am a completely dumb newbie.
6
u/khooke Extreme Brewer 6d ago
JavaDocs are always a good place to start, but depending on what you're using (api / 3rd party library etc), the corresponding docs for that api / library https://docs.oracle.com/en/java/javase/25/docs/api/index.html
5
2
u/Minecraftian14 6d ago
If you're on a jetbrains IDE, press ctrl+q.
Either it shows the docs right away, or asks you if you want to download them (configure in settings to always download).Otherwise, googling for the docs and looking at usage examples always help a lot.
1
u/GermanBlackbot 6d ago
Most library JARs have a corresponding javadoc-JAR that your setup should automatically download, either by default or whenever you reach a point where it wants to display the Javadoc.
You can of course also read the javadoc in a browser because most librarys upload their javadoc to...somewhere. It's not mandatory, but all the big ones (apache-commons etc.) do.
1
u/BanaTibor 6d ago
In intellij you can tell the ide to also download the javadoc. There is a keyboard shortcut for quick doc. Also what others said, read the documentation. It is baffling how people just do not think about reading the doc anymore.
3
u/GermanBlackbot 6d ago
To understand what was happening, I instinctively Ctrl+Clicked on it.
That's the beauty: If you want to understand what's happening, this is a way to do it. If you have the corresponding -sources JAR your IDE will usually use this, but in a pinch it can use the .class files themselves and decompile them, though some details might geht lost along the way this way.
But that's if you want to understand what's happening. Usually you don't need to understand what's happening in detail.
1
u/Full_School_7230 6d ago edited 6d ago
What are things that I should ask? The must know things. Suppose, I get to know about a class name now read it's methods and it's working or read docs or or src code.
3
u/GermanBlackbot 6d ago
You should ask yourself: "What does this class or method do?"
To understand this, you read the javadoc.You rarely need to ask yourself: "How does this class do this?"
Because unless the javadoc specifically mentions it, it simply doesn't matter to you. Sure, if you're curious you can dive in, but I don't need to understand how anArrayListor aHashMapdoes things under the hood. All I need to know is written down in the Javadoc. If an implementation detail is relevant it will be mentioned in the doc.1
6
5
u/vegan_antitheist 6d ago
That's heavily optimised code. You probably won't write code like that. Most companies want you to write code that is easy to understand and maintain. JRE code is not like that. The JVM might even run something else. What you see is the implementation used by your own JRE. But when you ship it, it could be something else. You should only read the interfaces. They are the abstraction you use. It's a contract that usually doesn't change over time. If you code it to work with the actual implementation you might do something that can break with a new release.
4
u/logperf 6d ago
Look up the java platform documentation (javadocs). It contains a description for each method, each class and each parameter.
For a high level overview there are plenty of examples in the internet to show how to use those classes. AI can also generate minimalistic examples for you to learn from.
Reading the source is usually not required and I would even advise against it because you might get stuck with the implementation details of a particular version. The javadocs are supposed to say only what will remain true even if the implementation changes in the future.
2
u/Available_Safe1818 5d ago
Read the api docs.
> How do I force myself to treat things like ServerSocket as "black boxes" so I can just focus on building my app and solving DSA patterns?
You know what you have to do, so you act like an adult and you do it.
1
u/RobertDeveloper 6d ago
what do you care what code is behind something? you can only call the interface it exposes anyway, so only focus on that.
1
u/Cyberkender_ 6d ago
Additionally to the documentation that was mentioned it's useful read about how sockets connection work: open connection, r/w connection, listen connection, etc.. Once you know how it works, implement or make use of the needed clases it's almost a trivial question. I did it this way, it worked for me to use server sockets in C/C++, Pascal/Delphi, Java...
1
u/lajete 5h ago
At any given time, I don't even know all the details of the application that I've worked on for 5 years (and that my team has been developing for almost 15 years). Let alone the libraries we use, APIs we integrate with, etc. Unless there is a problem (or you're doing a security audit), all that matters is the interface, not the implementation.
One strategy you can use when integrating a new third-party component, is writing automated tests for it. Not in the same was as you would for your own code, because presumably the third-party component is properly tested by its developers. But to document and validate your assumptions about how it works (and thus also automatically detect if those assumptions ever become outdated when your update to a new version).
1
u/stuhlmann 2h ago
Ctrl+Clicking is a very good habit in general. Keep it up and you will get better at knowing when it's leading you down a rabbit hole or not.
•
u/AutoModerator 6d ago
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.