• Post Reply Bookmark Topic Watch Topic
  • New Topic

if, else if, else trouble  RSS feed

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to compile this program and i keep receiving an error. I can't figure out what the problem is, perhaps I need another pair of eyes to look at it.
Anyways here is my code:



And here is the error message i get when i compile it via command prompt:


I've declared the variable num as an integer, why is it asking for boolean?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you know the difference between '=' and '==' in java? Note: java is different from C (if you are familiar with that language - if not, don't worry about it).
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, i completely forgot about this. The program works properly now.

Just to clarify, in Java "==" means true equality, where as "=" is simply assignment?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazar Buko wrote:Right, i completely forgot about this. The program works properly now.

Just to clarify, in Java "==" means true equality, where as "=" is simply assignment?


Yes.
As for "==", it is mostly used to test equality between primitive types (e.g. int, char). equals() is used when testing the equality of objects (e.g. Strings and mostly everything). When used between objects, "==" only returns true if the left and right refer to the exact same object. The following would print false then true:

 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:


I see, the first one would be false since x and y are different instances of the String object. Where as x.equals(y) simply means, x and y have the same values.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nazar Buko wrote:
I see, the first one would be false since x and y are different instances of the String object. Where as x.equals(y) simply means, x and y have the same values.

Correct.
 
John Vorona
Ranch Hand
Posts: 48
Java Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!