Tag Archives: kotlin tutorial

Android Pong

In this tutorial we will create the world oldest game, the Pong, but in our case we give it the name Android Pong. We will use the Android Canvas to draw first a black background rectangle and this will give space for our Android Pong game.

Android Pong is a very simple game, as you can see on the picture. It will have at the left top corner a text. This will show the current points. Next to the text will be the start position of our ball. The player shape is a rectangle at the bottom of the playground.

Continue reading Android Pong

Follow and like us:

Break Expression

Suppose that you are working with loops. It is sometimes desirable to terminate the loop immediately without checking the test expression. In such case, break is used. It terminates the nearest enclosing loop when encountered (without checking the test expression).

How break works?

Almost always it is used with if..else statements. For example: If expression is evaluated to true, break is executed which terminates the for loop.

Continue reading Break Expression

Follow and like us:

Recursion and Tail Recursion

A function that calls itself is known as recursive function. This technique is known as recursion. A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

How does recursion work in programming?

Here, the recurse() function is called from the body of recurse() function itself. Here’s how this program works.


Example

Here, the recursive call continues forever causing infinite recursion. To avoid infinite recursion, if…else (or similar approach) can be used where one branch makes the recursive call and other doesn’t.

Continue reading Recursion and Tail Recursion

Follow and like us:

Continue Expression

Suppose you are working with loops. It is sometimes desirable to skip the current iteration of the loop. In such case, continue is used. The continue construct skips the current iteration of the enclosing loop, and the control of the program jumps to the end of the loop body.

How continue works?

Almost always it is used with if..else statements. For example: If the expression is evaluated to true, continue is executed which skips all the codes inside while loop after it for that iteration.

Continue reading Continue Expression

Follow and like us:

Data Class

While building any application, we often need to create classes whose primary purpose is to hold data/state. These classes generally contain the same old boilerplate code in the form of getters, setters, equals(), hashcode() and toString() methods.

Kotlin has a better solution for classes that are used to hold data/state. It’s called a Data Class. A Data Class is like a regular class but with some additional functionalities.

With Kotlin’s data classes, you don’t need to write/generate all the lengthy boilerplate code yourself. The compiler automatically generates a default getter and setter for all the mutable properties, and a getter (only) for all the read-only properties of the data class. Moreover, It also derives the implementation of standard methods like equals(), hashCode() and toString() from the properties declared in the data class’s primary constructor.

Example Customer data class


Syntax of data class

Continue reading Data Class

Follow and like us: