Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Eclipse not detecting if(apples = true) problem  (Read 643 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345
« Posted 2013-02-03 18:03:46 »

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?
Online actual

JGO Coder


Medals: 19



« Reply #1 - Posted 2013-02-03 18:12:06 »

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)
 
Offline wreed12345
« Reply #2 - Posted 2013-02-03 18:15:17 »

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
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Roquen

JGO Ninja


Medals: 66



« Reply #3 - Posted 2013-02-03 18:16:25 »

It's not an error...it's perfectly legal.  The thing is that it should (by default) be showing a warning.
Offline wreed12345
« Reply #4 - Posted 2013-02-03 18:18:28 »

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?
Online actual

JGO Coder


Medals: 19



« Reply #5 - Posted 2013-02-03 18:19:40 »

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.
Offline Jimmt
« Reply #6 - Posted 2013-02-03 18:34:26 »

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

1  
apple == true
or
1  
if(apple)
just checks for whether it is true or not, but does not change the value. Overall, "==" is value of and "=" is assignment.
Offline wreed12345
« Reply #7 - Posted 2013-02-03 18:36:27 »

lol i understand that
Offline Cero
« Reply #8 - Posted 2013-02-03 18:37:57 »

BTW, my version of Eclipse does highlight it as an warning but not an error.

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...


Offline Danny02

JGO Knight


Medals: 36



« Reply #9 - Posted 2013-02-03 18:40:29 »

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.

1  
2  
3  
4  
5  
6  
BufferedReader reader = ...;
String line;
while( (line=reader.nextLine()) != null )
{
  ...do something with the line
}
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Jimmt
« Reply #10 - Posted 2013-02-03 18:41:10 »

lol i understand that
why'd you ask the question then...you know server space isn't free
Offline wreed12345
« Reply #11 - Posted 2013-02-03 18:45:26 »

@Cero and @Danny02 helped answer what i was asking, @jimmt i didnt ask that question lol im not that much of a newb
Offline sproingie
« Reply #12 - Posted 2013-02-03 18:46:59 »

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.
Offline Jimmt
« Reply #13 - Posted 2013-02-03 18:57:58 »

@Cero and @Danny02 helped answer what i was asking, @jimmt i didnt ask that question lol im not that much of a newb
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.
Offline Cero
« Reply #14 - Posted 2013-02-03 19:02:41 »

You might want to look into the FindBugs plugin

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

Offline Orangy Tang

JGO Kernel


Medals: 48
Projects: 11


Monkey for a head


« Reply #15 - Posted 2013-02-03 19:05:34 »

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.

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).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline wreed12345
« Reply #16 - Posted 2013-02-03 20:17:17 »

Thanks orangy!
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (70 views)
2013-05-17 21:29:12

alaslipknot (80 views)
2013-05-16 21:24:48

gouessej (111 views)
2013-05-16 00:53:38

gouessej (106 views)
2013-05-16 00:17:58

theagentd (117 views)
2013-05-15 15:01:13

theagentd (106 views)
2013-05-15 15:00:54

StreetDoggy (150 views)
2013-05-14 15:56:26

kutucuk (174 views)
2013-05-12 17:10:36

kutucuk (171 views)
2013-05-12 15:36:09

UnluckyDevil (180 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.12 seconds with 21 queries.