Skip to content

Data Type

Rajesh Khadka edited this page Apr 20, 2019 · 1 revision

Data Types

Primitive

Data type which directly contains value: byte, short, int, long, float, double, char, boolean

Data types Description Examples of Literals Size in Byte Range Minimum Range Maximum
byte 8 bit signed integer 64, 0, -1, 60634 1 -128 +127
short Represents a 16-bit signed integer 23, 32727 2 -32,768 +32,767
int Represents a 32-bit signed integer 42, -8, 0 4 -2,147,483,648 +2,147,483,647
long Represents a 64-bit signed integer 42, -8, 0 8 -9x10^18 9x10^18
float Represents a single-precision 32-bit 2.1f, -8f, 0f 4 +- 10^-45, 7 +- 10^38
double Represents a double-precision 64-bit floating point number 2.1f, -8f, 0f 8 +- 10^-324 +- 10^308

Non-Primitive Data Type

Data type which holds the reference of the object which was located on the heap. It doesn't hold directly the value rather addresses of the value located in heap. Not-Primitive Data type is classes: Integer, String, Double, etc.

Memory Allocation of Primitive and Non-primitive Data Type

https://www.scientecheasy.com/2018/06/memory-allocation-primitive-nonprimitive.html

Wrapper classes

Data Type Wrapper Class
int Integer
long Long
byte Byte
double Double
float Float
short Short

Widening and Narrowing

Widening

Small primitive data type value is automatically transformed in wider primitive data type. int -> long, short -> int etc.

     var b: Short = 32323
     var c: Int = 234324234
     c = b.toInt()

Narrowing

casting wider primitive type value to a smaller primitive type value.

    var a: Byte = 127
    var b: Short = 32323
    a = b.toByte()

Clone this wiki locally