跳过正文
  1. 博客/
  2. 后端/
  3. 框架/

Learning Scala From Java

3 分钟· ·
后端 框架 Java
作者
Allen
一个强大、轻量级的 Hugo 主题。
目录

Recently I had finshed reading one book : « Scala for The Impatient», I found a lot intrest thing during reading this book.Cause I had known Python and Java before, I can see mixed fetures of Scala between Python and Java.This blog is my thought of studying a new language from other learned language.

Introudction
#

What is Scala, Scala is a new lanuage ? No technically Scala is Java. They both create *.class file, and run on JVM.The most different between them: Scala use .scala as suffix and Java use .java as suffix.

The difference of two lanuage is the syntax which deeply affectting your coding style.This blog will introuce the most different of two lanuage and we will find out why is it.

Stronger Object-Oriented
#

Scala and Java both generate Java bytecode(we will call it bytecode later) file, and let it run on the JVM.The bytecode is totally a object-oriented format.

The basic structure of bytecode is class, but we will see something wrong in Java.For examples.In Java library, they give us two type int and Integer. One is C level data type, Other is a really Object Class.This is a huge problem, do we really need a Object Class like Integer, this Class make a C level data type not so effective (It’s huge than int).The only reason we need it for collection data type like List,Set,Map and so on can only load Class Object.So we not only make it ugly in Java but also we waste lot of time packing and unpacking between Class-Object and C-level Object.

This problem is fixed or improve by Scala.In Scala libray, there are no int or long or char anymore.There are all to be Class.And in Scala offical collection (scala.collection) we can load this Class-Object as a c-level data type undergroud. This trick is done by Manifest type, using reflect to make the collections to save base type like intchar and so on as its’ elements.

In this way, we not only delete c-level data type, but also delete the un-object-oriented part in a object-oriented lanuage.This is improvement of Scala.We needn’t pack and unpack again and again.But we share the speed of C level data type without touch it.

Enhance Function
#

Java8 support lambda fuction in 2014,so in a long time if you want use something like function, you can use a anonymous class which implemented some interface.
eg:

    Thread thread = new Thread(new Runnable() {
  
        @Override
  
        public void run() {
  
            // do something     
  
        }
  
    });
  

You can see if we want run a thread we must initialize one object.Actually you only need the run function.

Let’s see what Python do with function.If you want to call one object as function you only need give a object __call__ function.Then you can call as obj(*arg, **kwds).The Python will transform it to obj.__call__(*arg, **kwds).

We can learn it from Python, if Java want to call obj as function so he need use some unnified rule.So when you can write just function and Java will turn it to some Interface.

Scalabuild 23 generic Class from Function0 to Function22 to help us write our function as a object.By the way, in Java, you maybe use avoid in a method to announcing no nedd to return.In Scala, you must have a return, if you really don’t need one, just return Unit which is same as void.

Now you can use fuction as a veriables now in Scala

def rInt(): Int = 1
  
val k: () => Int = rInt _
  

In upper, we use fuction rInt as a virable of k, () => Int is tell the Scala the type of function.So when call some thing like k(), it will tranceform to k.applay().

Scala build a rule for us like HTTP to Internet.Maybe this is not so meaning just building 23 trait (like interface in Java).But you will a huge power when on the basement.It kind like HTTP, thanks to it, the Internet give a amazing world to us.

Do More
#

Scala is shorter than Java.In my word, I think Java is kind of wordy.If you want to print some word, you nedd use System.out.println, maybe you can static import to reduce it(use import static System.out).Also there are too mush strict rule in Java.Such as: Only one public class in a file.Your package need use same physcial address.etc.

Java is strict lanuage, you aren’t trust by the compiler.While Scala give us more freedoom.You can save a lot of time in it.

Let us see what Scala do for us.

  • add varies and method in builder

just use one line like

class T(var name:String)
  

it’s equal

class T{
  
private String name;
  
public String getName(){
  
return this.name;
  
}
  
public String setName(String name){
  
this.name = name;
  
}
  
public T(String name) {
  
this.name = name;
  
}
  
}
  
  • saving brackets

if a fuction use no params, you can save brackets.It maybe confused if you want a function not just call them.Just add _ after your function.Scala will know you just need a function.

  • use symbol as your function

In Java, we are not allowed to use *,/+= as your class method, Scala open it, you can what you like.Sometimes, + will be more clearly than just add.

  • add your patch to other library

In the your kingdom of Scala, you can simple add any patch to other class without recompiling Java again.

  implicit def addKing(w:String) = new {
  
	def king()= println(w + " is my king")
  
  }
  

  
  def say = "Scala" king
  

just add one rule use implicit to tell Scala, you can add String a new method king.if you run say, it will print Scala is my king.

Compare Java, Scala more like a human, he will think a lot for your code.If String have no method call king, he will look up in his scope, is there any definenation of a convertion to a new Class which have a method call king.If he find one, he will do it for you, convert the String to the new Class and call the method again.


Summary
#

There are so many secret in Scala, if you realy want to know how Scala do for us, just use scalac to compile to .class file, and use javap to tranceform to Java code.That will help you a lot.There are also a intresting part in new power switch in Scala, it’s too important to write a new blog to introducing it.

Scala is a intersting lanuage, if you want to know more about it, you can read the Scala library code .It’s really perfect.

相关文章

Java的char类型到底几个字节
6 分钟
后端 框架 Java
从例子里解Spring IOC
7 分钟
后端 框架 Java SpringBoot
触摸Python的GIL
18 分钟
后端 框架 Python
如何让你的Python更快
11 分钟
后端 框架 Python
千万级数据处理小结
8 分钟
后端 框架 大数据
从关系角度来看XPath
5 分钟
后端 框架 Python