table of Contents
type of data
Basic type | Type description |
---|---|
Byte | An 8-bit signed two's complement integer. Value range is -128 to 127 |
Short | 16-bit signed two's complement integer. The value range is -32768 to 32767 |
Int | 32-bit signed two's complement integer. The value range is -2147483648 to 2147483647 |
Long | 64-bit signed two's complement integer. The value range is -9223372036854775808 to 9223372036854775807 |
Char | 16-bit unsigned Unicode character, the range value is U+0000 to U+FFFF |
String | Char type sequence (string) |
Float | 32-bit, IEEE 754 standard single-precision floating-point number |
Double | 64-bit IEEE 754 standard double-precision floating-point number |
Boolean | true or false |
The data types listed in the above table are all objects, which means that Scala does not have the native types in Java. In scala, it is possible to call methods on basic types such as numbers.
Pay attention to the difference between scala types and Java:
All types in scala start with a capital letter.
Int is used for shaping instead of Integer
. Variables defined in scala can be defined without writing types, so that the scala compiler automatically infers
that the default integer type in Scala is Int, by default The floating point type is: Double
scala type hierarchy
Type hierarchy description:
Types of | Description |
---|---|
Any | The parent class of all types, it has two subclasses AnyRef and AnyVal |
AnyVal | The parent class of all numeric types |
AnyRef | The parent class of all object types (reference types) |
Unit | Represents empty. Unit is a subclass of AnyVal. It has only one instance of (), which is similar to void in Java, but scala is more object-oriented than Java |
Null | Null is a subclass of AnyRef, which means it is a subclass of all reference types. Its instance is null. Can assign null to any object type |
Nothing | Subclasses of all types cannot directly create instances of this type. When a method throws an exception, it returns the Nothing type. Because Nothing is a subclass of all classes, it can be assigned to any type. We programmers do not use Nothing, this is maintained by the system itself. . |
The following code will report an error
val b:Int = null
val a:Double = null
The Null class is a type of null reference object, which is a subclass of every reference class (class inherited from AnyRef). Null is not compatible with value types.
Type conversion
Overview
When a Scala program is performing an operation or assignment action, the data type value with a small range is automatically converted to a data type value with a large range, and then the calculation is performed. For example, the result of the operation of 1 + 1.2 is a Double type of 2.2, and some At that time, we will involve some actions similar to "rounding", to convert a decimal into an integer and then calculate, these contents are type conversion in Scala.
Type conversion in scala is divided into type conversion of value types and type conversion of reference types. Here we first focus on: Type conversion of value types. Type conversion of value types is divided into:
- Automatic type conversion
- Forced type conversion
Automatic type conversion
A data type value with a small range will be automatically converted to a data type value with a large range. This action is called: automatic type conversion. The
automatic type conversion from small to large is:
Byte, Short, Char -> Int -> Long -> Float -> Double
Sample code
val a:Int = 6
//因为是int类型和double类型的值进行计算, 所以最终结果为: Double类型
val b:Double = 6 + 6.3
//这样写会报错, 因为最终计算结果是Int类型的数据, 将其赋值Byte类型肯定不行.
val c:Byte = a + 1
Forced type conversion
A data type value with a large range can be converted into a data type value with a small range through a certain format (coercion function). This action is called: coercion.
Note: The use of forced type conversion may cause a loss of precision!
var name = 1
1.name.toDouble
Use toDouble, toInt and other methods, object name.to method can realize simple data type conversion.
2.name.doubleValue
Three methods, doubleValue, longValue, and intValue can be used for simple data type conversion.
3.Integer.valueOf(name)
Integer or string type can be converted to Integer type.
4.String.valueOf(name)
Any type of data can be converted to string type (including character array)
For more complex and specific conversions, you need to customize the conversion type and method, and use placeholders to convert all elements in the data set in a specified way.