java - 在 Scala 中使用 Java 库?

标签 java spring hibernate scala

我有一个问题希望有人指导我 我可以在 Scala 中使用所有 Java 库,例如 Guava、Gson、Log4j、Spring、Hibernate 等吗??? 例如我可以将下面的代码转换为 scala 吗? (GUAVA库)

import com.google.common.base.Joiner;
public class mainJ {
    public static void main(String[] args) {
        String[] fantasyGenres = {"Space Opera", "Horror", "Magic realism", "Religion"};        
        String joined = Joiner.on(',').join(fantasyGenres);
        System.out.print(joined);
    }
}

我想选择 SCALA 作为主要编程语言,但我不知道我是否可以在 scala 中不受限制地使用 Java 库? 任何人都可以在 scala 中转换上面的代码吗? 谢谢

最佳答案

根据Scala FAQ (强调):

The standard Scala backend is a Java VM. Scala classes are Java classes, and vice versa. You can call the methods of either language from methods in the other one. You can extend Java classes in Scala, and vice versa. The main limitation is that some Scala features do not have equivalents in Java, for example traits.

Do I need to convert Java data structures to Scala, and vice versa?

You do not have to convert Java data structures at all for using them in Scala. You can use them "as is". For instance, Scala classes can subclass Java classes, you can instantiate Java classes in Scala, you can access methods, fields (even if they are static), etc.

关于java - 在 Scala 中使用 Java 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18976484/

相关文章:

java - 无法使用 Spring 和 Thymeleaf 传输 View 上的对象

java - 如何使用 setVisibility 恢复可见性

java - 通过 Android Intent 附加多个文件,适用于一个文件,但不能处理多个文件

java - 转换 Mule HttpRequest 'GET' 响应

java - Grails 内置测试

java - MyBatis foreach 与 spring 不工作

java - 使用文件 ..xml 中定义的名称 '..' 创建 bean 时出错

java - 以多对一关系归档的版本导致对象引用未保存的 transient 实例

java - 在 Hibernate 5 中,CriteriaQuery 等同于 Criteria 的限制和投影是什么?

hibernate firebird - 创建新表