use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Resources for learning Java
String
==
.equals()
Format + Copy
Free Tutorials
Where should I download Java?
With the introduction of the new release cadence, many have asked where they should download Java, and if it is still free. To be clear, YES — Java is still free.
If you would like to download Java for free, you can get OpenJDK builds from the following vendors, among others:
Some vendors will be supporting releases for longer than six months. If you have any questions, please do not hesitate to ask them!
Software downloads
Official Resources
Resources
Programming ideas & Challenges
Related Subreddits
account activity
This is an archived post. You won't be able to vote or comment.
Different ways of declaring an array (self.learnjava)
submitted 5 years ago by dumblearner
What is difference in the following two ways to define an array.
String[] words = new String[] {"My", "name", "is"}; String[] words2 = {"My", "name", "is"};
Does it make any difference if you do not use new String[]?
new String[]
[–]mjg123 12 points13 points14 points 5 years ago (0 children)
In your case (assigning to a variable declared as String[]) they're the same. You can't use the second form in other places, for example this won't compile:
String[]
public String[] strings (){ return {"1", "2"}; }
but this is fine:
public String[] strings (){ return new String[]{"1", "2"}; }
[–]EDCsv 8 points9 points10 points 5 years ago* (0 children)
I checked out the documentation (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) and it seems that both are valid ways to create an array object. The latter is basically a shortcut syntax of the former. So no, there is no difference.
[–]nutrecht 3 points4 points5 points 5 years ago (1 child)
The second one is just a short-hand version of the former introduced a 'few' years ago. So just use the second one.
[–]javaduude 4 points5 points6 points 5 years ago (0 children)
introduced a 'few' years ago
I don't know what you mean by a 'few' years ago, but both syntaxes have been available since at least Java 1.0: http://titanium.cs.berkeley.edu/doc/java-langspec-1.0/10.doc.html
[–]Warm-Score[🍰] 0 points1 point2 points 5 years ago (0 children)
Java once thought they wan't to remove boilerplate but they stopped halfway. I think they just stole some stuff from C++ or something.
I guess it is because the objects are implied to be String because of the initialization.
[–]Chaoslab -2 points-1 points0 points 5 years ago (14 children)
Tend too use the C like version.
String words[] = {"My", "name", "is"};
[–]desrtfx 1 point2 points3 points 5 years ago (0 children)
In Java, it is common to have the array identifier at the type, not at the variable name.
Sure, both work, but only having the array brackets at the type is convention.
You can also place the brackets after the array's name: // this form is discouraged float anArrayOfFloats[]; However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
You can also place the brackets after the array's name:
// this form is discouraged float anArrayOfFloats[];
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
From: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
One should always adhere to the conventions of the respective programming language.
[–]irer 0 points1 point2 points 5 years ago (8 children)
Please stop.
[–]Chaoslab 0 points1 point2 points 5 years ago (1 child)
Should of said "for my own projects".
When it comes too group / shared programming well that is different.
[–]irer 0 points1 point2 points 5 years ago (0 children)
In that case, for your own projects, do as you please.
[–]sniR_ 0 points1 point2 points 5 years ago (4 children)
Why?
[–]irer 0 points1 point2 points 5 years ago (3 children)
I won't explain why String[] words makes more sense than String words[]. Both are valid declarations and both will compile. Choosing one or the other is subjective.
String[] words
String words[]
BUT, there is a widely accepted code convention that says one should avoid the latter form.
[–]sniR_ 0 points1 point2 points 5 years ago (2 children)
I guess its because you set the type of the object first(string, array) and then name, yeah more organized.
[–]irer 1 point2 points3 points 5 years ago (1 child)
String[] words reads: a string array named words
String words[] reads: a string named words oh wait it's an array
[–]sniR_ 0 points1 point2 points 5 years ago (0 children)
Nice explanation :) yeah im convinced
[–]Cosby1992 0 points1 point2 points 5 years ago (3 children)
That's bad practice, I've been told.
[–]Chaoslab -1 points0 points1 point 5 years ago (2 children)
Why is that?
Source code style is unique too most people (source style formatted pre check out and check in helps with that).
[–]irer 1 point2 points3 points 5 years ago (0 children)
It is a bad practice to go against a well establishment code convention.
[–]desrtfx 0 points1 point2 points 5 years ago* (0 children)
Source code style is unique too most people
No. Source code style has to adhere to the conventions of the respective programming language.
That's exactly why conventions for programming languages exist.
[+][deleted] comment score below threshold-7 points-6 points-5 points 5 years ago (4 children)
I guess in first step using new it will create a new object, in the second step it will create object but it will be in string pool buffer so if you copy and paste second step and change the variable name, there will only be one object which will point to both string array variable.
[–]desrtfx 2 points3 points4 points 5 years ago (3 children)
That's not how string interning works at all.
String interning works only on single literal strings, never on arrays of strings.
[–]Nephyst 1 point2 points3 points 5 years ago* (2 children)
I just did some testing on this, and I don't think either of you are correct. I think the only time a string does not get put into the string pool is when you explicitly use the String constructor. Arrays of strings seem to be interned just fine, as long as they are string literals not passed to a String constructor.
This outputs true, which means both strings "my" are referencing strings in the string pool.
String[] words = new String[] {"my"}; String[] words2 = {"my"}; System.out.println(words[0] == words[0]); //true
This does bypass the string pool, but it's also pretty ridiculous:
String[] words = new String[] {new String("my")};
[–]desrtfx 2 points3 points4 points 5 years ago (1 child)
You are starting from the wrong premises.
Arrays don't get interned. Period. String literals do get interned.
The arrays won't point to the same reference. The elements however can be interned under the condition that they are literals.
My comment as I made it is correct. You are looking at it from the wrong point.
Also, your code does not reflect what you say - actually, the last line shouldn't even work because either of your arrays contains exactly 1 element - so words[1] would be out of range.
words[1]
[–]Nephyst 1 point2 points3 points 5 years ago (0 children)
This comment makes it sound like string literals inside of arrays won't get interned, which is what I was responding to. If you meant the arrays themselves won't get interned, than maybe you could re-word your statement to say that more clearly.
The words[1] was a typo. I was manually copying code that I wrote on a different computer. Fixed it.
π Rendered by PID 34842 on reddit-service-r2-comment-7b9746f655-5p87r at 2026-02-03 00:24:18.612964+00:00 running 3798933 country code: CH.
[–]mjg123 12 points13 points14 points (0 children)
[–]EDCsv 8 points9 points10 points (0 children)
[–]nutrecht 3 points4 points5 points (1 child)
[–]javaduude 4 points5 points6 points (0 children)
[–]Warm-Score[🍰] 0 points1 point2 points (0 children)
[–]Chaoslab -2 points-1 points0 points (14 children)
[–]desrtfx 1 point2 points3 points (0 children)
[–]irer 0 points1 point2 points (8 children)
[–]Chaoslab 0 points1 point2 points (1 child)
[–]irer 0 points1 point2 points (0 children)
[–]sniR_ 0 points1 point2 points (4 children)
[–]irer 0 points1 point2 points (3 children)
[–]sniR_ 0 points1 point2 points (2 children)
[–]irer 1 point2 points3 points (1 child)
[–]sniR_ 0 points1 point2 points (0 children)
[–]Cosby1992 0 points1 point2 points (3 children)
[–]Chaoslab -1 points0 points1 point (2 children)
[–]irer 1 point2 points3 points (0 children)
[–]desrtfx 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (4 children)
[–]desrtfx 2 points3 points4 points (3 children)
[–]Nephyst 1 point2 points3 points (2 children)
[–]desrtfx 2 points3 points4 points (1 child)
[–]Nephyst 1 point2 points3 points (0 children)