title: How can I use a Java Optional object in Scala?
body: <p>I have to use a Java function in Scala, which takes arguments of type <code>Optional&lt;Type&gt;</code>, and the object I have to pass to it also comes from a Java library but is simply of type <code>Type</code>. How can I use that in Scala? </p>
  
  <p>Is it OK, If I typecast it like this. Note that here <code>javaObj</code> comes from a Java library and is of type <code>Type</code>. But <code>someFunctionDefinedInAJavaLibrary</code> function here expects an argument of type <code>Optional&lt;Type&gt;</code>.</p>
  
  <pre><code>import java.util.Optional
  ....
  // javaObj here is of type Type and comes from a Java library
  val scalaObj: Optional[Type] = javaObj.asInstanceOf[Optional[Type]]
  // someFunctionDefinedInAJavaLibrary's argument is of type Optional&lt;Type&gt;
  val r = someFunctionDefinedInAJavaLibrary(scalaObj)
  </code></pre>