• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Boolean check

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I understand the expression (b1 = i1 == i2) gives you false here.
So, if (false) System.out.println("true") and it should print true, right??
But it prints false, why??? :roll:

Thanks
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chitra

Equals operator has the higher priority than assignment operator
so first equals operation takes place.In this case,it evaluates to false.Second step false is assign to b1.Finally if any boolean assignment
takes place in if condition, assigning value is the result of if condition.
So it prints false.

Hope ur clear
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chitra, please read my post in this thread of yours. If you do not change your displayed name, your JavaRanch membership will be suspended, and you will not be able post.
Thanks
-Barry

(NR close this account: Member # 99544)
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you replace the statement:

if (b1 = i1 == i2)

with

if (false)

then it will still print false as the output. The thing to understand here is that the if condition looks for:

if(true)
//then print this
else
//print this.

In your example code the condition was false so it will print the output of the else condition.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the program written by chitra as......
public class CompareTest {public static void main(String[] args) {boolean b1 = false;int i1 = 2;int i2 = 3;
if (b1 = i1 == i2) {
System.out.println("true");}
else {
System.out.println("false");}
}
}

y should the expression be (b1 = i1 == i2). and y not (b1 == i1 == i2)?

thankz for ur reply
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(b1 == i1 == i2) is illegal. when is a boolean == an int ?

in the expression (b1 = i1 == i2), b1 gets assigned the value of (i1 == i2), so true if i1 and i2 are equal. It would be clearer if it were retyped so :



but that is much too long. Better is



instead of the first if block. Such constructs as



are more commonly seen in C, where some people are of the opinion that an extra hour spent reading their code is worth the extra two minutes they would spend typing the extra characters.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic