Social Icons

Saturday, May 19, 2018

Kotlin Variables

1 - Declarations of variables In Kotlin

we distinguish the variables whose value we can change, and the immutable variables that are not quite constants: the keyword val allows to declare an immutable variable, the var keyword is used to declare an alterable variable.
val name: String = "Michael"
// name = "Walid" // Forbidden !!! Because name has been declared with val
var age = 5
age + = 10 // no problem because age is alterable.
As a reminder, we must specify the type of variable after the name, not before as in Java. Obviously, an immutable variable must be initialized when it is declared. This works equally well for "primitive" types (everything is object in Kotlin) as for personal types.
class Person (val name: String, var age: Int = 10)

val Claude: Person = new Person ("Jim", 25)
Even if classes in Kotlin will be explained later, here the Person class has a read-only name property, and a read-write age property, which defaults to 10. (Note however, here the class Nobody has not defined a modifying method, but if it were, it would also have been probable that the variable Claude could see its modified state even though it was declared as val! Here the word -val val just prevents the object pointed by Claude from changing.)

2 - Basic types

The Byte, Short, Int, Long, Float, and Double numeric types are equivalent to the existing types in Java: except that, as a reminder, in Kotlin, everything is object. So we can write:
3.toString ()
7.downTo (0) // generates the range (ie the interval) decreasing from 7 to 0.
The Boolean type also works as in Java:
val under18 = age <18
val isLucas = name == "Lucas"
val iFoundHim = under18 && isLucas
The String type has the same methods as in Java, but also additional methods (complete list here). So we can write:
println ("martin" .all {it.isLowerCase ()}) // Test if the whole word is lowercase.
The all method, which tests whether all the elements of a collection fulfill a criterion, does not exist in Java: it is an extension of the Kotlin language. The piece of code constituted by the braces is an anonymous function (or lambda): it will be discussed later. Kotlin also adds the type Range (interval), very useful. Thus one can execute a loop thanks to a Range object (indeed, the basic Java syntax to perform a loop does not exist in Kotlin):
for (i in 3..7) {
    println (i)
} // Show numbers from 3 to 7 on multiple lines.
But we can also get a descending interval with downTo:
for (i in 10 downTo 0) {
    println (i)
} // Count down from 10 to 0, on multiple lines.
It is also possible to specify a progression other than 1:
for (i in 2..36 step 3) println (i) // Display the numbers 2,5,8, ..., 35
It is also possible to combine a descending interval and a defined progression:
for (i in 36 downTo 2 step 3) println (i) // Display the numbers 36,33, ..., 3
We will come back later in this chapter on the for loop. Also know, as we will see later with the functions, the type void does not exist in Kotlin, but we use instead the type Unit (which does not apply only to functions). Remember that you can see the methods available for a given object in the Kotlin Playground or in the Intellij Idea IDE with the CTRL + ESPACE command after entering the point next to the object. 

Younes Derfoufi 

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (169) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (33) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (140) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates