本贴是我摘抄自国外网站,用作备忘,也作为分享!

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 相同点和差异的更多相关文章

  1. Scala For Java的一些参考

          变量 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java ...

  2. 使用Scala实现Java项目的单词计数:串行及Actor版本

    其实我想找一门“具有Python的简洁写法和融合Java平台的优势, 同时又足够有挑战性和灵活性”的编程语言. Scala 就是一个不错的选择. Scala 有很多语言特性, 建议先掌握基础常用的: ...

  3. scala调用java的方法,返回了一个对象链表List<Student>,在scala中遍历该链表获取指定Student的名字name

    假设Student类如下: class Student { private int no; private String name; public int getNo() { return no; } ...

  4. Spark:用Scala和Java实现WordCount

    http://www.cnblogs.com/byrhuangqiang/p/4017725.html 为了在IDEA中编写scala,今天安装配置学习了IDEA集成开发环境.IDEA确实很优秀,学会 ...

  5. IDEA15使用maven编译scala和java

    机器安装maven,在IDEA中配置maven的home 创建项目:new-maven–> scala-archetype-simple project structure–>创建src. ...

  6. C#与Java的语法差异

    C#与Java的语法差异C与Java的语法差异前言程序结构基本语法数据类型字符串变量与常量运算符判断语句循环语句访问权限方法数组结构枚举类继承多态运算符重载接口命名空间预处理器指令正则表达式异常IO泛 ...

  7. scala 与 java 之间的关系

    scala来源于java,但又高于java. scala的设计者Martin Odersky就是一个JAVA控,这位牛人设计了javac和编写了jdk中的通用代码.可以说java语言本身就是Marti ...

  8. 编写Spark的WordCount程序并提交到集群运行[含scala和java两个版本]

    编写Spark的WordCount程序并提交到集群运行[含scala和java两个版本] 1. 开发环境 Jdk 1.7.0_72 Maven 3.2.1 Scala 2.10.6 Spark 1.6 ...

  9. Scala IDEA for Eclipse里用maven来创建scala和java项目代码环境(图文详解)

    这篇博客 是在Scala IDEA for Eclipse里手动创建scala代码编写环境. Scala IDE for Eclipse的下载.安装和WordCount的初步使用(本地模式和集群模式) ...

随机推荐

  1. QQ截图取色方法

    转自:http://www.oicqzone.com/qqjiqiao/2014110920194.html ctrl+alt+a截图的时候,会显示RGB值.是的,你也许会想,但是我要的是#RRGGB ...

  2. 兼容性所有浏览器的透明CSS设置

    兼容所有浏览器的透明CSS设置: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...

  3. 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. matlab 已知函数值纵坐标值(Y值)获得对应的横坐标

    clear all;clc; x=-pi/2:pi/50:pi; y=sin(x); plot(x,y); grid on; fm=max(y) id=find(y==fm); xm=x(id) 转自 ...

  5. C特殊浮点值NaN

    特殊浮点值NaN(Not-a-Number),例如asin()函数返回反正弦值,所以输入参数不能大于1,否则函数返回NaN值,printf()显示为nan,NaN或类似形式.

  6. 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题

    如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...

  7. 单元测试--四则运算2程序(c++)

    源代码: //2016 3.6 Cheng Qiqin //四则运算改进 #include <iostream> #include<ctime> #include<cst ...

  8. c++将引用作为函数的参数---6

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量别名.这种传递参数 的方法称为按引用传递. ...

  9. JavaScript个人学习记录总结(二)——验证表单输入之模式匹配

    该示例检查从文本窗口部件中获取姓名和电话号码这两个表单数据的有效性.当文本框中的值发生变化时,即引发一个change事件,从而可以调用一个函数来检查这两个输入值的格式是否正确. validator.h ...

  10. HDU-1011 Starship Troopers (树形DP+分组背包)

    题目大意:给一棵有根带点权树,并且给出容量.求在不超过容量下的最大权值.前提是选完父节点才能选子节点. 题目分析:树上的分组背包. ps:特判m为0时的情况. 代码如下: # include<i ...