3

The question is simple, what is the "technical" name (if it has one) of this kind of initialization of variables:

Random rn = new Random();
int a,b;
while((a = rn.nextInt(10)) != (b = rn.nextInt(10))) {
    System.out.println("The different random numbers are a: " + a + ", " + b);
}

What I want to know is the name for the initialization of variables within parenthesis which is allowed pretty much everywhere where the type of the assigned variable matches with what its required by the language/compiler ( (a = rn.nextInt(10)) )?

The second question would be why does java allows that, is it just for "comfort"?, or is it there a deeper purpose rather than allowing the programmer to create more "one-liner" instructions?

P.S: I searched for things like inline initialization or things like that but I was not able to find anything related.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Ordiel
  • 2,442
  • 3
  • 36
  • 52
  • 1
    Its called `assignment` not `initialization`. And it is used just for that purpose you see above. Because if you were to call `(rn.nextInt(10) != rn.nextInt(10))` then you don't have those values saved and you most likely can't duplicate those values. Maybe if you are lucky. – 3kings Nov 09 '15 at 20:16
  • @3kings `a = rn.nextInt(10)` is initialization of the variable. – Evan LaHurd Nov 09 '15 at 20:18
  • `a` and `b` are primitives, it does not matter. And since java is pass-by-value, it would not matter even in case of object instances. As long as the types match at least. – The Law Nov 09 '15 at 20:19
  • @JackmeriusTacktheritrix isn't `int a,b;` the initialization of the variable, and `a = rn.nextInt(10)` the assignment of `a`? – phflack Nov 09 '15 at 20:19
  • @JackmeriusTacktheritrix I think you should read up on that.. I'm quite sure it isn't. – 3kings Nov 09 '15 at 20:20
  • `int a,b;` is variable declaration. `a = rn.nextInt(10);` would be the initialization of the variable. – Evan LaHurd Nov 09 '15 at 20:20
  • @JackmeriusTacktheritrix http://stackoverflow.com/questions/2614072/java-define-terms-initialization-declaration-and-assignment – 3kings Nov 09 '15 at 20:20
  • 1
    [Cross-site duplicate](http://programmers.stackexchange.com/questions/228851/what-is-the-benefit-of-having-the-assignment-operator-return-a-value) – Sotirios Delimanolis Nov 09 '15 at 20:21
  • @3kings my bad I meant assignment :S – Ordiel Nov 09 '15 at 20:22
  • 2
    There's no special name for it, it's just an assignment expression. The result of an assignment expression is itself a value. – Sotirios Delimanolis Nov 09 '15 at 20:23
  • @TheLaw I know it is allowed even for objects, what I want to know is why does java allows that kind of assignments, is it just to encourage our laziness, or is there a deeper reason rather than that? (besides what is the name for that?) – Ordiel Nov 09 '15 at 20:24
  • It's useful and there's not much reason for it to return void. What else would it return, a boolean for if it set it successfully? Personally I prefer it returning what it was set to – phflack Nov 09 '15 at 20:26
  • @Ordiel I believe it has to do with how java evaluates conditional statements. First the left hand of the statement is evaluated (and if there is also initialization, then it is inititialized) and only then the right hand of the argument is evaluated and also initialize. So basically java does not evaluate in one step, but rather three steps. Get value on left, get value on right, and then compare. But why is that nobody knows, and yes, initializing or re-assigning variables like this is lazy and bad design. – The Law Nov 09 '15 at 20:28
  • @3kings Well I guess in his example, it could be assigning the value multiple times...but wouldn't the first time be the initialization of the variables? – Evan LaHurd Nov 09 '15 at 20:28
  • @JackmeriusTacktheritrix The way java handles variables and the fact that is is pass-by-value (or rather pass-by-copy) means that basically the variable is "virtually" re-initialized each time new assignment happens. – The Law Nov 09 '15 at 20:29
  • @Paul [Is this better?](http://stackoverflow.com/questions/16148580/assign-variable-value-inside-if-statement) – Sotirios Delimanolis Nov 09 '15 at 20:29
  • @SotiriosDelimanolis There was nothing wrong with the original duplicate. You can close it. I only intended to vote to reopen as I was in the middle of writing a long answer, but you are right it is a duplicate. I only got the gold badge yesterday so I didn't realise my vote would reopen in one step. I'm not sure I like this power. BTW how did you know it was me? – Paul Boddington Nov 09 '15 at 20:32
  • @PaulBoddington You're allowed to re-close. I knew it was you by looking at the edit history/revisions. (You're allowed one vote to close and one vote to reopen for any question. So you can re-open then re-close or close and re-open.) – Sotirios Delimanolis Nov 09 '15 at 20:34
  • @TheLaw I understand, I'm just referring to the link that @3kings pointed me to. The accepted answer says that initialization is the first assignment of a variable (after the default value is assigned...0 in the case of `a` and `b` here). In the OP's example, wouldn't the first iteration of the while loop "initialize" `a` and `b`, while subsequent iterations would perform "assignments"? – Evan LaHurd Nov 09 '15 at 20:36
  • 1
    @JackmeriusTacktheritrix Yes and no. It is complicated. In case of primitives like int, it is straightforward and it easy to say every subsequent iteration after initialization is just assignment. But consider you have super class A, that has childs B and C that override its methods. If you assign B to A and then C to A during each iteration, is A still the same object or is A different object? It operates on same data, has same memory footprint, so is it still the same object or was it "re-initialized"?. But those are really technical details for long discussion – The Law Nov 09 '15 at 20:42
  • @TheLaw *Every* language evaluates one operand then the other and then applies the operator. How else could it possibly work? And what does it have to do with conditional statements? – user207421 Nov 09 '15 at 20:58

0 Answers0