• 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

Primitives

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q56
{
static boolean x;
public static void main(String args[])
{
int a;
if(x)
a = x ? 1: 2;
else
a = x ? 3: 4;
System.out.println(a);
}
}

This code give o/p 4.

How can this be possible.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static boolean gets a default value of false when the class is loaded.
Thus a = x ? 3: 4; get executed to set a to 4. Hope this is what you were looking for.
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But a in int,How can we assign a boolean value to int.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alpana Singh:
But a in int,How can we assign a boolean value to int.


As I responded to your duplicate post...

The syntax a = b? c: d; means if b, then a = c; else a = d. There is no assignment of b (the boolean) to a.

Here, because x is false, the line a = x? 3: 4; assigns a = 4.
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statment is not assigning a boolean to an int. Check out the bottom of the following page in the Java Tutorial to see what's going on:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right.
The test questions always have these trap.
So please be carefull.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alpana
The syntax for the ternary operator states that if(condition==true) then assign
a=value1
else
a=value2 for a expression of the type, a=x?value1:value2

What you have specified is int a=x?2:3,where only x is boolean
This means that
if(x==true) then
a=2//assign integer value 2 to a which is a integer itself
else
a=3;

x just decides which value to assign ...either 2 or 3.

A more nesting of ternary operator would be like,
int tempvar=false?true?true?1:2:3:4; //what value will tempvar get
This will be evaluated in the foll way...:
the inner most expression first,
true?1:2--Result..returns 1(because value is true itself)
so the resultant expression becomes,
false?true?1:3:4
Now true?1:3 returns 1,
Resultant expression now becomes,
false?1:4
So the final value returned would be 4 .
reply
    Bookmark Topic Watch Topic
  • New Topic