Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1483 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-02-03 17: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?

Offline actual

JGO Coder


Medals: 23



« Reply #1 - Posted 2013-02-03 17: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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-02-03 17: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!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2013-02-03 17: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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2013-02-03 17: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?

Offline actual

JGO Coder


Medals: 23



« Reply #5 - Posted 2013-02-03 17: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
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-02-03 17: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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2013-02-03 17:36:27 »

lol i understand that

Offline Cero
« Reply #8 - Posted 2013-02-03 17: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
« Reply #9 - Posted 2013-02-03 17: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!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #10 - Posted 2013-02-03 17:41:10 »

lol i understand that
why'd you ask the question then...you know server space isn't free
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #11 - Posted 2013-02-03 17: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

JGO Kernel


Medals: 202



« Reply #12 - Posted 2013-02-03 17: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
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #13 - Posted 2013-02-03 17: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 18: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: 56
Projects: 11


Monkey for a head


« Reply #15 - Posted 2013-02-03 18: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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #16 - Posted 2013-02-03 19:17:17 »

Thanks orangy!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22

Longarmx (41 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!