In the last article, we learnt about some basic concepts of Kotlin like what is Kotlin, features of Kotlin, how to declare variables and data types in Kotlin. Before reading this article make sure you had read the last article Basics of Kotlin- Part 1. Let’s learn more about Kotlin.
Type Conversion is a procedure of converting one type of data variable into another data type. It is also known as Type Casting. Unlike Java, Kotlin does not support the implicit conversion of a smaller data type into a larger data type. It means we can not assign Int into Long or Double.
long b=a; //no error
var b:Long=a //compiler error Type mismatch
However, we can do the explicit conversion with the help of predefined helper functions. The list of helper functions used for numeric conversion in Kotlin is given below:
var b:Long=a.toLong() //compiles successfully no error
Operators are the special characters or symbols which performs the operation on operand(value or variable). There is various kind of operators in Kotlin:
Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) and modulo(%)
print(a+b) // Output : 16
print(a-b) // Output : 6
print(a*b) // Output : 55
print(a/b) // Output : 2
print(a%b) // Output : 1
Relation Operator is used to compare two values, two variables or two numbers. It always gives output as true or false.
print(a<b) //Output : false
print(a>b) //Output : true
print(a<=b) //Output : false
print(a>=b) //Output : true
print(a==b) //Output : false
print(a!=b) //Output : true
Assignment Operators are used to assigning the arithmetic operated values. The assignment of value takes from right to left.
var x = 10
var y = 2
var x + = y // Output : 12
Var y - = x // Output : 8
Var x * = y // Output : 20
Unary operators are used with only a single operand.
In pre increment/decrement the value will be updated before assigning to the variable and in post increment/decrement the value will be updated after assigning to the variable
print(a++) //Output : 10
print(a) //Output : 11
print(++a) //Output : 12
Logical Operators are used to check conditions between operands.
print((a>b)&&(a>c)) //Outout : false
print((a>b)||(a>c)) //Output : true
print(!flag) //Output : false
Kotlin uses the standard library function readLine() which read the line of string input from the standard input stream.
var name=readLine() //For string input
If we want the input of other data types, then we can use
Scanner object. For that, we need to import
Scanner class from Java standard library using:
Then, we need to create
Scannerobject from this class.
val scannerObj= Scanner(System.`in`)
Now we can use this scannerObj for taking the input from the user
var name=scannerObj.next() // For String input
var age = scannerObj.nextInt() // For Integer input
var charInput=scannerObj.next().single() //For char input
var doubleInput=scannerObj.nextDouble() //For double input
Comments in Kotlin:
Comments are used for making the source code easier for humans to understand. They provide explanatory information about the source code. Comments are ignored by the compiler so they will not execute. Kotlin has two types of comments:
- Single Line: Single line comment is used for commenting single line of the statement.
- Multi-line: Multi-line comment is used for commenting multiple lines of the statement.
//This is single line comment/* This is
That’s it for this article. We will continue in the next article.