Java Scala 的优点是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1345713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:28:05  来源:igfitidea点击:

What Are The Benefits Of Scala?

javascala

提问by Radi

I am a Java developer and I want to know how I can use Scala in my Java programs?

我是一名 Java 开发人员,我想知道如何在我的 Java 程序中使用 Scala?

采纳答案by oxbow_lakes

Go read Daniel Spiewak's excellent blog seriesabout Scala. With Scala you can keep:

去阅读Daniel Spiewak关于 Scala的优秀博客系列。使用 Scala,您可以保持:

  • all your Java libraries
  • all the advantages of running on a JVM (ubiquity, administrative tools, profiling, garbage collection etc)
  • 你所有的 Java 库
  • 在 JVM 上运行的所有优势(无处不在、管理工具、分析、垃圾收集等)

But you can write Scala code:

但是您可以编写 Scala 代码:

  • more concise and clear than Java (especially using more functionalstyle, such as in the collections library)
  • it has closures and functions as part of the language
  • it has operator overloading (from the perspective of usage)
  • it has mixins (i.e. interfaces which contain implementation)
  • 比Java更简洁明了(尤其是使用更多的函数式风格,比如在collections库中)
  • 它有闭包和函数作为语言的一部分
  • 它有运算符重载(从使用的角度来看)
  • 它有 mixins(即包含实现的接口)

回答by VonC

I am not sure you can easily use Scala in your Java programs, as in "call a Scala class from a Java class".

我不确定您是否可以在 Java 程序中轻松使用 Scala,例如“从 Java 类调用 Scala 类”。

You can try, following the article "Mixing Java and Scala".
Relevant extracts:

您可以按照“混合 Java 和 Scala”一文尝试。
相关摘录:

The problem is that the Java and Scala compilation steps are separate: you can't compile both Java and Scala files in one go.
If none of your Java files reference any Scala classes you can first compile all your Java classes, then compile your Scala classes.
Or, if none of your Scala files reference any Java classes you can do it the other way around.
But if you want your Java classes to have access to your Scala classes and also have Scala classes have access to your Java classes, that's a problem.

Scala code can easily call directly into Java code, but sometimes calling Scala code from Java code is trickier, since the translation from Scala into bytecode is not quite as straightforward as for Java:
sometimes the Scala compiler adds characters to symbols or makes other changes that must be explicitly handled when calling from Java.
But a Scala class can implement a Java interface, and an instance of that class can be passed to a Java method expecting an instance of the interface.
The Java class then calls the interface methods on that instance exactly as if it were a Java class instance.

问题在于 Java 和 Scala 的编译步骤是分开的:您不能同时编译 Java 和 Scala 文件。
如果您的 Java 文件都没有引用任何 Scala 类,您可以先编译所有 Java 类,然后再编译 Scala 类。
或者,如果你的 Scala 文件都没有引用任何 Java 类,你可以反过来做。
但是,如果您希望 Java 类可以访问您的 Scala 类,并且还希望 Scala 类可以访问您的 Java 类,那就有问题了。

Scala 代码可以很容易地直接调用 Java 代码,但有时从 Java 代码调用 Scala 代码比较棘手,因为从 Scala 到字节码的转换不像 Java 那样简单:
有时 Scala 编译器向符号添加字符或进行其他更改从 Java 调用时必须显式处理。
但是 Scala 类可以实现 Java 接口,并且该类的实例可以传递给需要该接口实例的 Java 方法。
然后 Java 类完全像调用 Java 类实例一样调用该实例上的接口方法。

The opposite is possible, of course, as described in Roundup: Scala for Java Refugees, from Daniel Spiewak.

相反的是可能的,当然,如在综述:Scala中为Java难民,从丹尼尔Spiewak

回答by Steve Lianoglou

Also, take a look at this recent news item post on Scala's site: "Research: Programming Style and Productivity".

另外,请查看 Scala 站点上最近的这篇新闻文章:“研究:编程风格和生产力”。

In his paper, Gilles Dubochet, describes how he investigated two aspects of programming style using eye movement tracking. He found that it is, on average, 30% faster to comprehend algorithms that use for-comprehensions and maps, as in Scala, rather than those with the iterative while-loops of Java.

在他的论文Gilles Dubochet 中,描述了他如何使用眼动跟踪研究编程风格的两个方面。他发现,理解使用 for-comprehensions 和 map 的算法(如在 Scala 中)比使用 Java 的迭代 while 循环的算法平均快 30%。

And another key quote from the news item:

新闻项目中的另一个关键引述:

Alex McGuire, who writes mission critical projects in Scala for power trading companies, says of Scala "The conciseness means I can see more of a program on one screen. You can get a much better overview. When I have some mathematical model to write with Java I have to keep two models in my head, the mathematical model itself and the second a model of how to implement it in Java. With Scala one model, the mathematical one, will do. Much more productive.”

用 Scala 为电力贸易公司编写关键任务项目的 Alex McGuire 谈到 Scala 时说:“简洁意味着我可以在一个屏幕上看到更多程序。你可以获得更好的概览。当我有一些数学模型可以编写时“Java 我必须在脑海中保留两个模型,一个是数学模型本身,另一个是如何在 Java 中实现它的模型。使用 Scala,一个模型,即数学模型,就可以做到。效率更高。”

You an read the rest of the post and other linked items there.

您的阅读后和其他链接项目的其余部分存在

回答by clay

UPDATED 2019

2019 年更新

I can name some simple points in plain language from my limited experience:

根据我有限的经验,我可以用通俗的语言说出一些简单的观点:

  1. Properties. C++ and Java had this notion of a public getter/setter function "property" wrapped around an internal class variable which led to large amounts of boilerplate code. C# formalized this as a real language feature and reduced much of the boilerplate in C# 3.0 with auto-implemented properties. Scala classes define trivial properties simply as regular read only vals or read/write vars. The class may later choose to replace those with get or get/set methods without affecting client code. For this, Scala provides the most elegant solution with the least language features and complexity.

  2. Arrays use regular generics. In Java/C#, int[]is redundant and confusing vs List<int>or List<Int>. Worse, in Java, List<Int>has lots of runtime overhead, so many developers have to know to use int[]. Scala avoids that problem. Also, in Java/C#, arrays support (covariant) casting, which was a mistake, that they now can't fix because of legacy concerns.

  3. Scala has better support for immutability. valis a basic language feature.

  4. Scala lets if blocks, for-yield loops, and code in braces return a value. This is very elegant in many situations. A very small plus is that this eliminates the need for a separate ternary operator.

  5. Scala has singleton objects rather than C++/Java/C# class static. This is a cleaner solution.

  6. Pattern matching. Object unpacking. Very nice in a large numbers of situations.

  7. Native tuples.

  8. "case classes" which are what most other languages would call record types or named tuples.

  9. Fancier standard library with more elegant collections.

  10. Multi-line strings. String interpolation formatting.

  11. Optional semi-colons.

  1. 特性。C++ 和 Java 有一个公共 getter/setter 函数“属性”的概念,它包裹在一个内部类变量周围,这导致了大量的样板代码。C# 将其正式化为一个真正的语言功能,并通过自动实现的属性减少了 C# 3.0 中的大部分样板文件。Scala 类将简单的属性定义为普通的只读变量或读/写变量。该类稍后可以选择用 get 或 get/set 方法替换那些方法,而不会影响客户端代码。为此,Scala 以最少的语言特性和复杂性提供了最优雅的解决方案。

  2. 数组使用常规泛型。在 Java/C# 中,int[]List<int>List<Int>. 更糟糕的是,在 Java 中,List<Int>有很多运行时开销,因此许多开发人员必须知道使用int[]. Scala 避免了这个问题。此外,在 Java/C# 中,数组支持(协变)转换,这是一个错误,由于遗留问题,它们现在无法修复。

  3. Scala 对不变性有更好的支持。val是一个基本的语言特性。

  4. Scala 让 if 块、for-yield 循环和大括号中的代码返回一个值。这在很多情况下都非常优雅。一个非常小的优点是这消除了对单独的三元运算符的需要。

  5. Scala 有单例对象,而不是 C++/Java/C# 类静态。这是一个更清洁的解决方案。

  6. 模式匹配。对象解包。在大量情况下非常好。

  7. 原生元组。

  8. “案例类”是大多数其他语言所称的记录类型或命名元组。

  9. 更优雅的标准库,更优雅的收藏。

  10. 多行字符串。字符串插值格式化。

  11. 可选的分号。

Cons.

缺点。

  1. Java has caught up a lot. Java 8 was first released in 2014, but it took several years for older Java versions to be phased out and the new Java 8 features to be fully used across the Java ecosystem. Now, lambdas and closures and basic functional collections, with support for filter/map/fold are quite standard for the Java ecosystem. More recently, Java has added basic varlocal variable type inference and has multi-line strings and switch expressions in release builds preview mode.

  2. Scala is complicated. I'd highlight features like implicitsto be inherently confusing.

  3. Scala has minimal backward compatibility. Scala 2.10 artifacts are incompatible with Scala 2.11.

  4. Building a Java API for other JVM-language developers like Scala or Clojure or Kotlin is normal, well supported and accepted. You generally don't want to build APIs in Scala that cater to non-Scala developers.

  1. Java已经赶上了很多。Java 8 于 2014 年首次发布,但旧 Java 版本的淘汰和 Java 8 的新功能在整个 Java 生态系统中得到充分利用需要数年时间。现在,支持过滤器/映射/折叠的 lambda 和闭包以及基本功能集合对于 Java 生态系统来说是相当标准的。最近,Java 添加了基本的var局部变量类型推断,并在发布版本预览模式下具有多行字符串和 switch 表达式。

  2. Scala 很复杂。我要强调一些implicits本质上令人困惑的功能。

  3. Scala 具有最小的向后兼容性。Scala 2.10 工件与 Scala 2.11 不兼容。

  4. 为其他 JVM 语言开发人员(如 Scala、Clojure 或 Kotlin)构建 Java API 是正常的,得到很好的支持和接受。您通常不想在 Scala 中构建迎合非 Scala 开发人员的 API。

回答by guilhebl

Scala or Java:

Scala 或 Java:

Pros:

优点:

  • Scala supports both functional and imperative OO programming styles and it advocates that both models are not conflicting with each other but yet they are orthogonal and can complement each other. Scala doesn't require or force the programmer to use a particular style, but usually the standard is to use functional style with immutable variables when appropriate (there are several benefits of using the functional approach such as concise and short syntax and using pure functions usually reduces the amount of non-determinism and side-effects from the code), while resorting to imperative programming when the code would look simpler or more understandable.
  • Scala doesn't require ; at the end of each line having it optional which leads to cleaner code
  • In Scala functions are first class cititzens
  • Scala supports some advanced features which are directly built in the language such as: Currying, Closures, Higher order functions, pattern matching, Higher Kinded Types, Monads, implicit params.
  • Scala can interact very well with Java and both can coexist. It is possible to use java libraries directly inside Scala code invoking Java classes from scala code.
  • Has Tuples built in the language which makes life easier in several scenarios
  • Supports operator overloading
  • has a rich Ecosystem and some popular open source projects in Apache are based on it.
  • Async and Non-blocking code is very easy to write with Scala Futures
  • Scala supports the Actor model using Akka which can be highly efficient and scalable when running distributed applications in multi-threaded and parallel business use cases (Enforce encapsulation without resorting to locks, State of actors is local and not shared, changes and data is propagated via message)
  • Code tends to be shorter if compared to Java (might not be always the case)
  • Scala 支持函数式和命令式 OO 编程风格,它主张两种模型彼此不冲突,但它们是正交的并且可以相互补充。Scala 不要求或强迫程序员使用特定的风格,但通常标准是在适当的时候使用带有不可变变量的函数式风格(使用函数式方法有几个好处,例如简洁和简短的语法以及通常使用纯函数减少代码的不确定性和副作用的数量),同时在代码看起来更简单或更易于理解时诉诸命令式编程。
  • Scala 不需要;在每一行的末尾,它是可选的,这会导致代码更清晰
  • 在 Scala 中,函数是一流的公民
  • Scala 支持一些直接内置于语言中的高级功能,例如:柯里化、闭包、高阶函数、模式匹配、高阶类型、Monads、隐式参数。
  • Scala 可以与 Java 很好地交互并且两者可以共存。可以在从 Scala 代码调用 Java 类的 Scala 代码中直接使用 Java 库。
  • 在语言中内置了元组,这使得在多种情况下的生活更轻松
  • 支持运算符重载
  • 拥有丰富的生态系统,Apache 中一些流行的开源项目都是基于它的。
  • 使用 Scala Futures 很容易编写异步和非阻塞代码
  • Scala 支持使用 Akka 的 Actor 模型,该模型在多线程和并行业务用例中运行分布式应用程序时可以高效且可扩展(强制封装而不求助于锁,actor 的状态是本地的而不是共享的,更改和数据通过信息)
  • 与 Java 相比,代码往往更短(可能并非总是如此)

Cons:

缺点:

  • Steep learning curve if compared to Java and other languages, requires more time in general from the learner to understand all the concepts clearly. Has many features
  • It is not as well established as Java in the market since it was invented later so Java in overall is more mature and more battle-tested.
  • Scala opens too many doors. It allows a lot of complex syntax that if used in a irresponsible way might lead to code that is hard to understand. Abusing things such as operator overloading, implicit params and other constructs can be counter-productive and might ruin code legibility.
  • Java is also evolving and still getting better with newer versions (such as with JDK 9 modules)
  • 与 Java 和其他语言相比,陡峭的学习曲线通常需要学习者更多的时间来清楚地理解所有概念。有很多特点
  • 它在市场上并不像 Java 那样成熟,因为它是后来发明的,所以 Java 总体上更成熟,也更经得起考验。
  • Scala 打开了太多的门。它允许使用许多复杂的语法,如果以不负责任的方式使用,可能会导致代码难以理解。滥用运算符重载、隐式参数和其他构造等内容可能会适得其反,并可能破坏代码的易读性。
  • Java 也在不断发展,并且在更新版本(例如使用 JDK 9 模块)时仍然变得更好