当前位置:网站首页>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.
边栏推荐
- hdu4545 魔法串
- FPGA:基础入门按键控制LED灯
- 多线程(进阶) - 2.5w字总结
- 七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
- How OpenHarmony Query Device Type
- Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
- SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
- GCC编译的时候头文件搜索规则
- Chapter 5: Multithreaded Communication—wait and notify
- nyoj754 黑心医生 结构体优先队列
猜你喜欢

Android 开发用 Kotlin 编程语言 二 条件控制

反射修改jsessionid实现Session共享

用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战

Scaling-law和模型结构的关系:不是所有的结构放大后都能保持最好性能

RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)

Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)

Nature:猪死亡1小时后,器官再次运转

RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)

Leetcode刷题——623. 在二叉树中增加一行

PCB layout must know: teach you to correctly lay out the circuit board of the op amp
随机推荐
深入理解 Istio 流量管理的超时时间设置
Score interview (1)----related to business
OpenHarmony如何查询设备类型
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
MMDetection in action: MMDetection training and testing
2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
Detailed explanation of PPOCR detector configuration file parameters
FPGA:基础入门LED灯闪烁
19.3 restart the Oracle environment
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
012年通过修补_sss_提高扩散模型效率
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
[Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
【名词】什么是PV和UV?
In-depth understanding of timeout settings for Istio traffic management
.NET深入解析LINQ框架(六:LINQ执行表达式)
结合“xPlus”探讨软件架构的创新与变革
FPGA: Basic Getting Started Button Controlling LED Lights
PostgreSQL 2022 Report: Rising popularity, open source, reliability and scaling key