Eclipse not detecting if(apples = true) problem

So every once in a while I make a mistake and write if(apples = true){} instead of if (apples == true){} but eclipse doesn’t bring up an error… is there some time when you would actually use that code so it isnt giving an error for that reason? Or is something else going wrong? Also if I were to write this code if(right = true && down = true) a syntax error comes up on down = true but not right = true. This is confusing to me…Any ideas of whats going on?

It will work, but the problem is that instead of comparing apples to true you are setting it to true so the then branch will always run. This is rarely (never?) what you want.

One thing is that you don’t need the == false/true. For instance instead of saying if (apple == true) you should be just saying if (apple) .

Your second issue may be because of operator precedence rules. If you change it to if ( (right = true) && (down = true)) it should work fine. And by fine I mean it should compile. I think is much simpler and safer to write if (right && down)

I understand that i was just curious of why there is no error coming up and why anyone would ever use that if there is meant not to be an error

It’s not an error…it’s perfectly legal. The thing is that it should (by default) be showing a warning.

I wonder why it isn’t… I don’t think i changed any settings or anything… Do you know of anytime where this would be useful?

because apple = true is a statement that returns true which is a boolean value that is allowed to be in an expression. BTW, my version of Eclipse does highlight it as an warning but not an error.

apple = true

sets apple as value of true, whether in the if statement or not.

apple == true

or

if(apple)

just checks for whether it is true or not, but does not change the value. Overall, “==” is value of and “=” is assignment.

lol i understand that

Nah mine doesnt do this either. No warning.

well you are putting a value into a boolean and checking it right thereafter.
So technically you could use it as such, however I would argue that this is very very bad style…

there is a usefull aplication for this.
when you have your normal BufferedReader you use the nextLine() method to read a file. And this method will return null if you reach the end of the file. so now you can do something like this.


BufferedReader reader = ...;
String line;
while( (line=reader.nextLine()) != null )
{
  ...do something with the line
}


why’d you ask the question then…you know server space isn’t free

@Cero and @Danny02 helped answer what i was asking, @jimmt i didnt ask that question lol im not that much of a newb

Jiminey Cricket, people, he’s not asking about the expression itself, he’s asking why eclipse didn’t generate a warning about it. And the answer is, eclipse just isn’t terribly smart about warnings. You might want to look into the FindBugs plugin, or give IntelliJIDEA a try.

I assumed you would infer the answer from my post. Apples = true assigns the value which is usually what you don’t want in an if statement, however it is syntactically legal.

yeah, however I find that indeed eclipse finds almost all things that findbugs does, but it doesnt hurt.
if you really hardcore, try PMD

Eclipse->Preferences. Java->Compiler->Errors/Warnings

Expand ‘Potential programming problems’.

Second option down is 'Assignment has no effect (eg. if (x = y)).

For me, that was set to ‘ignore’ by default, but you can change it to generate an error or a warning. The first thing I do when I setup a new Eclipse install is go through these check and crank most of them into errors. By default Eclipse’s settings are quite permissive, but IMHO there’s some super helpful ones in there (even if they’re not technically errors at times).

Thanks orangy!