Scala learning (1, environment configuration and basics)

1. Environment configuration
Suggestion: Use idea
online compiler
1. Download scala
2. Configure in environment variables, plus this
Insert picture description here
3. Configure java jdk etc. (I have configured it before)
4. File -> settings in idea, open The following interface

Insert picture description here
Just download it.
When creating a scala program, you need to add an environment, just create and download it directly.
5. The first program

object HelloWorld {
    
    
    /* 这是我的第一个 Scala 程序
     * 以下程序将输出'Hello World!'
     */
    def main(args: Array[String]) {
    
    
      println("Hello, world!") // 输出 Hello World
    }
  }

2. Basic
1. Constant: val
2. Variable: var

object Main {
    
    
  def main(args:Array[String])
  {
    
    
	  val a = "123"  //这个没有指定变量类型,就和python一样编译器会自己认识。
      println(a)
	  var name :String = "1234"   //这是指定变量类型
	  println(name)
  }	
}

3. Common data types: Byte, Short, Int, Long, Float and Double and a Boolean.
ps: Attention: toString() and to() functions
4. Arithmetic and overloaded identifiers The
regular ones are the same as the Java we are in contact with, but Scala can operate like function calls:

object HelloWorld {
    
    
  
    def main(args: Array[String]) {
    
    
      println(1.+(2))
    }
  }

5. Function call without parameters can be without parentheses

object HelloWorld {
    
    
    def main(args: Array[String]) {
    
    
      println("Hello".distinct)//获取字符中不重复的字符
    }
  }

6. The apply function can be omitted

object HelloWorld {
    
    
    def main(args: Array[String]) {
    
    
      println("Hello"(4))//拿到在第四个内存的数据,从0开始算的
      println("Hello".apply(4))
    }
  }

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/112726551