Kotlin. Basic types. Numeric literals. Numeric constants. Examples

Basic types. Numeric literals. Numeric constants. Examples


Contents


Search other websites:

1. Basic types classification

To use some variable (object) in the program, you need to correctly describe the type of this variable. According to the described type, the purpose of the variable in the program is determined. In the Kotlin language, 5 types of basic types are distinguished:

  • numeric types (numbers);
  • logical type Boolean;
  • character type Char;
  • string type String;
  • array – the Array type.

 

2. Classification of numeric types. The characteristics of types

Among the basic types, the largest set are numeric types, which are divided into two groups:

  • integer types: Byte, Short, Int, Long;
  • floating poing types (real types): Float, Double.

 

2.1. Integer types

The following table summarizes the characteristics of integer types.

Kotlin. Characteristics of integer types.

If an integer (literal) appears in the program, then the following rules apply to it:

  • if the value of the number does not exceed the range of values of type Int, then the type of this number is set to Int;
  • if the value of the number exceeds the range of values of the Int type, then the type of this number is set to Long.

If you need to set any integer (even a small one) as a Long type, then the suffix L is added to this number. For example, 23L, 232323L, 1000023939L.

Integer literal examples

25L
288
-3203
-209320302233L

 

2.2. Floating point types

For floating point numbers, the following table is typical.

Kotlin. Floating point types: Double, Float

According to the IEEE 754 standard, floating point numbers are characterized by the following statements:

  • Float type provides single precision;
  • Double type provides double precision.

The delimiter in floating point numbers is the character ‘ . ‘ (point).

Examples of floating point numbers

28.5     - Double
188.323F - Float
22.0f    - Float

 

3. Examples of declaring and using integer types

Example 1. Squaring a variable of the integer type Int.

fun main(args: Array<String>) {
  // Squaring an integer variable
  // 1. Declare a variable of type Int
  val n : Int

  // 2. Input n
  print("n = ")
  n = readLine().toString().toInt()

  // 3. Calculate n*n
  val square_n = n*n

  // 4. Display the result
  println("n*n = " + square_n)
}

The result of the program

n = 35
n*n = 1225

Example 2. Raise the number n to the power of 5 and print the result. The types Int, Long are used.

fun main(args: Array<String>) {
  // Raising a number to the power of 5
  // 1. Declare variables of type Int and Long
  val n : Int
  val res : Long // Result

  // 2. Input n
  print("n = ")
  n = readLine().toString().toInt()

  // 3. Calculate n^5
  val pow5 : Long
  pow5 = n.toLong()*n.toLong()*n.toLong()*n.toLong()*n.toLong()

  // 4. Display the result
  println("n^5 = " + pow5) // n^5 = 10000000000
}

 

4. Examples of declaring and using floating point types

Example 1. The program for calculating the area of a circle by the entered radius. Demonstration of using the Float type.

// Include the math library.
// Needed to use the PI constant in the program.
import kotlin.math.*

fun main(args: Array<String>) {

  // The program for calculating the area of a circle by the entered radius
  // 1. Declare variables
  var s:Float // area to be calculated
  var r:Float // radius

  // 2. Input radius
  print("r = ")
  r = readLine().toString().toFloat()

  // 3. Calculate the area
  s = (PI*r*r).toFloat()

  // 4. Display the result
  println("s = " + s)
}

Example 2. Program for calculating the roots of a quadratic equation. The Double type is used as a base. The program inputs the coefficients of the equation a, b, c.

fun main(args: Array<String>) {
  // Calculate the roots of a quadratic equation
  // 1. Declare variables of Double type
  val a:Double
  val b:Double
  val c:Double
  val D:Double
  val x1:Double
  val x2:Double

  // 2. Input a, b, c
  print("a = ")
  a = readLine().toString().toDouble()
  print("b = ")
  b = readLine().toString().toDouble()
  print("c = ")
  c = readLine().toString().toDouble()

  // 3. Calculate the discriminant
  D = b*b-4*a*c

  // 4. Calculate the roots
  if (D<0)
    println("The equation has no roots.")
  else
  {
    // Use function kotlin.math.sqrt()
    x1 = (-b-kotlin.math.sqrt(D))/(2*a)
    x2 = (-b+kotlin.math.sqrt(D))/(2*a)
    println("x1 = " + x1)
    println("x2 = " + x2)
  }
}

The result of the program

a = 1
b = 1
c = -2
x1 = -2.0
x2 = 1.0

 

5. Representation of numerical constants in hexadecimal

Integer constants can be represented in the base 16 number system. In this number system, the following are used to denote a number:

  • numbers from 0 to 9;
  • Latin letters A, B, C, D, E, F or a, b, c, d, e, f.

Each hexadecimal constant starts with a 0x character pair.

Example. Declaring integer constants in hexadecimal notation

val h1 = 0x0A5 // h1 = 165 
val h2 = 0x0f8 // h2 = 248

In decimal notation, the value of h1 is 165. Also, the value of h2 is 248.

 

6. Representation of numerical constants (literals) in the binary system

Integer constants can be represented in binary notation. To distinguish the binary system, each of the constants begins with a pair of characters 0b or 0B. Given that the constants are in the binary system, then the value of the digits can be 0 or 1.

Example.

// The constant in the binary system
val max = 0b11111111 // max = 255 in the decimal system
println("max = " + max)

val min = 0B1000 // min = 8
println("min = " + min)

The code below will throw the error “The value is out of range”

val errorValue = 0b00213 // Error - only digits 0 or 1

 

7. Representing numeric constants in a more readable form. The ‘_’ character

If the program uses numeric constants with a large number of decimal places, then you can set these constants in a convenient form, separating the digits with the ‘_’ character.

Example 1. Ways to represent the number 1,000,000,000 (one billion):

// Normal representation
var tt = 1000000000

// Representation using underscores
tt = 1_000_000_000 // this is more comfortable

tt = 1000_000_000 // one thousand million

tt = 1_000_0_00_0_00 // so too can

Example 2. Ways to represent binary 15

// Normal representation
var bin_number = 0b1111

// Representation using underscores
bin_number = 0b1_1_1_1
bin_number = 0b11_11

 

8. Constants MIN_VALUE, MAX_VALUE. Permissible limits of numeric types

For any numeric type (Int, UByte, Double, …), the constants MIN_VALUE and MAX_VALUE are defined, containing the minimum possible and maximum possible value of the numeric type.

For example, to determine the minimum possible value of the Short type in a Kotlin program, you need to write the following expression

Short.MIN_VALUE

The value for the Short.MIN_VALUE constant is -32768.

In the same way, you can define limit values of any numeric type: Int, Long, Double, ULong, and the like.

 

9. Displaying valid values of floating point types. Example

The example displays the limit values of the Float and Double types.

fun main(args: Array<String>) {
  // Print the minimum and maximum values of floating point types
  // 1. The Float type
  val minFloat : Float
  val maxFloat : Float
  minFloat = kotlin.Float.MIN_VALUE
  maxFloat = Float.MAX_VALUE
  println("minFloat = " + minFloat)
  println("maxFloat = " + maxFloat)

  // 2. The Double type
  val minDouble : Double = Double.MIN_VALUE
  val maxDouble : Double = Double.MAX_VALUE
  println("minDouble = " + minDouble)
  println("maxDouble = " + maxDouble)
}

The result of the program

minFloat = 1.4E-45
maxFloat = 3.4028235E38
minDouble = 4.9E-324
maxDouble = 1.7976931348623157E308

 

10. An example of using unsigned integer types. Output of boundary values of unsigned types.

The example displays the minimum and maximum allowable value of unsigned UByte types.

fun main(args: Array<String>) {
  // Print the minimum and maximum value of unsigned integer types
  // 1. Тип UByte
  val minUByte : UByte
  val maxUByte : UByte
  minUByte = UByte.MIN_VALUE
  maxUByte = UByte.MAX_VALUE
  println("Type UByte: min = " + minUByte + ", max = " + maxUByte)

  // 2. The UShort type
  val minUShort : kotlin.UShort // could be so
  val maxUShort : UShort
  minUShort = UShort.MIN_VALUE
  maxUShort = kotlin.UShort.MAX_VALUE // could be so
  println("Type UShort: min = " + minUShort + ", max = " + maxUShort)

  // 3. The UInt type
  val minUInt : UInt = UInt.MIN_VALUE
  val maxUInt : UInt = UInt.MAX_VALUE
  println("Type UInt: min = " + minUInt + ", max = " + maxUInt)

  // 4. The ULong type
  val minULong : ULong = ULong.MIN_VALUE
  val maxULong : ULong = ULong.MAX_VALUE
  println("Type ULong : min = " + minULong + ", max = " + maxULong)
}

The result of the program

Type UByte: min = 0, max = 255
Type UShort: min = 0, max = 65535
Type UInt: min = 0, max = 4294967295
Type ULong : min = 0, max = 18446744073709551615

 


Related topics