| Author |
answer the question from scjp5.0
|
nikunj thakur
Ranch Hand
Joined: Sep 23, 2007
Posts: 47
|
|
class Titanic { public static void main(String[] args) { Boolean bl = true; boolean b2 = false; boolean b3 = true; if((bl & b2) | (b2 & b3) & b3) System.out.print("alpha "); if((bl = false) | (b1 & b3) | (bl | b2)) System.out.print("beta "}; } } What is the result? beta alpha alpha beta Compilation fails. No output is produced. An exception is thrown at runtime. answer is exception is thrown at runtime i want to ask can we have more than 2 expresions for the above operator
|
 |
abhishek pendkay
Ranch Hand
Joined: Jan 01, 2007
Posts: 184
|
|
did you run the program and see the exception..??? cause as far as i see the program should not throw any exception... it wont actually compile cause this you should close it with ) noot }... apart from that the program is fine anyway please quote your sources so its also easier for us [ November 26, 2007: Message edited by: abhishek pendkay ]
|
The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
|
 |
Saroja Eshwar
Greenhorn
Joined: Oct 26, 2007
Posts: 10
|
|
Hi nikunj, The output you have given is wrong. It wont complile itself because of this line System.out.print("beta "}; Need to close the paranthesis here... System.out.print("beta "); If this has been corrected then your code will compile but it wont give any output.
|
 |
Gosia Wittemann
Ranch Hand
Joined: Nov 02, 2007
Posts: 50
|
|
I did actually try to compile and run this example (after correcting the paretheses issue and variable names (sometimes it's b1 - B one, and sometimes bl - BL)) and I get following exception: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from boolean to Boolean Of course, the easy solution is to make all variables primitive. But my question is why doesn't the compiler unbox the Boolean reference variable to a primitive?? I run it on Eclipse with Java 1.5
|
 |
nikunj thakur
Ranch Hand
Joined: Sep 23, 2007
Posts: 47
|
|
|
but i want to ask can put 3 expressions in for logical operators
|
 |
Gosia Wittemann
Ranch Hand
Joined: Nov 02, 2007
Posts: 50
|
|
|
nikunj, yes you can. After converting all variables to a primitive the code compiles, runs, just doesn't generate any output.
|
 |
Jared Cope
Ranch Hand
Joined: Aug 18, 2004
Posts: 243
|
|
Hi,
Originally posted by Gosia Wittemann: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Type mismatch: cannot convert from boolean to Boolean
After fixing the brackets and one's to el's (or vice-versa) boxing does indeed work for me. No need to convert everything to a primitive. Are you sure eclipse is compiling your code under 1.5? Check your preferences->Java->compiler setting. Cheers, Jared. P.S. There is no output.
|
SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
|
 |
Gosia Wittemann
Ranch Hand
Joined: Nov 02, 2007
Posts: 50
|
|
Jared, thanks so much, that was the trick, I used the 1.5 JRE, but with 1.4 compliance Anyway, searching for this bug made me install Eclipse Europa in the long last
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hi Nikunj, We're very happy that you're using our book to help you in your studies, and you are more than welcome to post mock questions from the book here in this forum for discussion. Speaking for myself and for all authors of mock exam questions, from now on, we need you to tell everyone the source of the mock exam question you're discussing. Thanks, Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
 |
|
|
subject: answer the question from scjp5.0
|
|
|