Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Thu Aug 11, 2016 12:12 pm
by sivarama2794
Hello Paul,
Sorry if my question is silly but I wanna know whether in real exam the difficulty level be specified or not?
Like easy,real brainer Just wanted to know
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Thu Aug 11, 2016 12:36 pm
by admin
No, the real exam does not specify the difficulty level and the exam objective. Our simulator lets you hide both.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sun Sep 04, 2016 1:31 pm
by jwitt98
I'm still a bit confused about Boolean.TRUE. Does this return a wrapper object or a primitive?
This is where my confusion stems:
Code: Select all
new Boolean("true") == Boolean.TRUE
This returns "false" which implies that both sides of the expression represent Boolean objects in which case no unboxing would occur.
However, the following returns "true":
Code: Select all
Boolean.valueOf("true") == Boolean.TRUE
This would seem to imply that unboxing is occurring. If Boolean.valueOf("true") returns a wrapper object and Boolean.TRUE returns a wrapper object, how is it that their equality comparisons return true?
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sun Sep 04, 2016 9:28 pm
by admin
Boolean.TRUE is a Boolean object. Please see this:
https://docs.oracle.com/javase/8/docs/a ... .html#TRUE
Boolean.valueOf also returns a Boolean object.
So there is no reason for unboxing.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sun Dec 18, 2016 10:47 pm
by eddie3
Hello, I was wondering if the case of Boolean.valueOf(Boolean boolean) creates a different object type from new Boolean(boolean) is unique to the Boolean class. For instance, does Integer.valueOf(Integer integer) create a different object type from new Integer(Integer integer) or are the two Integer methods substitutable? I did some research online but can't seem to find the answer. Thank you.
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sun Dec 18, 2016 11:19 pm
by admin
There is no valueOf method in Boolean that takes a Boolean. There is one that takes a boolean, though. The
Javadoc API description clearly says that it returns Boolean.TRUE or Boolean.FALSE object.
Now, regarding the Integer objects. Check out the
JavaDoc. It clearly says the following:
This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
The new operator always creates a new object. So that means in both the cases above, the object created using new and the one received using valueOf are different objects.
You should try creating one Integer object using new and one using valueOf and compare them using ==. That will validate your understanding.
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Thu Oct 12, 2017 11:10 am
by Sergey
Why does new Boolean(null); return false? I thought it should be compile time error. Why null in this case considered like string?
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Thu Oct 12, 2017 9:08 pm
by admin
Sergey wrote:Why does new Boolean(null); return false? I thought it should be compile time error. Why null in this case considered like string?
So if you have some constructor or a method that takes a String (or any other class ) as an argument, can't you pass null to it? Yes, right?
It is the same thing.
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Mon Apr 09, 2018 10:21 am
by halilaslan
However, the following returns "true":
Code: Select all
Boolean.valueOf("true") == Boolean.TRUE
This would seem to imply that unboxing is occurring. If Boolean.valueOf("true") returns a wrapper object and Boolean.TRUE returns a wrapper object, how is it that their equality comparisons return true?
The reason this expression is true is that both sides of the expression point to static final Boolean.TRUE. That is because Boolean.valueOf(param) returns either Boolean.TRUE or Boolean.FALSE
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sat Jan 05, 2019 2:38 pm
by crazymind
new Boolean("no") == false;
Does left side of "==" unbox to boolean before the comparison? thanks
Re: About Question enthuware.ocajp.i.v8.2.1415 :
Posted: Sun Jan 06, 2019 12:10 am
by admin
Yes, as per 15.21.2 of JLS, if one operand of == is a Boolean wrapper and another is a boolean primitive, then the wrapper is unboxed.
You can test this easily by printing out the value of your expression new Boolean("no") == false;