0

Instead of initialising optCar variable as below:

Optional<'Car'> optCar = Optional.of(car);

Why not initialise it simply as :

Car optCar = car;

When a business logic does not permit car to be null?

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Chandan Kumar
  • 303
  • 1
  • 3
  • 11
  • If you have a null check you don't need the optional. But the whole point of the optional is to avoid null checks. If you can be sure that car is *never* null, you can drop the optional. – geanakuch May 07 '21 at 04:55
  • In this scenario I want a NullPointerException to be thrown wherever its value is missing so that I could catch the bug. Then, what is the point in using Optional.of method at all? – Chandan Kumar May 07 '21 at 05:01
  • "Why not initialise it simply as..." Why don't you ask the person who wrote the code? – Sweeper May 07 '21 at 05:04
  • @ChandanKumar *"what is the point in using Optional.of method at all?"* -> wrong question. The right question is "why use `Optional` at all?" – ernest_k May 07 '21 at 05:07
  • If you want a NPE then the optional is obviously the wrong approach. – geanakuch May 07 '21 at 05:15
  • If `car` can be null, you should use `Optional.ofNullable` instead of `Optional.of`. You'd use `Optional.of` when you can guarantee that `car` is not null. – enzo May 07 '21 at 05:26
  • @ernest_k I understand that in Optional optCar = Optional.ofNullable(car); If 'car' were null, the resulting object would be empty. However, in Optional optCar = Optional.of(car); If 'car' were null, a NullPointerException would be thrown immediately. So, why should I use Optional.of method at all? Why should I not go by old JAVA way instead? – Chandan Kumar May 07 '21 at 05:35
  • 2
    You generally use `Optional.of` with literals. If your method returns an `Integer` that can be null and in some condition you want to return the number 1, you would use `Optional.of(1)` instead of `Optional.ofNullable(1)`. – enzo May 07 '21 at 05:43

2 Answers2

0

You totally can.

Optional is for situations where you would have a code

If(object == null) {…

Instead you use Optional and then you have cool access to stream like features and or/or else statements that make ur code so much nicer, for example:

return Optional.ofNullable(object).orElse(defaultObject);

Or another example

return Optional.ofNullable(object).map(Object::toString).orElseThrow();

This last code return string representation of ur object if that optional is present or else throws an exception. orElseThrow is most useful in situations where you actually are sure that object is always there so such situation will likely never arise and if it does u know where to look.

But like I said in the beginning if you don’t need these features u don’t need to use it

J Asgarov
  • 2,526
  • 1
  • 8
  • 18
0

if you are sure that car can never be null then using Optional would be an overkill.

The presence of Optional is to provide for clarity that this particular instance can be null and the Optional needs to be unwrapped and explicitly checked for the absence (or presence) of a value.

Do not go by what I say, hear it from the expert:

Our intention was to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.

Excerpted from Brian Goetz's answer here.

Khanna111
  • 3,627
  • 1
  • 23
  • 25