Scala学习笔记-基本数据类型

Scala-基本数据类型

这里写图片描述


scala支持八种基本数据类型,同时利用隐式转换,对基本数据类型进行了增强,下面就来介绍一下scala中的基本数据类型。


scala基本数据类型说明

scala中的基本数据类型同java的基本数据类型有所不同,scala没有区分基本类型和包装类型,而是统一定义为class类,注意这里的类不同于java中的包装类型,这仅仅是scala的一个普通类而已,scala编译器在翻译的过程中,会将其转换成java中的基本数据类型,就像下面这样:

class Test {
    val a: Byte = 0
}

被翻译成如下java代码:

public class Test
{
  private final byte a = 0;
  public byte a() { 
      return this.a;
  }
}

scala中包含的基本数据类型详情如下表所示:

序号基本类型增强类型大小取值范围
1ByteRichByte8-bit-128 ~ 127
2CharRichChar16-bit0 ~ 65535
3ShortRichShort16-bit-32768 ~ 32767
4IntRichInt32-bit-2^31 ~ 2^31 -1
5LongRichLong64-bit-2^63 ~ 2^63 -1
6FloatRichFloat32-bit
7DoubleRichDoube64-bit



其中RichXX是对于基本数据类型的扩展,提供了常用的操作基本数据类型的方法,当在基本数据类型上使用那些没有提供的方法时,scala就会自动应用 隐式转换 将其转换成对用的增强类型,并从中寻找被应用的方法,以Int为例,看一下源码中的注释:


/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// DO NOT EDIT, CHANGES WILL BE LOST
// This auto-generated code can be modified in scala.tools.cmd.gen.
// Afterwards, running tools/codegen-anyvals regenerates this source file.

package scala

/** `Int`, a 32-bit signed integer (equivalent to Java's `int` primitive type) is a
 *  subtype of [[scala.AnyVal]]. Instances of `Int` are not
 *  represented by an object in the underlying runtime system.
 *
 *  There is an implicit conversion from [[scala.Int]] => [[scala.runtime.RichInt]]
 *  which provides useful non-primitive operations.
 */
final abstract class Int private extends AnyVal {
  def toByte: Byte
  def toShort: Short
  def toChar: Char
  ...


可以从类的说明中看到,scala为Int添加了从Int到Rich的隐式转换。

代码示例

下面来看一些简单的例子

package my.scala.basictype

/**
  * Created by zhangdong on 2016/4/30.
  */
object Test1 extends App{

   /** 
     * Byte, Char,  Short, Int, ,Long, Float, Double, Boolean
     * */

    //Byte   -128 - 127
    val byte_1: Byte = 65
    println(byte_1.toChar)  // A
    //toInt 为RichByte提供的方法
    println(byte_1.toInt.getClass.getSimpleName) // int



   //Short  -32768 - 32767     2^15
   println("------------short------------")
   val short_1: Short = 10000
   println(short_1)    //10000
   println(short_1.getClass.getSimpleName) //short
   //RichShort
   println(short_1.toByte)  //16



   //Char  0 - 65535     2^16 - 1
   println("-----------char-------------")
   val char_1: Char = 10009
   //println((Character.MIN_VALUE + 0) + "-" + Character.MAX_VALUE)
   println(char_1)   // ✙
   //RichDouble
   println(char_1.toByte) // 25
   println(char_1.toDouble) //10009.0


   //Int   -2^31 - (2^31 - 1)
   println("-----------Int---------------")
   val int_1: Int = 2000
   println(int_1)
   //RichInt
   println(int_1.toShort) //2000
   println(int_1.toDouble) //2000.0

   val int_2 = - 190
   println(int_2.abs)  //求绝对值
   println(int_2.byteValue) //66 , -190超出byte范围
   println(int_1.compare(11145))   //-1
   println(int_2.compareTo(-823))  // 1
   println((int_1.compare(2000)))  //0

   println(int_2.isValidShort) //true
   println(int_1.isValidByte)  //false 不在byte范围内

   println(int_1.max(20001))  //20001  返回较大的
   println(int_1.min(19000))  //2000   返回自己


  val to_1 = 10 to (100, 4)
  println(to_1)  //包括100

  val until_1 = 10 until(100, 3)  //不包括100
  println(until_1)

  //Long  -2^63 - (2^63 - 1)
  //Float -2^63 - (2^63 - 1)
  //Double 

}

没有列举的可查看官方文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值