• 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

comparison with boolean

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to be patient with me. Again I am wondering about
the following thing:
My code looks like this:
boolean te=true; //here I assign te variable to true
if(te=false) //here I don't compare the variable, but reassign it to false
//and according to my understanding, here the expression assignment is
//done, hence, te=false, resulting in true. Shouldn't it then output true
//and not false?
//Does the compiler decide where to go based on the expression result
//succeeding or based on result turning te-variable to false?
{
System.out.println("true");
}
else
{
System.out.println("false");
}
Thanks again! JK
[ February 06, 2004: Message edited by: janne kallio ]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I think that the result should be "true"..Am I wrong?Can anybody advice pl!Thanks.
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jk,
In your code u r reassigning the value of te as false.
general form of if() else construct is:
if(true){
//do something;} // line 1
else{
//do some other thing //line 2
}
i.e. if if the condition checked in if() is true then line 1 will be executed ,otherwise the else part will be executed.
so, in ur code first the value of te is changed to false and then the condition is evaluated.
since now te is false, the else part is executed.
if u change ur code in this way it may be clear for u:
boolean te = false;
if(te = true){ // line A
System.out.println("true");
}
else{
System.out.println("False");
}
here at line a, u r assigining true to te .so, now the condition evaluates to true and hence prints true.
vineela
 
janne kallio
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, vineela!
Janne
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic