Kotlin's simple basic syntax

Introduction

Instead, the field decided to write an Android application in Kotlin. I used to write in Java a lot I will leave a note of how to write the basic syntax in Java and Kotlin as a memo when I forget how to write it in the future

Premise

--Check operation with paiza

output

#Call println from the System class
System.out.println("output");
#Call println
println("output")

Comment out

//1 line comment out
/*
Anything in this will be commented out
*/

Mold

Java Kotlin
Double 64-bit floating point number(Used in lowercase) 64-bit floating point number
Float 32-bit floating point number(Used in lowercase) 32-bit floating point number
Long 64-bit signed integer(Used in lowercase) 64-bit signed integer
Int 32-bit signed integer(Used in lowercase) 32-bit signed integer
Short 16-bit signed integer(Used in lowercase) 16-bit signed integer
Byte 8-bit signed integer(Used in lowercase) 8-bit signed integer
Char Character type representing one character(Used in lowercase) Character type representing one character
Boolean Boolean value(Used in lowercase) Boolean value
String String String

variable

String name = "ryo chiba"
var name:String = "ryo chiba"

constant

//Add final to make it a constant(Cannot be changed)
final String name = "ryo chiba"
name = "chiba ryo" //Get an error
fun main(args: Array<String>) {
    // Your code here!
    Constants.name
    Constants.name = "chiba ryo" //error
}
//It becomes a constant by adding const to the companion object.
class Constants {
  companion object {
    const val name = "ryo chiba"
  }
}

Array

Enter 1,2,3 int type value

#Pattern 1
int[] no = {1, 2, 3};

#Pattern 2
int foo[] = new int[3];
no[0] = 1;
no[1] = 2;
no[2] = 3;
#pattern 1
val no: IntArray = intArrayOf(1, 2, 3)

#Pattern 2
val no: MutableList<Int> = mutableListOf()
no.add(1)
no.add(2)
no.add(3)

if statement

int no = 1;
if(no > 0){
  System.out.println("0 or more");
} else {
  System.out.println("0 or less");
}
var no:Int = 1
if(no > 0){
  println("0 or more")
} else {
  println("0 or less")
}

#Can also be treated as an expression
var no:Int = 1
val anser = if (no > 0) { 
  "0 or more"
} else { 
  "0 or less" 
}

Switch(when)

int no = 1;
switch (no){
  case 0:
    System.out.println("0");    
    break;
  case 1:
    System.out.println("Is 1");
    break;
  default:
    System.out.println("Other than 0 and 1");  
}
var no:Int = 1
when (no) {
  0 -> print("0")
  1 -> print("Is 1")
  else -> print("Other than 0 and 1")
}

for

#for
for(int i=0; i<3; i++){
  System.out.println(i);
}

#For statement in an array
int[] no = {1, 2, 3};
for (int data: no){
  System.out.println(data);
}
#for
for (no in 1..3){
  println(no)
}

#For statement in an array
val no: IntArray = intArrayOf(1, 2, 3)
for (data in no) {
  print(data)
}

while

int no=0;
while(no < 10) {
  no++;
  System.out.println(no);
}
var no:Int = 0
while (no < 10) {
  x++
  println(no)
}

At the end

This time, I tried to summarize only the syntax that seems to be used often, but there are many writing styles and usages that are not yet available. Also, I would like to summarize what I learned from actually using Kitlin. I also hope that someone who has forgotten how to write in Kotlin will remind me of this article: blush:

Recommended Posts

Kotlin's simple basic syntax
ruby basic syntax memo
Java basic syntax + α trap