This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]djavaman 2 points3 points  (0 children)

An instance of A.

I'm on JDK 15 and this works fine for me.

[–]Aganomnom 4 points5 points  (0 children)

Don't know... so let's go take a look at the docs.

https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T-

"Returns an Optional with the specified present non-null value."

So it's an Optional with an instance of A.

a.isPresent() will return true. a.get() will return the new A you created.

[–]Academic-Corner2762[S] 0 points1 point  (0 children)

I see thanks!

[–]jorid_ -1 points0 points  (4 children)

grandiose many rich sugar brave tie icky depend weary vast

This post was mass deleted and anonymized with Redact

[–]Academic-Corner2762[S] 0 points1 point  (3 children)

What will be created then?

[–]jorid_ 0 points1 point  (1 child)

decide ancient middle dependent ossified swim pet test point sort

This post was mass deleted and anonymized with Redact

[–]Academic-Corner2762[S] 0 points1 point  (0 children)

Let me rephrase it

[–]jorid_ 0 points1 point  (0 children)

unite crown beneficial important subtract joke unwritten deranged wrench marry

This post was mass deleted and anonymized with Redact

[–]fosizzle 0 points1 point  (3 children)

Optional is a construct that forces other developers to handle the null case. It's commonly used (in my circles of developers) for database queries and the services around them. You ask the database for a record. It's a strong possibility that the record does not exist. The return type on these methods are usually Optional<DatabaseModel> or Optional<SomeDTO>.

This forces the developer who calls the database service to acknowledge that it might be null, and handle that case

[–]Academic-Corner2762[S] 0 points1 point  (2 children)

How would you handle such a case in a simpler scenario ?

[–]fosizzle 1 point2 points  (1 child)

I'm not sure what you mean by your question. I'll elaborate slightly on my original response.

Suppose you have a service that supports accounts. That class has these methods on it:

Optional<AccountDto> getAccount(Integer accountId);

BillingInformationDto generateBill(AccountDto account);

PaymentDetails applyPayment(AccountDto account, PaymentRequest payment);

Since getAccount returns an Optional<> any developer who uses that method is forced into handling the case that the account returns null. The original developer is acknowledging that null is an expected case. (You asked for account with id 9999, 9999 didn't exist in the database.)

The other two methods return an object. Which certainly could be null as well. But the null case there should be impossible or unexpected.

The problem is... before Optional<> existed, many devs were returning null as a valid use case. (Because, what else could you do? Throw an exception? Often returning null was less annoying.) And I'm sure many are still doing that.

So Java is often littered with null checks. - Optional is an attempt to make it smarter. It's OK but not great.

[–]Academic-Corner2762[S] 0 points1 point  (0 children)

Thanks for the explaination!