This time, I will vaguely look at the handling and types of variables and constants while comparing how to write them in swift, java, and kotlin.
At the time of declaration, add "let" and "var" in swift, the type name of the constant / variable in Java, and "val" and "var" in kotlin.
declaration.swift
let value1 = "HelloWorld" //constant
var value2 = 1 //variable
declaration.Java
final String value1 = "HelloWorld"; //constant
int value2 = 1; //variable
(* I am not aware of class constants this time.)
declaration.kt
val value1 = "HelloWorld" //constant
var value2 = 1 //variable
The main point is the comparison of Int, Double, String, and Bool types. Please note that there are some parts that are slightly different, such as "int" in Java but "Int" in kotlin.
type.swift
let num1: Int
let num2: Double
let text: String
let isTrue: Bool
type.Java
final int num1 = 1;
final Double num2 = 0.0;
final String text = "HelloWorld";
final boolean isTrue = true;
type.kt
val num1: Int = 1
val num2: Double = 0.0
val text: String = "HelloWorld"
val isTrue: Boolean = true
Of the three, type inference does not work in Java only. In the case of Java, as mentioned above, if you do not specify the type at the time of declaration, you will get angry, so inference does not come into play.
There is also a type specification in kotlin. But type inference works, so basically you don't have to be aware of it. If you have Java experience, it may be unpleasant to not specify the type at the time of declaration, but I think that it is okay to prioritize your own comfort.
The only exception is when you declare a part to be displayed on the screen if you do not specify the type. Now, I intentionally removed the type specification in a certain project. Then, it seems that you can not understand what this queryEditText is at once, I was angry that the queryEditText method text was "Unresolved reference".
If you do this with Xcode, you will still get angry. So, at the very least, make it a habit to specify the type when declaring a part.
This time, as a sample, let's look at the conversion between String type and Int type.
conversion.swift
let value1 = "100"
var value2 = 1
let convert1 = Int(value1)
let convert2 = "\(value2)"
conversion.Java
String value1 = "100";
int value2 = 1;
int convert1 = Integer.parseInt(value1);
String convert2 = String.valueOf(value2);
conversion.kotlin
var value1 = "100"
var value2 = 1
var convert1 = Integer.parseInt(value1)
var convert2 = value2.toString()
In Java, everything has the potential to be null. So when I referred to it, "actually it was null"-> "nullpo" often occurred, but kotlin creates a type called "nullable type" and does not accept null at all other than that. Thats how its going to be. Thanks to the nullable type, it prevents nulls from appearing in unexpected places.
In other words, it's almost the same as the Optional type in swift. Although there are some differences in notation, they can be treated in almost the same way.
Introduction to kotlin for iOS developers ①-Environment construction Introduction to kotlin for iOS developers (2) -Project creation Introduction to kotlin for iOS developers ③-About gradle Introduction to kotlin for iOS developers ④-Type [Introduction to kotlin for iOS developers ⑤-Practical XML] (http://qiita.com/parappa1002/items/867c5b30055312e74fdb) [Introduction to kotlin for iOS developers ⑥-kotlin creation] (http://qiita.com/parappa1002/items/9f898feb4f83e672b384)
Recommended Posts