aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about boolean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about boolean?" Watch "a question about boolean?" New topic
Author

a question about boolean?

Albert Lee
Greenhorn

Joined: Aug 04, 2002
Posts: 18
1. String foo = �blue�;
2. Boolean[]bar = new Boolean [1];
3. if (bar[0]) {
4. foo = �green�;
5. }
What is the result?
A.foo has the value of ��
B.foo has the value of null
C.foo has the value of �blue�
D.foo has the value of �green�
E.an exception is thrown
F.the code will not compile
the answer is f.But I think it should be c.Please
help me?thanks in advance!


Everyday create your history.
Roan Nicolas
Greenhorn

Joined: Nov 11, 2002
Posts: 23
The correct answer is indeed F. This code will not compile since the conditional expression in the if statement must result in a boolean value. The expression bar[0] in your code however is a Wrapper class Boolean and not the primitive type boolean.
You can get the code to compile if you use the booleanValue() method of the Boolean Class which returns the primitive value in the wrapper object.


Owee<p>SCJP 1.4
tony kanvas
Ranch Hand

Joined: Oct 26, 2002
Posts: 97
hi
Hi first if you try to compile your code it will give you (CE)incompatible type because you deal with wrapper class the problem with the way of the question i think
Remember this: Wrapper classes are immutable and final (you cant do any change)
folks Correct me if I am wrong
thanks
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Wrapper classes (including String) are immutable, which means the value of the Object can not be changed. YET this doesn't mean that their references can't be changed as well. A reference to a wrapper class object is NOT final unless you declare it final. so:
String s = "Hi";
s = "bye";
is totally a valid snippit.
The answer is F because the Boolean wrapper class can't be used in an if statement, you need a boolean - with small b - value for the if expression, and that's the trick in the Q.
HTH


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Adriano Dadario
Greenhorn

Joined: Nov 19, 2002
Posts: 1
I think that you must inicialize the variable first.
Then the code is:

Remember! Only primitive variables are inicialize with default values. Objects are null
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about boolean?
 
Similar Threads
Strings
why the code can't be compiled
HAS-A relation
what is the output and how..
PGJC Exam - clarification?