title: Difference between "Optional<ButtonType>.get()" and "alert.getResult()"
body: <p>I was fiddling around with JavaFX and got to a webpage which lists all the different types of dialog boxes and the creator of the content used something like this:</p>
  
  <pre><code>Alert alert = new Alert(AlertType.CONFIRMATION);
  alert.setTitle("Confirmation Dialog");
  alert.setHeaderText("Look, a Confirmation Dialog");
  alert.setContentText("Are you ok with this?");
  
  Optional&lt;ButtonType&gt; result = alert.showAndWait();
  if (result.get() == ButtonType.OK){
      // ... user chose OK
  } else {
      // ... user chose CANCEL or closed the dialog
  }
  </code></pre>
  
  <p>And what I did was the following:</p>
  
  <pre><code>alert.showAndWait();
  if (alert.getResult() == ButtonType.OK)
  {
      //User chose OK
  } 
  
  else
  {
      //User chose other
  }
  </code></pre>
  
  <p>I read the documentation of most of these methods and classes and what I can gather from this is that he uses <code>Optional</code> in case the method <code>showAndWait()</code> returns a <code>null</code>. According to the docs the <code>showAndWait()</code> method returns an <code>Optional.ofNullable</code> which may or may not be <code>null</code>. And then in the if statement he gets the result from that <code>Optional&lt;ButtonType&gt;</code> which contains whatever <code>showAndWait()</code> returned and compares it.</p>
  
  <p>In my part of the code I went straight to get the result from the <code>Alert</code> instance with <code>getResult()</code> and compared it which might be bad because <code>showAndWait()</code> might return null.</p>
  
  <p>Is that the main difference? Protection against null? Or is there another reason as to why using Optional might be better other than that? Just wondering if someone could explain this better.</p>
  
  <p>extra: Also I'd be very grateful if someone could tell me what the return type of <code>showAndWait()</code> means, it returns : <code>Optional&lt;R&gt;</code>. I'm assuming it's a generic type, but I've never seen that before, I've only seen <code>E</code> <code>T</code> <code>N</code> etc.</p>
  
  <p>Thank you for any input on this.</p>