• 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

It should be true.........

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the result of executing the following fragment of code:
boolean b1 = false;
boolean b2 = false;
if (b2 = b1 != b2)
{
System.out.println("true");
} else
{
System.out.println("false");
}
Answer : it will print "false"
I think it should print "true"
because first the expression b1!=b2 will return false (as b1 and b2 actually contain "false")
and now this "false" is compared with b2(which is also "false")
Hence when b2(false) = false is compared it is true,
So, it should print "true"...
Am I right??
Val Your Comments..??
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:
What is the result of executing the following fragment of code:
boolean b1 = false;
boolean b2 = false;
if (b2 = b1 != b2)
{
System.out.println("true");
} else
{
System.out.println("false");
}
Answer : it will print "false"
I think it should print "true"
because first the expression b1!=b2 will return false (as b1 and b2 actually contain "false")
and now this "false" is compared with b2(which is also "false")
Hence when b2(false) = false is compared it is true,
So, it should print "true"...
Am I right??
Val Your Comments..??



The problem is "when b2(false) = false is compared it is true,"
b2 = false is a assignment, not a comparsion, overall result is false then.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chichih Lin:


The problem is "when b2(false) = false is compared it is true,"
b2 = false is a assignment, not a comparsion, overall result is false then.



hi lin
i want a clarification the stat
if (b=b1!=b2){
------
}
irrespective of values of b,b1,b2
final thing in 'if' is assignment b=false / b=true
the assignment operation is a success so it is true (my guess).
so the o/p must be true,so whats actually happening ?
can u pull me out from my doubt.
---sai
 
sai jai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chichih Lin:


The problem is "when b2(false) = false is compared it is true,"
b2 = false is a assignment, not a comparsion, overall result is false then.



hi lin
i want a clarification the stat
if (b=b1!=b2){
------
}
irrespective of values of b,b1,b2
final thing in 'if' is assignment b=false / b=true
the assignment operation is a success so it is true (my guess).
so the o/p must be true,so whats actually happening ?
can u pull me out from my doubt.
---sai

 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the statement is:
if (b2 = b1 != b2)
notice the first = is assigment the second is != which is comparison operator.
assigment get done last by the compiler so first he computes the comparison.
b1 is false and b2 is false so b1 != b2 returns false (because they r equal).
now we have the assignmment :
b2= false
so now b2 equals false and thats also the result of the if so we have if(false)
that goes to the else block and prints false.
 
reply
    Bookmark Topic Watch Topic
  • New Topic