• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Presidence in an IF statement

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i understand: Conditional operators have higher presidence over Assignment operators, as the following example code shows:

public class Q10
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
boolean b = false;

if( b = i == j)
System.out.println("True");
else
System.out.println("False");
}
}

(Prints out "True")


What i cant understand is why a compliation error is not thrown at the fact a boolean variable is having an int assigned to it in the IF statement. It doesnt even look like a legal IF statemnt, but it is!

If you place the code:

b = i anywhere else, the compiler picks up this illegal assignment.

Is the assignment being ignored because the conditional side of the IF statement is the only part the compiler deals with?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What i cant understand is why a compliation error is not thrown at the fact a boolean variable is having an int assigned to it in the IF statement. It doesnt even look like a legal IF statemnt, but it is!



The reason it works is because...

As i understand: Conditional operators have higher presidence over Assignment operators,



The boolean variable "b" is *not* being assigned the value of "i", it is being assigned the boolean value of whether "i" is equal to "j". In this case, it is assigned the value of true.

Henry
 
jon ruane
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oooooooooooooooooooooooh!

thanks very much!
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Try this one:

public class Q10
{
public static void main(String[] args) {
int i = 10;
int j = 10;
boolean b = false;

b = i == j;
System.out.println(b);


}
}

It works fine.

First it compares i & j. The resulat is true. Then it assigns true to b.

Regards
Urs
 
reply
    Bookmark Topic Watch Topic
  • New Topic