Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 12)
12.
What will be the output of the program?
int x = 3; 
int y = 1; 
if (x = y) /* Line 3 */
{
    System.out.println("x =" + x); 
}
x = 1
x = 3
Compilation fails.
The code runs with no output.
Answer: Option
Explanation:

Line 3 uses an assignment as opposed to comparison. Because of this, the if statement receives an integer value instead of a boolean. And so the compilation fails.

Discussion:
4 comments Page 1 of 1.

Tinny said:   9 years ago
Answer is D because the code runs with no output.

Aniruddh said:   1 decade ago
In C with IF statement, we can perform this assignment operation. Will this rule not work in java ?
(1)

Chitrangad said:   1 decade ago
Here should be use comparisation operator. We can't assign the value within the if () statement.

Balasaheb said:   1 decade ago
== are use to compare to int
.equal are use to compare objects

Post your comments here:

Your comments will be displayed after verification.