Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Compare boolean to integer

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can anyone explain to me why the following prints 1? Thank you.
public static void main(String[] args)
{
boolean x = true;
int a;
if(x) a=x? 1:2;
else a=x? 3:4;
System.out.println(a);
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is amazing - seems to me it should produce a compiler error but it does compile and run as you said.
public class TestB {
public static void main(String[] args)
{
boolean x = true;
int a;
if(x) a=x? 1:2;
else a=x? 3:4;
System.out.println(a);
System.out.println( a=x ); // causes expected compiler error
}
}
Bill
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a = x ? 1 : 2;
just means
if (x)
a = 1;
else
a = 2;
therefore, expanding your code would look like:
if(x) //true)
{ if(x) //true
a = 1; //a gets assigned to 1
else
a = 2;
}
else //never executes this
{}
System.out.println(a) //prints 1
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java ternary operator(conditional operator) has higher precedence than assignment operator so x?1:2 is evaluated first and then the value is assigned to a
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you posted:
p s v main (S ar[]){
boolean x = true;
int a;
if(x) a=x? 1:2;
else a=x? 3:4;
System.out.println(a);
}
=====
even though a is not initialized ,as you are not fetching the value of a , it compiles AND at runtime when the Statemnt assigns 1 to a , it does not check whats the previous value is ,ruther simply reWrites .
BUT if you test as follows ,
public static void main(String[] args)
{
boolean x = true;
int a;
System.out.println(a);
if(x) a=x? 1:2;
else a=x? 3:4;
}
compiler complains " a has not been init " .
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your point Lashkar???
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lashkar,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic