-1

I'm currently reading Java A Beginners Guide and in certain points of the book there is a piece of code that confuses me but I can't find an explanation of what it does.

int a;
int b;
a = b = 0; //This is the line I don't understand.

What I do understand is that the value of 0 is copied into b and then b is copied into a but I don't understand what the point of this would be. Another example would be:

Queue(int size) {
   char q[];
   putloc = getloc = 0;
}

My question is, what is the point of this piece of code if you're trying to create a first in first out queue or line using an array?

  • 2
    It's just a more convoluted way of doing two assignments. It's particularly odd in your first case, where it would be more idiomatic to initialize each variable at the point of declaration. – Jon Skeet Oct 15 '15 at 04:12
  • If I were to hazard a guess, I'd say it's trying to demonstrate that assignment is an expression and returns a value? – merlin2011 Oct 15 '15 at 04:14
  • Possible duplicate of [Readability a=b=c or a=c; b=c;?](http://stackoverflow.com/questions/5373171/readability-a-b-c-or-a-c-b-c) – YoungHobbit Oct 15 '15 at 04:15
  • @YoungHobbit My question is not a duplicate. I ask what the purpose is of the line of code while his question is in reference to readability of his code. I would not be able to understand his question or the answers given to him if I haven't read the answers to my question first. – Richard Rosa Oct 15 '15 at 04:23
  • @RichardRosa, you should mark one of the answer, if they are helpful to you. – Harshit Oct 15 '15 at 04:50

2 Answers2

2

It's just a shortcut that does exactly the same as this:

a = 0;
b = 0;

Why a = b = 0; works? because the assignment operation is an expression that associates from right to left, so b = 0 executes first, assigning 0 to b and then the value in b is assigned to a, like this: a = b. Being explicit with the association order, this is what's happening:

(a = (b = 0));
   ^    ^
   |    |
   |    Executes first
   Executes second

And why would you do this? well, to make it explicit that both variables have the same value (and save a few keystrokes), but sacrificing some readability in the process. I think it's more clear to declare and assign each variable in a separate line, even if they have the same initial value:

int a = 0;
int b = 0;
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • OP is asking about what is the reason of doing this rather than simply assign the values in two different lines. – Harshit Oct 15 '15 at 04:25
1

My personal preference is a=b=c=d for the following reasons:

  • It is concise, saves lines
  • It conveys the concept that (a/b/c/d) are initialized to the same thing, that they are related
    However, caveat:

Don't do that if a/b/c/d are not related (and just happens to be initialized to 1). You'll reduce the readability of your code. Example:

     a=c=1;  // Foo-function related

    b=d=1;  // Bar-function related

Chaining assignments like this reduces the flexibility for you in the future to assign different initial values to the variables -- because then you'll have to break them up again.

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80