Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 23, 2021 06:31 pm GMT

Basics of Kotlin- Part2

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=11var b=5print(a+b) // Output : 16print(a-b) // Output : 6print(a*b) // Output : 55print(a/b) // Output : 2print(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 = 10var y = 2var x + = y // Output : 12Var y - = x // Output : 8Var 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=10print(a++) //Output : 10print(a) //Output : 11print(++a) //Output : 12

Logical Operators:

Logical Operators are used to check conditions between operands.
image

For examplevar a=10var b=5var c=16print((a>b)&&(a>c)) //Outout : falseprint((a>b)||(a>c)) //Output : truevar flag=trueprint(!flag) //Output : false

Input inKotlin:

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 inputvar age = scannerObj.nextInt() // For Integer inputvar charInput=scannerObj.next().single() //For char inputvar doubleInput=scannerObj.nextDouble() //For double input

Comments inKotlin:

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!


Original Link: https://dev.to/csj5483/basics-of-kotlin-part-2-51o3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To