scala vs java 相同点和差异
本贴是我摘抄自国外网站,用作备忘,也作为分享!
Similarities between Scala and Java
Following are some of the major similarities between Scala and Java programming language :
1) Both are JVM based language, Scala produce same byte code as Java and runs on Java Virtual Machine. Similar to Java compiler javac, Scala has a compiler scalac, which compiles Scala code into byte code. At this level, all JVM language like Groovy, JRuby, Scala becomes equals to Java, because they use same memory space, type system and run inside same JVM.
2) You can call Scala from Java and Java from Scala, it offers seems
less integration. Moreover, you can reuse existing application code and
open source Java libraries in Scala.
3) Major Java programming IDE like Eclipse, Netbeans and InetelliJ supports Scala.
4) One more similarity between Scala and Java is that both are Object Oriented, Scala goes one steps further and also supports functional programming paradigm, which is one of it's core strength.
Differences between Scala and Java
1) First and Major difference you will notice between Scala and Java is succinct and concise code. Scala drastically reduce number of lines from a Java application by making clever use of type inference, treating everything as object, function passing and several other features.
2) Scala is designed to express common programming patterns in elegant,
concise and type-safe way. Language itself encourage you to write code
in immutable style, which makes applying concurrency and parallelism
easily.
3) One difference, which some might not notice is learning curve. Scala
has steep learning curve as compared to Java, my opinion may be slightly
biased because I am from Java background, but with so much happening
with little code, Scala can be really tricky to predict. Syntax of Scala
looks confusing and repulsive as compared to Java, but I am sure that
is just the starting hurdle. One way to overcome this hurdle is
following a good Scala book like Programming in Scala or Scala in Action, both are excellent books for a Java developer, who wants to learn Scala
4) One of Scala's cool feature is built-in lazy evaluation, which allows
to defer time consuming computation, until absolutely needed and you
can do this by using a keyword called "lazy" as shown in below code :
// loading of
image is really slow, so only do it if need to show image
lazy val images =
getImages() //lazy
keyword is used for lazy computation
if(viewProfile){
showImages(images)
}
else(editProfile){
showImages(images)
showEditor()
}
else{
// Do something
without loading images.
}
If you love to learn by following examples, then I guess Scala CookBook
is a another good buy, contains tons of examples on different features
of Scala.
5) Some one can argue that Java is more readable than Scala, because of
really nested code in Scala. Since you can define functions inside
function, inside another functions, inside of an object inside of a
class. Code can be very nested. Though some time it may improve clarity,
but if written poorly it can be really tricky to understand.
6) One more difference between Scala and Java is that Scala supports Operator overloading. You can overload nay operator in Java and you can also create new operators for any type, but as you already know, Java doesn't support Operator Overloading.
7) Another major difference between Java and Scala is that functions are objects in Java. Scala treats any method
or function as they are variables. When means, you can pass them around
like Object. You might have seen code, where one Scala function is
accepting another function. In fact this gives the language enormous
power.
8) Let's compared some code written in Scala and Java to see How much different it look:
Java:
List<Integer>
iList = Arrays.asList(2,
7, 9,
8, 10);
List<Integer>
iDoubled = new ArrayList<Integer>();
for(Integer number:
iList){
if(number
% 2 == 0){
iDoubled.add(number * 2);
}
}
Scala:
val iList = List(2, 7,
9, 8,
10);
val iDoubled = iList.filter(_
% 2 == 0).map(_ * 2)
【注】: 上面的代码,原贴中在标记红色部分是有错误的,估计是typo吧,不管如何,出于技术探索,还是要调试过的代码,不要错代码,至少不要明显有错的代码!
You can see that Scala version is lot succinct and concise than Java
version. You will see more of such samples, once you start learning functional programming
concepts and patterns.
That's all on this article about similarities and differences between Scala and Java. Though they are two separate programming language,
they have lot in common, which is not a bad thing at all and in my
opinion that's the only thing, which will place Scala as Java
alternative, if at all it happens in future.
以上是 scala vs java 相同点和差异 的全部内容, 来源链接: utcz.com/z/391573.html