DEV Community

Cover image for Basics of Kotlin - Part 2
Chetan
Chetan

Posted on • Updated on

Basics of Kotlin - Part 2

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.

Kotlin Type Conversion:

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.

In Java:

int a=10;
long b=a; //no error
Enter fullscreen mode Exit fullscreen mode

In Kotlin:

var a=10;
var b:Long=a //compiler error Type mismatch
Enter fullscreen mode Exit fullscreen mode

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:

  • toInt()
  • toByte()
  • toShort()
  • toChar()
  • toLong()
  • toFloat()
  • toDouble()
For example:
var a=10;
var b:Long=a.toLong() //compiles successfully no error
Enter fullscreen mode Exit fullscreen mode

Operators:

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:

Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) and modulo(%)
image

Example:
var a=11
var b=5
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
Enter fullscreen mode Exit fullscreen mode

Relation Operators:

Relation Operator is used to compare two values, two variables or two numbers. It always gives output as true or false.
image

For example:
 var a=11
 var b=5
 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
Enter fullscreen mode Exit fullscreen mode

Assignment Operators:

Assignment Operators are used to assigning the arithmetic operated values. The assignment of value takes from right to left.
image

For example:
var x = 10
var y = 2
var x + = y // Output : 12
Var x - = y // Output : 8
Var x * = y // Output : 20
Enter fullscreen mode Exit fullscreen mode

Unary Operators:

Unary operators are used with only a single operand.
image

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

var a=10
print(a++) //Output : 10
print(a) //Output : 11
print(++a) //Output : 12
Enter fullscreen mode Exit fullscreen mode

Logical Operators:

Logical Operators are used to check conditions between operands.
image

For example
var a=10
var b=5
var c=16
print((a>b)&&(a>c)) //Outout : false
print((a>b)||(a>c)) //Output : true
var flag=true
print(!flag) //Output : false
Enter fullscreen mode Exit fullscreen mode

Input in Kotlin:

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
Enter fullscreen mode Exit fullscreen mode

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:

import java.util.Scanner
Enter fullscreen mode Exit fullscreen mode

Then, we need to create Scannerobject from this class.

val scannerObj= Scanner(System.`in`)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
   multiline
   comment */
Enter fullscreen mode Exit fullscreen mode

That's it for this article. We will continue in the next article.
Happy Learning!

Top comments (0)