Page 1 of 1
About Question enthuware.ocpjp.v8.2.1849 :
Posted: Sat Jun 04, 2016 5:55 pm
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
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Sat Jun 04, 2016 7:21 pm
by admin
What do you mean by "missing"?
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jun 06, 2016 1:47 am
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
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jun 06, 2016 1:56 am
by admin
schchen2000 wrote:
So, my question to you is what did you mean when you said "the supplier function itself is null"????
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.
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jun 06, 2016 1:28 pm
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"????
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.
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Wed Mar 08, 2017 3:35 am
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%
}
}
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Wed Mar 08, 2017 10:30 am
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.
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Sun Jul 22, 2018 10:41 am
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
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jul 23, 2018 4:28 am
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.
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jul 23, 2018 12:29 pm
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!
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jul 23, 2018 12:46 pm
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.
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Mon Jul 23, 2018 1:37 pm
by eliquinox
My bad, that's what I was missing!
Thanks a lot for your help!
Re: About Question enthuware.ocpjp.v8.2.1849 :
Posted: Wed Feb 19, 2020 10:07 am
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.