Kotlin Learning Series (2)-Built-in types

1. Basic type

KotlinWith respect Javafor less, Javain addition to the basic package as well as the type of class, but in Kotlinthe wrapper classes are
Insert picture description here
above picture is very obvious comparison here is not to say, if we say that the next in Kotlinhow to declare variable

1.1 Declare variables

In the kotlintwo variables are declared valandvar
Insert picture description here

What is the difference between the two? That is to say, the valdeclared variable cannot be changed when we use it below. It varcan be valdeclared , and the declaration is similar Javato the finaldecoration in ours.As
Insert picture description here
we see in the figure below , oh, The Kolinnumber of characters in a variable declared in is actually Javamore! Actually it is not, here we will say Kotlinthe type derivation in the next , the type of Kotlinthis variable can be derived from the value on the right side of the equal sign, so it is actually unnecessary for me to put Type it out, like the following
Insert picture description here

1.2 LongType tags that are confusing

In Javadeclaring a Longtype of variable in, we usually add Lor after the value l, but this lis easily confused with uppercase Ior uppercase 1, Kotlinand it must be used Lin .You can lnot use lowercase to compile.
Insert picture description here

1.3 Numeric type conversion

In Javawe can assign a Inttype of variable to a Longtype of variable, just use the =number, but Kotlinit is not possible in the middle, we must call its conversion method.We
Insert picture description here
directly use the =number compiler to directly prompt an error, then what should we do Do?
Insert picture description here

1.4 Unsigned types

There Javais no one among us, but Cthere is, and there is this type of KotlincompatibilityC
Insert picture description here

1.5 String

It's basically Javathe same as the one in here.

1.5.1 """ """Declaration string

KotlinWe can use three double quotes in the declaration string, what we declare here, what it prints
Insert picture description here

1.5.2 String templates

The string template starts with a dollar sign $and contains any expressions in variable names or curly braces.
Insert picture description here
Insert picture description here

1.5.3 String comparison

JavaUse equalsand to compare two strings in ==, kotlinrespectively corresponding to ==and===
Insert picture description here

Array

Interval

set

function

Published 87 original articles · Like 319 · Visit 1.49 million +

Guess you like

Origin blog.csdn.net/Greathfs/article/details/104593787