This is an example in Joshua Bloch's Effective Java.. In the following bool method, the return type is Boolean but the return statement is returning a variable of the Boolean class how does it compile and work ? please explain public class BoolTest { public static void main(String args[]) { System.out.println("whats the value :"+BoolTest.bool(true)); } public static Boolean bool(boolean a) { return a?Boolean.TRUE:Boolean.FALSE; } }

Post by:Mark Spritzler
,
ranger
staff

Apr 22, 2004 18:59:00

Huh? I don't recall that on the Sun Certified Java Developer assignment. I think you want to post that in a different forum. But here is an answer, the code is correct BoolTest.bool(true) calls the bool method passing it a boolean. If then compares it against constants in the Boolean class. The println() call will take the TRUE or FALSE and convert it to a string to print out. Mark

Post by:sivaramp pratapa
,
Greenhorn

Apr 24, 2004 17:49:00

so, you are trying to tell me, that when a variable is passed to a function as a parameter, it does some thing and returns an output. As i have understood with your answer, seems like you dint even try to understand the question! since, this was an example for a static factory method, i thought a forum which talks about advanced java is the right choice for this question. my question was how does function return an instance of Boolean class! with a return statement working on variables of Boolean class. This is not the first time,i got such an answer from the Bartender. But any way thanks for atleast trying to guide me !!

Post by:Satish Avadhanam
,
Ranch Hand

Apr 24, 2004 23:31:00

Originally posted by sivaramp pratapa:

how does function return an instance of Boolean class! with a return statement working on variables of Boolean class.

Here's the API for the variables you are pointing from Sun:

FALSE public static final Boolean FALSE The Boolean object corresponding to the primitive value false. TRUE public static final Boolean TRUE The Boolean object corresponding to the primitive value true.

So I think it should be clear now why they are returning an instance of Boolean class.

Originally posted by Sivaramp: In the following bool method, the return type is Boolean but the return statement is returning a variable of the Boolean class

As Satish pointed out, the fields Boolean.FALSE and Boolean.TRUE return a Boolean object corresponding to the primitive object FALSE or TRUE respectively. So it is returning the correct object as specified in the method signature.

Originally posted by Sivaramp: since, this was an example for a static factory method, i thought a forum which talks about advanced java is the right choice for this question.

Perhaps a forum which talks about Java in General (advanced) might be appropriate for your question (assuming you were actually asking about factory methods or something advanced), but this forum is for people to discuss the Sun Certified Java Developer (SCJD) assignments and exams. Since this question seems to be about return types of the wrapper classes to the primitives, I have moved this thread to Java in General (beginner). You can now find this thread here. Regards, Andrew

This thread has been viewed 924 times.

All times above are in ranch (not your local) time.
The current ranch time is Dec 19, 2018 02:07:42.