About Android basic grammar

I think the development languages are mainly Java and Kotlin,

This time, we will focus on "Kotlin" and publish it.

As a feature,

■ Data can be omitted

For example, code that assigns a string to the following variable.

val a : String = "Hello World"

Furthermore, the type inference function allows the type specification to be omitted as follows.

val a = "Hellow World"

■ null safety

null is ... a special value that means nothing

Nullable types have a "?" After the type name, such as val a: String? = Null. ], You can substitute Null.

val b : String? = null
b?.toUpperCase() 

Note: toUpperCase () is a function that can only be called with a String variable.

■ Concise access to classes

For Java, the fields need getter and setter access, but Kotilin is not required.

■ println function The println function outputs only the character string specified in the argument.

println("Hello,World")

■ Nullable type

val a : String? = null
a?.toUpperCase()

■ Unacceptable type

val a : String? = null
a!!.toUpperCase()

■ Difference between val and var

The variable declared with val is an immutable variable and its value cannot be changed later.
Variables declared with var are mutable variables whose values can be changed later.

□ Example

val a = 100
a = 1000
>error:val cannot be reassigned
var b = 10
b = 100
println(b)
>100

It is a good idea to think about var and val and use them properly.

■ Object

Multiple data types are prepared according to the value to be stored in the same size as Java.

Integer is Byte,Short,Int,4 types of Long.
Floating point types that include a decimal point are Float,Two types of Double.
Boolean is true with Boolean,Represents a false divalent.
Char type characters are single quotes(')。
String type string is double quoted(")

□ Example

The variable a is declared in the Short type and 10 is assigned as the initial value.

The next line uses the is operator to ask if the variable a is of type Short.

val a : Short = 10
a is Short
>true

If you omit the type specification and use the type inference function, it will be as follows.

val a = 123
a is Int
>true

Since 123 can be represented by 8 bits (1 byte), all integer types should be candidates.

For the following integer values, add A to make it a Long type.

val a = 123A
a is Long
>true

Then floating point

val a = 123.456
a is Double
>true

If you add F to the variable a,

val a = 123.456F
a is Float 
>true

It becomes a Float type.

Declare a Boolean variable b and try substituting true and false.

var b : Boolean = true
b = false
b is Boolean
>true

The Char type is enclosed in a single quarter, and the String type is enclosed in a double quote.

val d = 'abc'
d is Char
>true
val d = "a"
d is String
>true

■ Data type conversion

Java can convert implicit data types, but Kotlin cannot convert implicit data types.

In the case of Kotlin, there is a [to type name ()] method for data type conversion.

val a : Short = 10
var i : Int
i = a.toInt()
print(i)
>10

Try converting a string to a number

val str = "100"
val a = str.toInt()
print(a)
>100
  • In Kotlin, use [to type name ()] for data type conversion.

■ Array

Kotlin's array becomes an Array class, which is created and initialized by the arrayof function and arrayofNulls function.

val Number = arrayOf<Int>(0,2,4,6,8,10)
print(Number[0])
>0

Following arrrayOf, is called a type argument.

You can access the elements of the array by the array name [Index].

(The index is also called a subscript and starts at 0, so the variable Number returned 0.)

Using type inference:

val Numbers = arra>Of(1,3,5,7,9,11)
print(Numbers[4])
>9

■ List

The function of using multiple objects together is called "collection".

  • In Kotlin, list sets and maps are called collections.
List is immutable.
val ints : List<Int> = listOf<Int>(1,3,5,7,9)
print(ints)
print(ints[3])
print(ints.get(0))
print(ints.size)
>[1,3,5,7,9]515

You can access the elements of the list either by using an index like an array or by specifying the index in the get () method.

size returns the size of the list (the number of elements).

List cannot be omitted, but listOf can be omitted.

MutableList is mutable.
val ints : MutableList<int> = mutablelistOf(1,3,5,7,9)
ints[2] = 4
print(ints[1])
ints.add(11)
print(ints)
>3[1,3,4,7,9,11]
val ints : MutableList<int> = mutablelistOf(1,3,5,7,8)
ints.remove(4)
ints.removeAt(2)
print(ints)
>1,3,5

MutableList is generated by the mutablelistOf function.

The remove () method removes the matching element.

The removeAt () method removes by element number.

MutableList allows you to [Add] elements, [Change] values, and [Delete] elements.

This time, we will introduce you so far!

If you have any suggestions, please let me know! I look forward to working with you next time.

Recommended Posts

About Android basic grammar
Java basic grammar
Java basic grammar
Java basic grammar
Java basic grammar
About Android App Components
About the Android life cycle
JavaScript overview and basic grammar
10 Things I Hate About Android
[Android / Java] Learned about DataBinding
Review of Ruby basic grammar
About the basics of Android development
Memorandum (Ruby: Basic grammar: Iterative processing)
[Rails] About helper method form_with [Basic]
About =
Basic basis of Android asynchronous processing "AsyncTask"
Introduction to Ruby basic grammar with yakiniku
Memorandum (Ruby: Basic Grammar: Classes and Instances)
About UI thread processing in Android asynchronous
[Java] I personally summarized the basic grammar.
[Basic knowledge of Java] About type conversion
Basic Java grammar you should know first
I have a question about Android studio.