263

I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have:

String one = "", two = "", three = "" etc...

But I'm looking for something like:

String one,two,three = ""

Is this something that is possible to do in java? Keeping efficiency in mind.

Neuron
  • 5,141
  • 5
  • 38
  • 59
user83643
  • 2,901
  • 2
  • 17
  • 13
  • 7
    _Keep efficiency in mind_ efficiency of what? – khachik Jun 01 '11 at 14:31
  • 24
    Length of code, time to type, visually simple. That's what I mean. I know that reserving memory is reserving memory and that this question is related to the 'human' side of things. – user83643 Jun 01 '11 at 14:33
  • 1
    I'd say that this is generally against accepted Java conventions, it will surprise the reader and IMHO is harder to read than declaring them on separate lines. – Simeon Jun 01 '11 at 14:39
  • 4
    I checked for the performance (on java 8) a = b =c = d =e = true takes 2x+ times than a = true; b = true ; and so on. – Amit Kumar Gupta Aug 13 '15 at 05:34
  • @AmitKumarGupta I tested it on JDK16 and I found no discernible difference using JMH. How did you test? Also once you assign one,two,three something other than a constant it becomes the same (multiple assignment). In fact multiple variable assignment can be leveraged when dealing with multithreaded code since DUP uses the stack. – Adam Gent Jun 02 '21 at 16:52

7 Answers7

385
String one, two, three;
one = two = three = "";

This should work with immutable objects. It doesn't make any sense for mutable objects for example:

Person firstPerson, secondPerson, thirdPerson;
firstPerson = secondPerson = thirdPerson = new Person();

All the variables would be pointing to the same instance. Probably what you would need in that case is:

Person firstPerson = new Person();
Person secondPerson = new Person();
Person thirdPerson = new Person();

Or better yet use an array or a Collection.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
75

You can declare multiple variables, and initialize multiple variables, but not both at the same time:

 String one,two,three;
 one = two = three = "";

However, this kind of thing (especially the multiple assignments) would be frowned upon by most Java developers, who would consider it the opposite of "visually simple".

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
28

No, it's not possible in java.

You can do this way .. But try to avoid it.

String one, two, three;
one = two = three = "";
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
23

Works for primitives and immutable classes like String, Wrapper classes Character, Byte.

int i=0,j=2   
String s1,s2  
s1 = s2 = "java rocks"

For mutable classes

Reference r1 = Reference r2 = Reference r3 = new Object();`  

Three references + one object are created. All references point to the same object and your program will misbehave.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78
  • 4
    I disagree with "All references pointing to same object and your program will misbehave." The only problem is that what happens to the object of one reference happens to the object of another reference -- because it's the same object. This isn't misbehaviour; it's how Java works. – GKFX Jul 20 '14 at 15:04
  • 1
    what do you mean with "misbehave"? It will behave differently then 3 separately initialised objects, but it really depends on what you want.. – Neuron Apr 28 '18 at 00:39
17

You can do this:

String one, two, three = two = one = "";

But these will all point to the same instance. It won't cause problems with final variables or primitive types. This way, you can do everything in one line.

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
  • This is also works with final vars. Ex: public static final String ONE, TWO = ONE = "Blah"; Not only are all vars defined & initialized to the same value, but the split definition & assignment of var ONE doesn't violate the final limitation; it's considered a single expression. – Yurelle Aug 24 '23 at 18:39
2

I do not think that is possible you have to set all the values individualling (like the first example you provided.)

The Second example you gave, will only Initialize the last varuable to "" and not the others.

RMT
  • 7,040
  • 4
  • 25
  • 37
1

Edit: As Zeeen pointed out this will not work in Java. The question I'd intended to answer was in Groovy as well, this was submitted in error.


Way too late to this but the simplest way I've found is:

String foo = bar = baz = "hello"
println(foo)
println(bar)
println(baz)

Output:

hello
hello
hello
starscream_disco_party
  • 2,816
  • 5
  • 26
  • 42