Mixing Java code in a Scala program?

Is this allowed in Scala code:

DomNode node = node.getFirstChild() 

where DomNode is a Java type from an external java library, and getFirstChild () is determined by the type of DomNode.

I am porting an existing java program to Scala, and it would be very convenient if I left the original java declerations and also minimize porting efforts.

+4
source share
2 answers

You can use Java classes in a Scala program, but you must use the Scala syntax:

 val node: DomNode = node.getFirstChild() 

Java syntax cannot be used in the form of Type variableName .

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

 val node = node.getFirstChild() 

therefore, you do not need to explicitly specify the node type; you can let Scala infer a type.

+18
source

IntelliJ IDEA can translate from Java to Scala for you. If you paste Java code into a .scala file, IntelliJ IDEA notices it and asks if you want to try the automatic conversion. You may want to check it out .

PS

I have never tried myself ...

+11
source

All Articles