当前位置:网站首页>Android development with Kotlin programming language - basic data types
Android development with Kotlin programming language - basic data types
2022-08-05 10:57:00 【AaVictory.】
I. Introduction
Kotlin: A programming language widely used by Android developers around the world
Second, variable declaration
1. Kotlin uses two different keywords
- val : for variables whose value never changes.Variables declared with val cannot be reassigned.
- var : for variables whose value can be changed.
2. Examples
//num is a variable of type Int, the initial value is 10var num: Int = 10//The value of num can be changed from 10 to 15num=15Int is a type that represents integers and is one of many numeric types that can be represented in Kotlin.Similar to other languages, you can also use Byte, Short, Long, Float, and Double, depending on your numeric data.
- Assume there is a String named name.If you want to ensure that the value of name is always "Kotlin", you can declare name using the val keyword:
val name: String = "Kotlin"These keywords allow you to specify which variables can have their values changed.Please use it as needed.If the referenced variable must be reassignable, declare it as var.Otherwise, use val.
Third, type inference
val name= "Kotlin"Since the value of "Kotlin" is of type String, the compiler infers that name is also String.Note that Kotlin is a statically typed language.This means, the type will be resolved at compile time and will never change.
Using Kotlin's type inference to ensure code simplicity and type safety
Four. Null Safety
In some languages, it is possible to declare a reference type variable without explicitly providing an initial value.In such cases, the variable usually contains a null value.By default, Kotlin variables cannot hold null values.This means that the following code snippet is invalid:
val name:String= null- For a variable to hold a null value, it must be of a nullable type.You can designate a variable as nullable by suffixing the variable type with a ?, as shown in the following example:
val name:String? = null- After specifying the String? type, you can assign a String value or null to name.
Nullable variables must be handled with care or the dreaded NullPointerException may occur.For example, in Java, if you try to call a method on a null value, the program will crash.
边栏推荐
- Go compilation principle series 6 (type checking)
- L2-042 老板的作息表
- 【深度学习】mmclassification mmcls 实战多标签分类任务教程,分类任务
- ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
- SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
- hdu4545 魔法串
- What are the standards for electrical engineering
- Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
- 例题 可达性统计+bitset的使用
- 微信小程序标题栏封装
猜你喜欢
随机推荐
【综合类型第 35 篇】程序员的七夕浪漫时刻
PostgreSQL 2022 报告:流行度上涨,开源、可靠性和扩展是关键
Linux:记一次CentOS7安装MySQL8(博客合集)
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
HDD杭州站•ArkUI让开发更灵活
MySQL 中 auto_increment 自动插入主键值
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
5G NR 系统消息
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
[Android] How to use RecycleView in Kotlin project
Oracle 19.3 restart 环境
字节一面:TCP 和 UDP 可以使用同一个端口吗?
What are the standards for electrical engineering
PG优化篇--执行计划相关项
poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
hdu4545 魔法串
L2-042 老板的作息表
MMDetection in action: MMDetection training and testing









