当前位置:网站首页>变量与常量
变量与常量
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
边栏推荐
- How does win11 unblock the keyboard? Method of unlocking keyboard in win11
- 如何实现横版游戏中角色的移动控制
- Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
- 你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
- 648. Word replacement
- #DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
- 客户案例|华律网,通过观测云大幅缩短故障定位时间
- 为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
- Two kinds of updates lost and Solutions
- Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cyclic functions [chapter]
猜你喜欢

Matplotlib drawing interface settings

为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?

South China x99 platform chicken blood tutorial

应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设

It's worth seeing. Interview sites and interview skills

双塔模型的最强出装,谷歌又开始玩起“老古董”了?

cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘

L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?

NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?

Ternary expressions, generative expressions, anonymous functions
随机推荐
【JDBC Part 1】概述、获取连接、CRUD
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
DNS series (I): why does the updated DNS record not take effect?
How to realize the movement control of characters in horizontal game
Win11时间怎么显示星期几?Win11怎么显示今天周几?
用语雀写文章了,功能真心强大!
【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
Paint basic graphics with custompaint
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
Welcome to CSDN markdown editor
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
Programming mode - table driven programming
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
如何实现横版游戏中角色的移动控制
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
谈谈制造企业如何制定敏捷的数字化转型策略
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Jerry's test box configuration channel [chapter]
Use br to back up tidb cluster data to azure blob storage