1. Operators

 

Operators

 ----  val   ----

With a val type you can assign a value once. If you try to assin something again, you can get an error.

For example, if you have a dog called Maxwell, you can not call him John because his name is Maxwell.




 

---- var ----


With a var type the values can change to the same type of values.

For example,  this kind of example shows an amount of money and money can change (increase or decrease),






Note: If the type was already defined, you can not change the type value.



Also, number types won't implicitly convert to other types, so you can not assign a short value to a long variable or a byte to an int. Kotlin does this because implicit numerical conversion is a common source of errors in programs. 



But you can always assign them by casting like this: 








Kotlin supports underscores in numbers, so you can specify long constants in a format that makes sense to you. 








Also, you can specify thousands using underscores: 







---- Nullability ----

Kotlin helps avoiding null pointer exceptions when you declare a variable type explicitly by default, it's value can not be null.

So, you can use the question mark operator to indicate that a variable can be null.



When you have a complex data types such as a list, you can allow for the list to be null, but if it's not null, it's element cannot be null or you can allow bot the list o the elements to be null
Or you can allow both elements of the list to be null

If you really love no pointer exceptions, kotlin will let you keep them. The not know assertion operator can force your way past a nullable type in Kotlin. Though, it will still throw an an exception when it's null. In programming slang, the exclamation mark is often called, Bang. Its a lot easier to say double bang than not null.

Note: It is generally a bad idea to use the double bang operator and thats why they 've made you type two exclamation marks instead of one. You can do some cool null testing with the question mark operator saving you the pain of many if else statements.


In Kotlin, "!!" is an operator that is known as the double-bang operator. This operator is also known as "not-null assertion operator". This operator is used to convert any value to a non-NULL type value and it throws an exception if the corresponding value is NULL.

At he end of the code we will throw a NULL Pointer Exception at runtime










Practice Time: Basic Operations


If you start with 2 fish, and they breed twice, producing 71 offspring the first time, and 233 offspring the second time, and then 13 fish are swallowed by a hungry moray eel, how many fish do you have left? How many aquariums do you need if you can put 30 fish per aquarium?

Solution: 2.plus(71).plus(233).minus(13).div(30).plus(1)


Practice Time: Variables

  1. Create a String variable rainbowColor, set its color value, then change it.
  2. Create a variable blackColor whose value cannot be changed once assigned. Try changing it anyway.
var rainbowColor = "red" rainbowColor = "blue" val blackColor = "black" blackColor = "white" // Error

Practice Time: Nullability

  1. Try to set rainbowColor to null. Declare two variables, greenColor and blueColor. Use two different ways of setting them to null.
var greenColor = null var blueColor: Int? = null

Practice Time: Nullability/Lists

  1. Create a list with two elements that are null; do it in two different ways.
  2. Next, create a list where the list is null.
listOf(null,null) [null, null] var list: List, = listOf(null, null) var list2:List,? = null

Practice Time: Null Checks

  1. Create a nullable integer variable called nullTest, and set it to null. Use a null-check that increases the value by one if it's not null, otherwise returns 0, and prints the result.

Hint: Use the Elvis operator.

println(nullTest?.inc() ?:0)

Comments

Popular posts from this blog