#4 Exploring Kotlin -Type Conversion, Operators

Type Conversion:

Arun Pandian M
4 min readMar 12, 2019

Type Conversion is a process of converting one data type to another data type. Kotlin does not support implicit type conversion like java.
If you do the implicit Type conversion it will make compile error.

In Java implicit conversion

int intValue = 55;
long longValue = intValue;

In kotlin:

var intValue: Int = 1039094
var longValue: Long = intValue //type mismatch error

below functions supported to type conversion:

  • toByte()
  • toShort()
  • toInt()
  • toLong()
  • toFloat()
  • toDouble()
  • toChar()

Type Conversion Examples:

//int to long conversion
val intValue = 22
val longValue = intValue.toLong()
// double to int
val doubleValue = 22.22
val intValue = doubleValue.toInt()
// int to string
val intValue = 22
var stringValue =intValue.toString()
//string to int
val strValue = "22"
val intValue = strValue.toInt()
val strValue = "22arun"
val intValue = strValue.toInt() // throwing NumberFormatException

Operators:

An Operator manipulates individual operands and returns a result. The data Operators are represented by special characters or by keywords.

Ok, let’s see about various Operators supported by Kotlin.

Following Operators are offered by kotlin:

  • Unary Operator
  • Arithmetic Operator
  • Relation Operator
  • Assignment Operator
  • Bitwise Operator
  • Logical Operator

Unary Operator

The unary Operator is manipulating with the only a single operand and give the result. Following are some unary Operator given below

  1. unary plus (+)
+a // 
  • corresponding function is a.unaryPlus()

2.unary minus

-a
  • corresponding function is a.unaryMinus()

3.unary increment

  • ++increment by 1
++a
  • corresponding function is a.inc()

3.unary decrement

  • — decrement by 1
--a
  • corresponding function is a.dec()

Arithmetic Operator

An arithmetic Operator is a mathematical function that takes two operands and performs a calculation on them.

1. + Operator

a+b
  • plus
  • corresponding function is a.plus(b)

2. - Operator

a-b
  • minus
  • corresponding function is a.minus(b)

3. * Operator

a * b
  • times
  • corresponding function is a.times(b)

4. / Operator

a/b
  • div
  • corresponding function is a.div(b)

5. % Operator

a%b
  • mod
  • corresponding function is a.mod(b)

Relation Operator

Relation Operator is used to comparing the two operands and return the result Following are some relational Operators

1. > Operator

a > b
  • greater than
  • corresponding function is a.compareTo(b) > 0

2 . < Operator

a < b
  • less than
  • corresponding function is a.compareTo(b) < 0

3. >= Operator

a >= b
  • greater than or equals to
  • corresponding function is a.compareTo(b) >= 0

4. <= Operator

a <= b
  • less than or equals to
  • corresponding function is a.compareTo(b) <= 0

5. == Operator

a==b
  • is equal to
  • corresponding function is a?.equals(b) ?: (b === null)

6. != Operator

a!=b
  • not equal to
  • corresponding function is !(a?.equals(b) ?: (b === null))

Assignment Operator

Assignment Operators are used to assign value to a variable. we already know about “=”. Its exactly same like Java. Followings are Assignment Operator:

  1. += Operator
a+=b 
  • equal to a = a + b
  • corresponding function is a.plusAssign(b)

2. -= Operator

 a -= b
  • equal to a = a -b
  • corresponding function is a.minusAssign(b)

3. *= Operator

a *= b
  • equal to a = a * b
  • corresponding function is a.timesAssign(b)

4. a /= b Operator

a /= b
  • equal to a = a /b
  • corresponding function is a.divAssign(b)

5. %= Operator

a %= b
  • equal to a = a % b
  • corresponding function is a.modAssign(b)

Bitwise Operator:

Bitwise and bit shift Operators are used on only two integral types (Int and Long) to perform bit-level operations.

  1. shl
  • shifts bit pattern to the left by a certain number of specified bits
  • corresponding function 1.shl(2)
1 shl 2 // equals 1.shl(2) = 4

4.inv() // -5

2. shr

  • shifts bit pattern to the right by a certin number of specified bits.
  • corresponding function 2.shr(3)
2 shr 3 // equals 2.shr(3) = 0

3. and

  • Compares corresponding bits of two values. If both bits are 1, it is evaluated to 1. If either of the bits is 0, it is evaluated to 0.
  • corresponding function 2.and(4)
2 and 4 // equals 2.and(4) = 0

4. or

  • Compares corresponding bits of two values. If either of the bits is 1, it gives 1. If not, it gives 0.
  • corresponding function is 1.or(5)
1 or 4 // equals 1.or(4) = 5

5. xor

  • Compares corresponding bits of two values. If corresponding bits are different, it gives 1. If corresponding bits are same, it gives 0.
  • corresponding function is 5.xor(4)
5 xor 4 // equals 5.xor(4) = 1

6. inv

  • Inverts the bits in this value.
4.inv() // -5

Logical Operator:

  • It represents booleans and has two values: true and false like java.
  • Kotlin has following Operators

1.|| (Logical OR)

  • The result of the operation “operand1 || operand2” will be

1. “true” if any of the operands (operand1 or operand2) is “true” or both the operands (operand1 or operand2) is “true”.

2. “false” only if both operand1 and operand2 are “false”

(9 > 15 || 15 < 13)  //false

2. && (Logical And)

  • The result of the operation “operand1 && operand2” will be

1. “true” only if both operand1 and operand2 are “true”

2. “false” if any of the operands (operand1 or operand2) is “false” or both the operands (operand1 or operand2) are “false”.

5 > 2 && 4 == 4 // true

3.! (Logical NOT)

  • The result of the boolean value will reverse the condition of the value
  1. If the value is “true” then it will be false
  2. If the value is “false” then it will be true
!(17 < 27 ) //false

I’m off ! bye bye. I will come with packages imports and control flow in Kotlin in my next post. If you have any feedbacks or doubts, please leave that as a comment.

And if you really liked this post and want to make me feel happy, don’t forget to 👏!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arun Pandian M
Arun Pandian M

Written by Arun Pandian M

Senior Android developer at FundsIndia, A time investor to learn new things about Programming. Currently in a relationship with Green Bug(Android).

No responses yet

Write a response