Un3qual Un3qual - 2 months ago 9
Java Question

Declaring multiple variables on one line out of order

I recently saw this in some Java code:

int pos = -1, ch;


Is that the same as

int pos, ch = -1;


?

Answer

No, the first code snippet only initializes pos with -1 and leaves ch uninitialized. The second one does it the other way round leaving pos uninitialized and ch with the value -1. But in either case, both ch and pos will be created and you will be able to set or update their values.

Comments