当前位置:网站首页>变量与常量
变量与常量
2022-07-07 21:42:00 【51CTO】
1、什么是变量
变量是指向特定值的一个引用。
2、变量的组成
变量是由三个要素组成分别是数据类型,标识符和值。
变量的数据类型确定了其值的取值范围,大小,类型等。Java是一门注重数据类型的语言,因此我们在声明任何变量时要确定变量的数据类型。
变量的标识符是变量唯一识别标志,我们通过标识符可以确定到变量本身。通俗来说标识符就是变量的名字。这个名字就是变量的唯一识别标志。标识符的出现主要是为了方便我们操控这个变量,灵活地进行变量的后续操作。
变量的值是为了确定其引用指向的特定的值。在Java中任何没有被赋值的变量都不能直接操作。在未被赋值之前,都只能称之为声明一个变量。
3、变量的操作
①声明
②赋值
任何变量的赋值,都将使用“=”赋值。
③变量的其他相关操作
如算数运算,改值等等。
4、数据类型
因为我们知道变量的构成三要素之一是数据类型,那么数据类型到底有哪些呢?
在Java中数据类型被分为,基本数据类型与引用类型(对象/包装类)。
基本数据类型有四种:整型,浮点型,布尔型,字符类型。
整型:整型是被用来表示整数部分的值的类型。
根据取值范围我们有可以将整型分为短整型(short),整型(int),长整型(long)。
浮点类型:用来描述有小数点的值的类型。
浮点类型根据小数点精度问题可分为单精度(float)和双精度(double)。
在Java中默认是使用的双精度,在开发当中更多的也是使用双精度。若要使用单精度,必须要使用float强转。
字符类型用来表示字母,符号。
布尔类型用来表示对错。其值只有两个,一个为true,一个为false。
引用类型暂时在这里不讲,后面在讲完类与对象之后将会在那里提及。
5、变量命名规则:
- 大小写字母和数字构成
- 由多个单词构成,首单词小写,其余单词首字母大写。如(strNumber)。
6、常量
常量是具有公约性的值,如PI,e,数据库连接的四个参数等等。
通常常量使用static final修饰。
常量通常在一个类中被多个方法使用且值不会因为被使用而发生改变。这就解决了变量会因为使用而使得值发生改变的问题。
命名规则:全名称大写。
更多了解
https://edu.51cto.com/lecturer/14175030.html
边栏推荐
- DNS series (I): why does the updated DNS record not take effect?
- ByteDance senior engineer interview, easy to get started, fluent
- Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
- OpeGL personal notes - lights
- The essence of analog Servlet
- Reptile combat (VII): pictures of the king of reptiles' heroes
- Debugging and handling the problem of jamming for about 30s during SSH login
- [azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
- operator
- Anti climbing killer
猜你喜欢
L2: current situation, prospects and pain points of ZK Rollup
Dry goods sharing | devaxpress v22.1 original help document download collection
South China x99 platform chicken blood tutorial
Automatic classification of defective photovoltaic module cells in electronic images
Wechat official account oauth2.0 authorizes login and displays user information
Jerry's test box configuration channel [chapter]
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
PKPM 2020 software installation package download and installation tutorial
解决uni-app中uni.request发送POST请求没有反应。
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
随机推荐
SAR影像质量评估
用语雀写文章了,功能真心强大!
Can I open a stock account directly online now? Is it safe?
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Main functions of OS, Sys and random Standard Libraries
How to turn on win11 game mode? How to turn on game mode in win11
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
客户案例|华律网,通过观测云大幅缩短故障定位时间
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Jerry's power on automatic pairing [chapter]
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
Automatic classification of defective photovoltaic module cells in electronic images
What is the reason for the abnormal flow consumption of 4G devices accessing the easygbs platform?
Win11U盘不显示怎么办?Win11插U盘没反应的解决方法
The essence of analog Servlet
强化学习-学习笔记9 | Multi-Step-TD-Target
Px4 autonomous flight
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
海外代理推荐