Skip to main content

Kotlin Data Type

Kotlin Data Type
Data type (basic type) refers to type and size of data associated with variables and functions. Data type is used for declaration of memory location of variable which determines the features of data.
In Kotlin, everything is an object, which means we can call member function and properties on any variable.
Kotlin built in data type are categorized as following different categories:

Number
Character
Boolean
Array
String

Number Types
Number types of data are those which hold only number type data variables. It is further categorized into different Integer and Floating point.

Byte 8 bit -128 to 127
Short 16 bit -32768 to 32767
Int 32 bit -2,147,483,648 to 2,147,483,647
Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38
Double 64 bit 4.94065645841246544e-324 to 1.79769313486231570e+308
Char 4 bit -128 to 127


val value1 = 'A'
//or
val value2: Char
value2= 'A'

Boolean Data Types
Boolean data is represented using the type Boolean. It contains values either true or false.

Boolean 1 bit true or false

Array
Arrays in Kotlin are represented by the Array class. Arrays are created using library function arrayOf() and Array() constructor. Array has get (), set() function, size property as well as some other useful member functions.
Creating Array using library function arrayOf()
The arrayOf() function creates array of wrapper types. The item value are passed inside arrayOf() function like arrayOf(1,2,3) which creates an array[1,2,3].
The elements of array are accessed through their index values (array[index]). Array index are start from zero.

val id = arrayOf(1,2,3,4,5)
val firstId = id[0]
val lasted = id[id.size-1]

Creating Array using Array() constructor
Creating array using Array() constructor takes two arguments in Array() constructor:

First argument as a size of array, and Second argument as the function, which is used to initialize and return the value of array element given its index.

val asc = Array(5, { i -> i * 2 }) //asc[0,2,4,6,8]

String
String in Kotlin is represented by String class. String is immutable, which means we cannot change the elements in String.

String declaration:
val text ="Hello, JavaTpoint"
Types of String

String are categorize into two types. These are:
1. Escaped String: Escape String is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' etc.

val text1 ="Hello, JavaTpoint"
//or
val text2 ="Hello, JavaTpoint\n"
//or
val text3 ="Hello, \nJavaTpoint"

2. Raw String: Row String is declared within triple quote (""" """). It provides facility to declare String in new lines and contain multiple lines. Row String cannot contain any escape character.

val text1 ="""Welcome To JavaTpoint"""

Comments

Popular posts from this blog

BCA SEM 5 SHELL SCRIPT PROGRAM

1  Write a shell script to execute following commands 1. Sort file abc.txt and save this sorted file in xyz.txt 2. Give an example of : To execute commands together without affecting result of each other. 3. How to print “this is a three –line 1. Text message” 4. Which command display version of the UNIX? 5. How would u get online help of cat command? echo “sorting the file” sort abc.txt > xyz.txt echo “executing two commands” who ; ls echo “this is \n a three-line \n Text message” # use -e option if required echo “The version is `uname -a`” echo “Help of cat command” man cat 2  Write a shell script to execute following commands 1. How would u display the hidden files? 2. How delete directory with files? 3. How would user can do interactive copying? 4. How would user can do interactive deletion of files? 5. Explain two functionality of “mv” command with example? echo “1. How would u display the hidden files” echo “2. How delete directory with files” echo “3. How

C++

I NTRODUCTION TO OOP,CLASSES & OBJECTS 1. Use of scope Resolution of Operators. 2. Define a function outside a using scope resolution operators. 3. Write a program to calculate the area of circle, rectangle and square using function overloading. 4. Write a program to calculate the area of circle, rectangle and square using with class & object. 5. Write a program to demonstrate the use of returning a reference variable. 6. Create a class student,stores the details about name,roll no,marks of 5 subject,1.get function accept value of data members,2. display function to display,3.total function to return total of 5 subjects marks. 7. Create function power() in c++. & Create function power() in c++ and default argument. 8. Write a C++ program to swap the value of private data members from 2 different classes. 9. Write a program to illustrate the use of this pointer. 10. An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is do

USER- DEFINED FUNCTIONS

1. Write a program to calculate average temperature of five days.Create temp() function . 2. Write a program that uses recursive function fibo() thatgenerates a Fibonacci series containing N elements. 3. Write a program that uses a recursive function fact() that findsthe factorial of a given number N. 4. Program to find if the given no. is prime or not. The functionshould accept the number as argument and return if the no. isprime or not. 5. Write a function that accepts an array of integer values. Thefunction should find the number which divides all the othernumbers. 6. Write a user-defined function to perform Square of a number 7. Write a user-defined function to perform Area of a number 8. Write a user-defined function to perform Reverse the number 9. Write a program that uses a function to check whether anentered three digit number is palindrome or not. 10. Write a program to calculate the result of following with recursive calls offunction. 11. Simple interset with usin