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….


This content originally appeared on DEV Community and was authored by Chetan

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

In Kotlin:

var a=10;
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:

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

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

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

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 y - = x // Output : 8
Var x * = y // Output : 20

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

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

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

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

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
   multiline
   comment */

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


This content originally appeared on DEV Community and was authored by Chetan


Print Share Comment Cite Upload Translate Updates
APA

Chetan | Sciencx (2021-04-23T18:31:28+00:00) Basics of Kotlin- Part 2. Retrieved from https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/

MLA
" » Basics of Kotlin- Part 2." Chetan | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/
HARVARD
Chetan | Sciencx Friday April 23, 2021 » Basics of Kotlin- Part 2., viewed ,<https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/>
VANCOUVER
Chetan | Sciencx - » Basics of Kotlin- Part 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/
CHICAGO
" » Basics of Kotlin- Part 2." Chetan | Sciencx - Accessed . https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/
IEEE
" » Basics of Kotlin- Part 2." Chetan | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/. [Accessed: ]
rf:citation
» Basics of Kotlin- Part 2 | Chetan | Sciencx | https://www.scien.cx/2021/04/23/basics-of-kotlin-part-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.