Mixing Java code in Scala program?

后端 未结 2 509
心在旅途
心在旅途 2021-01-12 10:28

Is this allowed in Scala code:

DomNode node = node.getFirstChild()

where DomNode is Java type from external java library and getFirstChild(

2条回答
  •  天涯浪人
    2021-01-12 10:44

    You can use Java classes in a Scala program, but you would ofcourse have to use Scala syntax:

    val node: DomNode = node.getFirstChild()
    

    You cannot use Java syntax in the form Type variableName.

    edit (thanks to ericacm) - You can also just specify

    val node = node.getFirstChild()
    

    so you don't have to specify the type of node explicitly; you can let Scala infer the type.

提交回复
热议问题