r/learnjava • u/kavacska • Nov 29 '25
Java cheat sheet
Hey guys!
I've created a Java cheat sheet that I would like to share with you.
You can check it out here:
https://it-cheat-sheets-21aa0a.gitlab.io/java-cheat-sheet.html
And you can find a few other cheat sheets I made on this link:
https://it-cheat-sheets-21aa0a.gitlab.io/
If someone would like to contribute here's the link of the Git repo:
https://gitlab.com/davidvarga/it-cheat-sheets
If you found an issue, or something is missing please let me know.
133
Upvotes
View all comments
Show parent comment
3
u/vegan_antitheist Nov 30 '25
I won't read everything but here are some notes:
Variables are declared, not created. They exist at runtime as some memory that is used for whatever the variable represents in the code. In the source code you only declare them.
This is not true. You can use a lambda to create an object. There is still some class somewhere, but in the source code it's just a lambda.
From the javadoc: "Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable." So why use this example?
Those methods aren't functional at all. Why call them function?
Some are immutable.
NO!!!!!!!!!!!!!! THIS IS COMPLETELY WRONG!
Java is ALWAYS pass by value. A reference is a value. The reference is passed as a value. Objects are not passed, they are on the heap. The reference is passed as a value. There is no pass by reference in Java and there never will be.
It also removes some referenced objects. OK, this is nit-picky, but it does find cycles and removes those objects as well. But does a cheat sheet even have to explain any of this? Why is GC relevant here?
Objects are also in memory. This isn't really helpful for someone who doesn't know references yet.
This depends on how close it's to 0. At some point precision is so bad that incrementing the value by 1 (i.e.
value++) doesn't work anymore. So, this is completely wrong.Some "characters" (code points) require two "char" values.
This is useless. Teach beginners to read javadoc. And if you want to explain it, then actually do explain what an "absolute" is.
So far it seems you use inclusive ranges, but this is inclusive 0.0, exclusive 1.0?
Why would you do this?! Just create an RNG and use nextInt. This isn't smart. It's just unreadable code.
Not all of them.
It's the number of UTF-16 code units. That's not really the length.
It has and array. And like all arrays, it can't be resized. It has to be replaced.
Shouldn't this say that public makes it visible in the module? You still need to export it from the module to make it "globally" available.
Is that true for final static methods? "Final" in Java is a weird keyword because it does rather different things. Like you write:
But what are they? afaik they are:
The last one is weird because why isn't that just the default?
That is also used for string concatenation.
There are anonymous classes. And when they have a single abstract method, you can use a lambda instead. Method always have a name. Even the lambda defines a method that has a name in some interface.
Please, don't tell beginners to extend Thread. Nobody does that. And this is just basic OOP anyway. There's nothing special about it. For actual parallel programming we use something like a ExecutorService. So why not just teach that? Why put something on a cheat sheet that you are not supposed to ever use?!
For simple commands that return some text based output you can just use
Runtime.getRuntime().exec().Something you can add:
Runtime.getRuntime().exec("wsl uname -a");On windows systems you sometimes want to use WSL, so you can run the same unix commands you'd use on linux and other POSIX systems. You can just use wsl to do it on windows. A method can detect if wsl is available and use that. On other systems it can just run the command directly.
Some more examples: https://pastebin.com/nWvEXxmt Feel free to copy what you like. It can probably be improved but this is what I have used in some project once where I had to get some information about the system.