About Question enthuware.ocajp.i.v8.2.1415 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sivarama2794
Posts: 4
Joined: Sat Aug 06, 2016 11:07 am
Contact:

About Question enthuware.ocajp.i.v8.2.1415 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post by admin »

No, the real exam does not specify the difficulty level and the exam objective. Our simulator lets you hide both.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jwitt98
Posts: 3
Joined: Thu Aug 04, 2016 10:40 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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.
If you like our products and services, please help us by posting your review here.

eddie3
Posts: 14
Joined: Sat Dec 17, 2016 10:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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.
If you like our products and services, please help us by posting your review here.

halilaslan
Posts: 1
Joined: Thu Apr 05, 2018 8:49 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post by crazymind »

new Boolean("no") == false;

Does left side of "==" unbox to boolean before the comparison? thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1415 :

Post 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;
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 60 guests