r/learnjava • u/[deleted] • Jun 17 '22
i just created my very first java program without following a tutorial
public class Main {
public static void main(String[] args){
String reversable = "it works let's go";
String reversed = "";
for(int i = reversable.length(); i>0; i--){
reversed += reversable.charAt(i - 1);
}
System.out.println(reversed);
}
}i'm happy
13
Jun 17 '22
You even put the opening curly braces on the same line as the class and method declarations. You are in a better spot than I was when I first started.
9
4
3
u/Housy5 Jun 17 '22
Very nice!
The only comment I have is use a StringBuilder instead of += when concatenating a string. This is because every time you call += it will create a completely new String and copy every character to the other String. In other words copying n amount of characters n times or O(n²). Where StringBuilder is basically fancy ArrayList where you can add characters in constant time turning it into O(n). which is significantly faster.
Do not take this as criticism though. You're doing great!
1
u/SushiWithoutSushi Jun 17 '22
Do you know about some place to learn about this type of little optimizations? Something likehttps://java-design-patterns.com/patterns/
I'm about to complete a big project and I'd like to double check for optimizations as it doesn't run particularly fast right now.
StringBuilder is something I didn't know existed and it could probably help me save some time, thanks.
2
u/Zyklonik Jun 18 '22
All you really need, given the constraints that you have mentioned, is profiling. Take your pick - https://www.baeldung.com/java-profilers. See which parts of the codebase as well as control and logic flow are the bottlenecks, fix them. With all due respect, stuff like using
StringBuilderis a micro-optimisation. Don't get me wrong, it's good advice in this context, but for project level optimisations, always profile first.Orthogonal to profiling, you might want to have a look at your application's memory, thread, and CPU performance using something like
JConsoleorjvisualvm. Oftentimes, something as banal as increasing the minimum heap size (https://docs.oracle.com/cd/E15523_01/web.1111/e13814/jvm_tuning.htm#PERFM164) makes a massive difference in performance. In general, Java trades memory usage for performance, so best to keep that in mind.Then choosing the right GC algorithm also makes quite a bit of difference (sometimes massively so) (https://developers.redhat.com/articles/2021/11/02/how-choose-best-java-garbage-collector#serial_collector), but usually, the default GC algorithm works just fine, and profiling (as above) as well as the specifics of your application need to be considered to gather sufficient data to see which GC algorithm might work best.
In the worst case, there are actually options where you can even see the assembly code that would probably be generated by the JIT compiler during runtime (probably because the actual generated assembly may be optimised differently in long-running applications), but 99+% of usecases won't require this.
Edit: If my post seems unfair to Java, then let me clarify - the Java compiler doesn't do almost any optimisation instead offloading it to the JVM.
I know your comment was more about language patterns instead, but the danger there is that the JVM may change underneath you, and so in general it's not really wise to rely on language-level optimisations (in addition to the fact that the ROI on it is much much lesser than what you get by focusing on profiling and tweaking the JVM).
1
u/SushiWithoutSushi Jun 18 '22
Wow, thank you. I wasn't expecting such a complete answer. As someone without a computer science background this seems something quite useful that will take me some time to digest. I'll see if I can study about it after I finish my finals. Thanks again.
2
1
1
1
1
1
ā¢
u/AutoModerator Jun 17 '22
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://imgur.com/a/fgoFFis) 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.