• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does (a=true) from if (a=true) evaluates to true ...

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isnt there an assignment going on within the parathesis ?
is it because the assignment is of a boolean that a boolean is returned ?
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do the assignment first and then do the boolean test. Since you are saying a = true, a will be equal to true, and then you do the boolean test which gives you true.
This is a trick they will try to do by using = instead of ==
Bill
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sarim,
if() in java takes boolen value. boolean value in java are true and false. C & C++ programmer have to be careful as in that boolean could also take numerical values...where 0 mean false and 1 mean true. Why i am saying is that in the parentheses of if statement you could either directly put boolean value or an expression.
so when you say if(a=true)....here a=true is an expression result of which is boolean value that is true.
Where as in c & c++ we could use expression which could evaluate to 0 (false) and 1 (true).
So while going through the code given in exam be careful of seeing that if it is = or == operator.
Hope things are clear to you now.

Chek the following code:
class Test {
public static void main(String [] args) {
boolean flag = false;
if (flag=true)
System.out.println("Hello Java");
else
System.out.println("Hello Sarim");
}
}

check the output.
Regards,
Raj.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic