About Question enthuware.ocpjp.v8.2.1849 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1849 :

Post by schchen2000 »

Optional's orElseGet method takes a java.util.function.Supplier function as an argument and invokes that function to get a value if the Optional itself is empty. Just like the orElse method, this method does not throw any exception even if the Supplier returns null. It does, however, throw a NullPointerException if the Optional is empty and the supplier function itself is null.
When you say "the supplier function itself is null" in the above quote, did you mean to say if the supplier function itself is missing?

Thanks.

Schmichael

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

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by admin »

What do you mean by "missing"?
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by schchen2000 »

admin wrote:What do you mean by "missing"?
It does, however, throw a NullPointerException if the Optional is empty and the supplier function itself is null.
This was part of the quote I gave in my previous question. In that previous quote, the Supplier generating a null return value is ok and I agreed. What I was thinking was if the Supplier is there, it will return EITHER a null value OR a non-null value but when you said "supplier function itself is null" so I thought the Supplier argument inside orElseGet method is missing. That's where the word "missing" came from.

Thanks to you. When you ask what "missing" means and that forced me rethink my logic. If that Supplier argument is missing inside the orElseGet method, then the program won't even compile in the first place, never mind generating a NullPointerException at runtime.

So, my question to you is what did you mean when you said "the supplier function itself is null"????

Thanks.

Schmichael

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

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by admin »

schchen2000 wrote: So, my question to you is what did you mean when you said "the supplier function itself is null"????

Code: Select all

orElseGet(supplierRef);
supplierRef is null.

Remember that in Java you always pass objects by their references. So it is only the references that can be null. So when one says some object is null, what is meant is that the reference that is supposed to point to an object of that type is pointing to nothing i.e. it is null.
If you like our products and services, please help us by posting your review here.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by schchen2000 »

admin wrote:
schchen2000 wrote: So, my question to you is what did you mean when you said "the supplier function itself is null"????

Code: Select all

orElseGet(supplierRef);
supplierRef is null.

Remember that in Java you always pass objects by their references. So it is only the references that can be null. So when one says some object is null, what is meant is that the reference that is supposed to point to an object of that type is pointing to nothing i.e. it is null.
Ok. I get it now. Thank you.

Ingvarr
Posts: 7
Joined: Sun May 08, 2016 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by Ingvarr »

I fail to see how the second correct option protects us from an NPE; after all, it falls back to the very same method that produces nulls in the first place:

Code: Select all

class Test{
    static Double getPrice(String str) { 
        Double d = Math.random() > 0.5 ? Double.parseDouble(str) : null;
        return d;
    }
    public static void main(String[] args) {
        Optional<Double> price = Optional.ofNullable(getPrice("1111"));
        Double y = price.orElseGet(()-> getPrice("333"));
        System.out.println(y);                               // probability of null is 25%
    }
}

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

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by admin »

It is true that y may still be assigned a null. But that is not same as a NullPointerException! It just means that y is null. NPE will be thrown only if you call a method on y, which is not happening in the given code.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

eliquinox
Posts: 3
Joined: Sun Jul 22, 2018 10:33 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by eliquinox »

The following code throws NPE (in two places if ran in isolation):

Code: Select all

Supplier<Double> nullSupp = () -> null;
        Optional<Double> doubleOptional = Optional.ofNullable(null);
        double d = doubleOptional.orElse(null); //NullPointerException
        double a = doubleOptional.orElseGet(nullSupp); //NullPointerException
All I did was replace method call in orElse() from getPrice() to null in order to simulate a method that returns null.
By the same token, I have replaced a supplier that in orElseGet() from the one that returns the result of getPrice() to the one that returns null directly.

I may be missing something. Could someonw please justify why, given the example above, the test answers are correct?

Thanks,

Ed

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

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by admin »

Well, you need to get into the habit of going through the JavaDoc API descriptions to understand what is going on.
After line 2, doubleOptional refers to an Optional object containing null because that is what Optional.ofNullable(null) will return.
At line 3, the call to doubleOptional.orElse(null) will return a null because doubleOptional points to an Optional object containing null. But you can't unbox a null into a double and so the JVM throws a NullPointerException.

You should work out the next statement similarly.
If you like our products and services, please help us by posting your review here.

eliquinox
Posts: 3
Joined: Sun Jul 22, 2018 10:33 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by eliquinox »

That is exactly the point,

Code: Select all

Optional<Double> doubleOptional = Optional.ofNullable(null);
double d = doubleOptional.orElse(null); //NullPointerException
I guaranteed to throw a NPE. The question states that getPrice() method may or may not return null (presumably for any arbitrary input String). When modelling the worst path, i.e. passing the null directly instead of having method return null, like above, it follows that the at least the first answer marked right does not guarantee that NPE will not be thrown at runtime. By modelling the worst path for the second answer we have :

Code: Select all

Supplier<Double> nullSupp = () -> null;
Optional<Double> doubleOptional = Optional.ofNullable(null);
double a = doubleOptional.orElseGet(nullSupp); //NullPointerException
In this code I have Optional.ofNullable() take null as opposed to having getPrice() return null and a Supplier supplying null. In this scenario we are again guaranteed to get NPE at runtime.

I have followed the Java Doc for optionals and read the source code, but I am still unable to gather why the two answers guarantee not to throw NPE.

Thanks!

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

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by admin »

Your code is different from the code given in the question. The options given in the question have "Double d = ", while your code has "double d = ". I explained why your code throws NPE. The code given in the question won't throw NPE.
If you like our products and services, please help us by posting your review here.

eliquinox
Posts: 3
Joined: Sun Jul 22, 2018 10:33 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by eliquinox »

My bad, that's what I was missing!

Thanks a lot for your help!

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1849 :

Post by bvrulez »

The last conversation covers an important fact: orElse() and orElseGet() can return "null", so then "Double y = null". This is not possible with a Primitive. So "double y = null" would not compile (or maybe throw an Exception). I would imagine this made for a nice trick on an exam question.

Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests