[JAVA] Introduction to kotlin for iOS developers ④-Type

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.

Declaration of constants and variables

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

Type of type

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

Type inference

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.

Type specification

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. スクリーンショット 2017-03-14 0.13.11.png 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, スクリーンショット 2017-03-14 0.12.25.png I was angry that the queryEditText method text was "Unresolved reference".

スクリーンショット 2017-03-14 0.34.02.png 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.

Type conversion

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()

Null safe

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.

Please also include other articles

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

Introduction to kotlin for iOS developers ④-Type
Introduction to kotlin for iOS developers ⑥-Kotlin creation
Introduction to kotlin for iOS developers ③-About gradle
Introduction to kotlin for iOS developers ①-Environment construction
Introduction to kotlin for iOS developers ②-Project creation
Kotlin Class to send to Java developers
Introduction to Programming for College Students: Introduction
Generics of Kotlin for Java developers
Kotlin Class part.2 to send to Java developers
Introduction to java for the first time # 2
Introduction to Programming for College Students: Variables
Interoperability tips with Kotlin for Java developers
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Memo for migration from java to kotlin
Introduction to Ruby 2
Kotlin functions and lambdas to send to Java developers
Introduction to Ratpack (Extra Edition) --Ratpack written in Kotlin
[Introduction to Java] About type conversion (cast, promotion)
Initial settings for rewriting Java projects to Kotlin
Introduction to web3j
Getting started with Kotlin to send to Java developers
[Java] Introduction to Java
Introduction to migration
Introduction to Doma
An introduction to Groovy for tedious Java engineers
[Introduction to Java] Basics of java arithmetic (for beginners)
Introduction to programming for college students (updated from time to time)
Introduction to Java for beginners Basic knowledge of Java language ①
Introduction to Programming for College Students: Making a Canvas
I want to create a generic annotation for a type
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Introduction to JAR files
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to design patterns (introduction)
[Java, Kotlin] Type Variance
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
How to type backslash \
An introduction to functional programming for object-oriented programmers in Elm
Introduction to Programming for College Students: Preparation Let's Install Processing
[IOS14] How to get Data type image data directly from PHPickerViewController?
Introduction to batch files (for statements, arrays, deferred environment variables)
Introduction to Programming for College Students: How to Draw Basic Rectangle
Introduction to Programming for College Students: Draw a Straight Line