r/javahelp • u/[deleted] • Nov 26 '22
Solved Which list creation style is preferred?
List<String> list = new ArrayList<>(); // 1: let the compiler figure it out
List<String> list = new ArrayList<String>(); // 2: Specify the generic twiceAlso, is there a technical term for when the compiler "figures it out"?
15
u/dionthorn this.isAPro=false; this.helping=true; Nov 26 '22
"type inference" is what we call it when you leave the <> empty and let the compiler figure it out.
Java Language Specifications chapter 18 covers the topic:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-18.html
The 'best' way is to follow whatever style guide you are using for the project. For example:
https://google.github.io/styleguide/javaguide.html
If the guide doesn't specify either way is fine, but a lot of people like verbose easy to read code. I'd say it's just preference in this case.
6
5
u/emilbm Nov 26 '22
var list = new ArrayList<String>();
1
Nov 26 '22
Why use
varhere? I think that makes it less clear?3
u/Tobidara96 Nov 26 '22
In what way? You can clearly see it’s a list of strings. Either option 1 from what you said or what that guy said. Option 2 looks like it’s from legacy code.
0
Nov 26 '22
Because it's not clear that 'list' can be any implementation of a List rather than just an ArrayList - if the implementation changes later to a LinkedList, it could effect other parts of the program that expect it to be an ArrayList.
2
u/Tobidara96 Nov 26 '22
But var only works for method variables so I don’t see how that is a problem.
0
Nov 27 '22
var listhas higher coupling thanList<String>right? For the same reason as https://stackoverflow.com/q/147468?1
u/Tobidara96 Nov 27 '22
The keyword ‘var’ is only used in methods so it doesn’t matter if I use the interface or not since I don’t need to hide implementation details from myself. If I then return the list I can mark the return type as list if it doesn’t matter which implementation it is.
15
u/ignotos Nov 26 '22
The <> is often referred to as the "diamond operator".
In my experience it's more common to omit the type, and use the diamond operator where possible. That's because it reduces the need to "repeat yourself".
4
1
-2
•
u/AutoModerator Nov 26 '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
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://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.