This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

'%' operator  RSS feed

 
Chris Cordner
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I've been doing some Java exercises and I'm flummoxed very early on. The following code works fine:



But if I change the if statement to if (number%2=0) then I get an error: unexpected type - expected: variable, found: value at compile time. Why is this?

Thanks
Chris
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The opposite of != is ==.
= is an assignment
== is a comparison
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, like Fred said, when you are using a conditional statement (if statement) and you want to test for equality you want to use == (only when you are testing string or object equality then you use the .equals() method but we'll discuss more on that later).
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all historical; over 40 years ago somebody used = to mean assignment rather than equality. The assignment operator ought to be := but it is too late to change it now.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It's all historical; over 40 years ago somebody used = to mean assignment rather than equality. The assignment operator ought to be := but it is too late to change it now.



It is so in the Pascal family of languages (including PL/SQL).

If you want X, you know where to find it.

 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tip:

when you checking if number is even or odd, you can replace
withModulus operator according to Joshua Bloch has very poor performance and especially that could be seen on bigger numbers. So AND'ing with 1 should give you the same answer just the way faster. Work out with pencil to see how it works.
 
Chris Cordner
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - I'd forgotten all about the '==' - and thanks for that second solution. I'm working my way through some exercises to build up experience and it suggested using the modulus for that one, but I'll try the other method too.

Chris
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:. . . It is so in the Pascal family of languages (including PL/SQL). . . .
I still use := myself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!