val与var
- val为常量名
- var为变量名
数据类型
- Byte/Char
Short/Int/Long/Float/Double
Boolean - 数字默认是Double类型,1.1为Double,1.1f为Float
- 类型转换:10.asInstanceOf[Double]
- 判断类型:10.isInstanceOf[Int]
lazy
lazy val a = 1,一般用在耗时计算或者网络请求上面
import scala.io.Source._
lazy val info = fromFile("/User/admin/Desktop/hello.txt").mkString
当使用info常量的时候,才会调用fromFile的方法
开发工具
- idea
- 下载idea的scala插件
- maven依赖
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
- 代码编译
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>compile-scala</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile-scala</id>
<phase>test-compile</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
网友评论