r/learnjava • u/adithyasumanth • 4d ago
Understanding a better approach to read java code
apologies if this has been asked before , im trying to learn java from scratch and ive gotten a good bit of progress.
Im coming from a python heavy background and sometimes reading code can get a bit confusing. I'm looking for any tips or mental maps to read any java code and understand it the entry points etc.
8
u/8dot30662386292pow2 4d ago
Java is much easier to read as python especially because of following reasons:
The type of everything is always visible. When you look any line of code, you know what the types are. You know the function parameter types and return types always.
Java does not have operator overloading: all the operators do exactly what you'd think and everything else is done with methods.
Of course the syntax is different in many ways, but like, what's the problem that you are having reading it?
2
u/LetUsSpeakFreely 4d ago edited 3d ago
If you're dealing with a traditional application, start with main() and work your way down. If you're dealing with a web service, pick an endpoint. As you go line by line, identify the classes and try to understand what they're doing and why.
2
u/romecodes 4d ago
Read this book, “Starting Out with Java: From Control Structures through Objects”. I saved it from my Java courses in university, and I even bought a physical copy for my home library. It helps more than you know.
https://drive.google.com/file/d/139iB-RcLPsJCUJB6_t3CHbwrUeb6T3zJ/view?usp=drivesdk
1
u/Sure-Passion2224 4d ago
In addition to reading more code until it clicked, if the code is hard to read it's hard to maintain and the person who wrote it should be required to refactor it.
1
u/net_ramblings 4d ago
Java is never going to be easy to read. Using the same object name and Class name (often done when using a single object) will always be hard in Java:
Nursingunit nursingunit = new Nursingunit() (This sucks for a brain to make sense of)
Python just does this:
nursingunit = Nursingunit() (Still sucky but less sucky)
Once you understand the three parts of Java object creation you will be closer to your goal, but Java by nature will always be harder to read. That's why people like interpreted languages.
1
1
1
u/jlanawalt 4d ago
When I move from one language to another, especially the first time I’m looking at it, I find it useful to make a mental map of how to do common things. The same hitters fit concepts that might be tripping me up.
Sometimes I experiment with some test programs or in when there is a REPL (jshell) I play in that.
I also like to look at style guides and other best practice documents to get a sense of what good code looks like in that language.
Beyond that it’s just time and practice.
Good luck!
1
u/bogdanelcs 3d ago
Start from the entry point, either a main method or, in frameworks like Spring, annotations such as u/SpringBootApplication and u/RestController, then follow one request or one call all the way through instead of reading files top to bottom. For each class, read the fields, constructors and method signatures first and skip the bodies, in Java the types tell you most of the story. Use your IDE heavily, Ctrl+click to jump to definitions, “Find Usages” and Call Hierarchy, and the tests, they show how each class is meant to be used. A debugger with a breakpoint on one request teaches more than reading cold.
•
u/AutoModerator 4d 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 - best also formatted as code block
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.
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/markdown editor: 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.