This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "boolean" Watch "boolean" New topic
Author

boolean

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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

Joined: Apr 26, 2001
Posts: 12
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

Joined: Feb 24, 2001
Posts: 290
hi ,
I think that is not a condition that is an assignment of int to boolean.Please explain it again.
senthil kumaar
Greenhorn

Joined: Apr 26, 2001
Posts: 12
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

Joined: Mar 16, 2001
Posts: 216
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

Joined: Mar 10, 2001
Posts: 151
GOOD EXPLANATION GIVEN BY RAVINDRA.
rajashree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: boolean
 
Similar Threads
initailzation
if one int is less than or greater than another int by one
The strange ways of compile time constants
please help meeeee
Convert 1d array to 2D boolean