当前位置:网站首页>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=15
Int 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 lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
- PCB layout must know: teach you to correctly lay out the circuit board of the op amp
- 这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口
- Leetcode刷题——623. 在二叉树中增加一行
- 教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
- 结合“xPlus”探讨软件架构的创新与变革
- 上位机开发C#语言:模拟STC串口助手接收单片机发送数据
- 提问题进不去。想问大家一个关于返回值的问题(图的遍历),求给小白解答啊
- Android 开发用 Kotlin 编程语言三 循环控制
- [Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
猜你喜欢
TiDB 6.0 Placement Rules In SQL 使用实践
字节一面:TCP 和 UDP 可以使用同一个端口吗?
Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
E-sports, convenience, efficiency, security, key words for OriginOS functions
Scaling-law和模型结构的关系:不是所有的结构放大后都能保持最好性能
5G NR 系统消息
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
Create a Dapp, why choose Polkadot?
Data Middle Office Construction (10): Data Security Management
API 网关简述
随机推荐
Linux:记一次CentOS7安装MySQL8(博客合集)
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
图像分割模型——segmentation_models_pytorch和albumentations 组合实现多类别分割
Oracle 19.3 restart 环境
FPGA:基础入门LED灯闪烁
abc262-D(dp)
我要抓狂了。。又回到了几天不能A一道题的时候
【MySQL基础】-【数据处理之增删改】
微信小程序标题栏封装
Http-Sumggling缓存漏洞分析
2022技能大赛训练题:交换机snmp配置
提问题进不去。想问大家一个关于返回值的问题(图的遍历),求给小白解答啊
Go学习笔记(篇二)初识Go
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
5G NR 系统消息
如何用Golang来手写一个Blog - Milu.blog 开发总结
nyoj754 黑心医生 结构体优先队列
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
发现C语言的乐趣
Leetcode刷题——623. 在二叉树中增加一行