gpt4 book ai didi

java - 在 Scala 中使用 Java 库?

转载 作者:行者123 更新时间:2023-12-01 06:39:13 24 4
gpt4 key购买 nike

我有一个问题希望有人指导我我可以在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com