当前位置:网站首页>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.
边栏推荐
- nyoj86 找球号(一) set容器和二分 两种解法
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- 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.
- MySQL 中 auto_increment 自动插入主键值
- The fuse: OAuth 2.0 four authorized login methods must read
- 拓朴排序例题
- 【深度学习】mmclassification mmcls 实战多标签分类任务教程,分类任务
- Android 开发用 Kotlin 编程语言一 基本数据类型
- Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
- 导火索:OAuth 2.0四种授权登录方式必读
猜你喜欢
随机推荐
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
FPGA: Basic Getting Started Button Controlling LED Lights
【深度学习】mmclassification mmcls 实战多标签分类任务教程,分类任务
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
How to choose coins and determine the corresponding strategy research
深入理解 Istio 流量管理的超时时间设置
问题征集丨ECCV 2022中国预讲会 · Panel专题研讨会
[Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
L2-042 老板的作息表
In-depth understanding of timeout settings for Istio traffic management
Chapter 5: Multithreaded Communication—wait and notify
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.
Oracle 19.3 restart 环境
Data Middle Office Construction (10): Data Security Management
PostgreSQL 2022 Report: Rising popularity, open source, reliability and scaling key
A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
FPGA:基础入门LED灯闪烁
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
【名词】什么是PV和UV?









