title: Get default value if the object is null in Java
body: <p>I am curious to know how I can write a shorter version of the following code in Java.</p>
  <p>I have the following Java class (belongs to JAX-RS):</p>
  <ul>
  <li><a href="https://github.com/neo4j/jsr311-api/blob/master/src/javax/ws/rs/core/Response.java" rel="nofollow noreferrer">javax.ws.rs.core.Response</a></li>
  </ul>
  <p>I need back the int value of the responseStatus if that's possible (response is not null) otherwise default int status value needs to be returned.</p>
  <p>I do not want to add any library dependency to my project just for this small piece of code.</p>
  <p><strong>This is the code that came up in my mind first:</strong></p>
  <pre class="lang-java prettyprint-override"><code>    private static int getDefaultStatusCodeIfNull(final Response response) {
          if (Objects.isNull(response)) {
              return Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
          }
  
          return response.getStatus();
      }
  </code></pre>
  <p><strong>The following code is maybe shorter with lambda but it is so long and hard to read:</strong></p>
  <pre class="lang-java prettyprint-override"><code>    int status = Optional.ofNullable(response)
            .orElse(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build()).getStatus();
  </code></pre>
  <p>Is there any shorter one-line way to get this int value?</p>
  <p>Do you think the 2nd one is a better solution than the 1st?</p>