• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test {
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);
}
}
Can anybody please explain me how the above code is able to assign a boolean to a int value.
 
senthil kumaar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
I think u got confused with this line.
a = x ? 1: 2;
which is indicated in red is condition
which is indicated in blue is the value to be assigned to var a.
I hope u understand.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
I think that is not a condition that is an assignment of int to boolean.Please explain it again.
 
senthil kumaar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the place where boolean var (x) is placed is for condition, which inturn return boolean value (true/false), using this value var (a)is assigned.
In this program instead of giving the condition, they directly given the boolean var (x), using the value of x it will assign the value to var (a).
[This message has been edited by senthil kumaar (edited May 04, 2001).]
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
Kindly go through my explanation given in the code below :

Please note I have put the fol code just to drive home the point
your code can be broken down into the if else construct also

I am sure it is fine now .
Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 05, 2001).]
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOOD EXPLANATION GIVEN BY RAVINDRA.
rajashree.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic