1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FlutterエンジニアがJetpack Composeを学んでみた(varとval編)

Posted at

FlutterエンジニアがKotlinのJetpack Composeを学んでみることにしました。その際に、Flutterだとこのような使い方をすると言う理解をしてみると、理解しやすいのではないかなと思い、記事を書いてみました。

とても簡単な内容となっていると思いますが、Flutterエンジニアが理解しやすいものになれればなと思っています!(逆にAndroidエンジニアがFlutterを学習できるといいなと思っております!!)

varとvalについて

Kotlin には変数を定義するためのキーワードとして、val と var が用意されています。

val

・再代入できない参照を保持するための変数(immutable reference)
・Flutterで言うところのfinalになる。

var

・再代入可能な変数 (mutable reference)
・Flutterで言うところのvarになる。(同じ)

使用例

Kotlin

val a = 1
val name: String = "John"

a = 2 //NG(Val cannot be reassigned)
name = "Ryutaro" //NG(Val cannot be reassigned)

var b = 1
b = 2 //OK

Flutter

final b = 1;
final user = "taro";

b = 2; //NG
user = 'ken';//NG

var a = 1;

a = 2;//OK

参考文献

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?