Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

= and == difference in If statement Java

I am facing something strange here. Please help me understand if I am missing something. My if condition was supposed to be:

if(configuredPdf == true)

But by mistake I had written:

if(configuredPdf = true)

And my Eclipse compiler does not ask me to correct it. Then I assume there is no compile time or checked exception. So:

(configuredPdf = true)

Returns a boolean?

like image 727
Kumar Avatar asked Nov 28 '22 20:11

Kumar


1 Answers

Yes, configuredPdf = true assigns true to your variable and returns true. Therefore if (configuredPdf = true) is a valid syntax even though it's usually a bug.

It's safer to use if (configuredPdf) to avoid this kind of typo.

like image 146
Eran Avatar answered Dec 05 '22 11:12

Eran