当前位置:网站首页>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.
边栏推荐
- FPGA: Use of the development environment Vivado
- PostgreSQL 2022 报告:流行度上涨,开源、可靠性和扩展是关键
- 2022技能大赛训练题:交换机snmp配置
- E-sports, convenience, efficiency, security, key words for OriginOS functions
- Import Excel/CSV from Sub Grid within Dynamics 365
- The fuse: OAuth 2.0 four authorized login methods must read
- 张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
- 深入理解 Istio 流量管理的超时时间设置
- ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
- 【MySQL基础】-【数据处理之增删改】
猜你喜欢
sqlserver编写通用脚本实现获取一年前日期的方法
【MySQL基础】-【数据处理之增删改】
012年通过修补_sss_提高扩散模型效率
60行从零开始自己动手写FutureTask是什么体验?
产品太多了,如何实现一次登录多产品互通?
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
今天告诉你界面控件DevExpress WinForms为何弃用经典视觉样式
SQL Outer Join Intersection, Union, Difference Query
随机推荐
数据可视化(二)
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
5G NR 系统消息
Import Excel/CSV from Sub Grid within Dynamics 365
Nature:猪死亡1小时后,器官再次运转
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
多线程(进阶) - 2.5w字总结
nyoj86 找球号(一) set容器和二分 两种解法
解决【命令行/终端】颜色输出问题
GCC编译的时候头文件搜索规则
秘乐短视频挖矿系统开发详情
如何测试一下现场的备机失败,转发主机的场景?
什么是 DevOps?看这一篇就够了!
012_SSS_ Improving Diffusion Model Efficiency Through Patching
Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
双因子与多因子身份验证有什么区别?
What are the standards for electrical engineering
例题 可达性统计+bitset的使用
Guys, I am a novice. I use flinksql to write a simple count of user visits according to the document, but it ends after executing it once.