Yes, technically it is "legal" since the compiler will accept it, but it is sloppy coding. Separate the assignment and the return statement to two lines.
The above code is legal if y and z are boolean variables.
y & z do not have to be booleans. This is an assignment operation. This will work as long as z is assignable to y. However (I'll repeat again), even though this works it is sloppy coding.
I certainly agree that it's sloppy coding, and would never perform it in any usable class. I just created the example previously because of the boolean comment.
class test4 { static int a=10; static int amethod() {int z=0; return(a=z);//line L } public static void main(String[] args) { System.out.println("hello" + a + amethod()); }
} result is hello 10 0
and i thought z value is returned as well assigned to a,but a still remains 10. pls xplain.
a isn't set to 0 until amethod() is called; it isn't called until after the value of a is used in the println() statement. Switch the order of a and amethod(), and you'll get 0 0.