当前位置:网站首页>How many constants and data types do you remember?
How many constants and data types do you remember?
2022-08-10 11:56:00 【51CTO】
前言
In our last post we were talking aboutjavaNotes in the base,关键字,和标识符,这篇我们讲一下javaBasic constants and data types,This series is in strict accordance with thejava学习大纲,A series of step-by-step updates,Brothers who are beginners can pay attention,本系列持续更新.
1.介绍
java中,有许多,All kinds of data need us to represent,有的是可变的,some can't be changed,所以javadeclares two types to describe our data,一个是常量(不可改变的量,如常见的:1,2,1.5,2.7等),一个是变量(可以改变的量).
“常量”的广义概念是:‘不变化的量’(利如:在计算机程序运行时,不会被程序修改的量;数学函数中的某一个量,例如每一个具体的圆的半径、直径数值;物理学中的靠近地面的重力加速度;真空中的光速数值;不同的微粒的各自的质量)换言之,常量,在计算机技术方面虽然是为了硬件、软件、编程语言服务,但是它并不是专门为硬件、软件、编程语言而引入的概念.常量可区分为不同的类型,如:25、0、-8为整型常量,6.8、-7.89为实型常量,‘a’、‘b’为字符常量.常量一般从其字面形式即可判断.这种常量称为字面常量或直接常量.
2.常量
2.1、常量概念
常量是不能改变的量,That is, each constant is a value.
3、数据类型
Just like in mathematics, there are two types of numbers: decimals and integers,在JavaIt needs to represent the diverse content in life and production,Its data types are richer than decimals and integers in mathematics.
Javais a strongly typed language that strictly distinguishes data types,That is, each data must have a data type.
3.1、整数
(1)十进制表示方式:正常数字,如 13、25等
(2)二进制表示方式:以0b(0B)开头,如0b1011 、0B1001
(3)十六进制表示方式:以0x(0X)开头,数字以0-9及A-F组成,如0x23A2、0xa、0x10
(4)八进制表示方式:以0开头,如01、07、0721
代码演示:
3.2、小数
如2.13、1.0,0.5.
代码演示:
3.3、布尔型
Used to indicate whether,布尔类型只有两个值 trueRepresents true that the condition is satisfied, false代表假,即不满足条件.
代码演示:
3.4、字符
3.4.1、普通字符
(1)表示单个字符,The keys of the keyboard can enter a character.A Chinese character can represent a character.
(2)characters need to be used’’将内容包裹
(3)Character content cannot be empty
如 ‘1’、 ‘a’、 ‘ ’(空格)、 ‘中’、 ‘$’
代码演示:
语法错误:
出现语法错误,IDEA会报红,Alerts you to grammatical errors
如果报错,我们没注意到,Is forced to perform,The code will not compile,and will give us more obvious hints
3.4.2、转义字符
Sometimes we cannot give a character directly,Need to use escape character for escape action.转义字符也为一个字符,The escape symbol is composed of the escape symbol and the escaped character:\
如:
\t 制表符,print out the least1个,最多8个空格
\' 单引号,打印出‘
\" 双引号,打印出"
\\ 反斜杠,打印出 \
代码演示:
3.4.3、字符串
(1)字符串String是一种特殊的数据类型
(2)字符串必须使用“”包裹
如 “我爱Java”、 “0”、 “a”、 “”、 “null”
代码演示:
3.5、null常量
used in object-oriented,He means that the value isnull,并不是没有,只有一个值,Is mainly used to fight for the reference data type.【null】
3.6、符号常量
用finalModified variables are symbolic constants
格式:final 变量类型 变量名(大写)=初始化值;
比如:Our name,We basically won't change the name after we've chosen it.,but the name is often used,all we can define as symbolic constants.
代码演示:
展示:
What if we want to change the name later??能改吗?Let's take a look:
我们能看到,不符合java语法,编译不通过.
4.Three types of constants
常量有三种类型:静态常量、成员常量和局部常量.
静态常量:
使用在 final 之前 public static 修饰.public static 修饰的常量作用域是全局的,不需要创建对象就可以访问它,在类外部访问形式为 HelloWorld. PI.这种常量在编程中使用很多.
成员常量:
作用域类似于成员变量,不能修改.
局部常量:
作用域类似于局部变量,不能修改.
在定义常量时就需要对该常量进行初始化.
final 关键字不仅可以用来修饰基本数据类型的常量,还可以用来修饰对象的引用或者方法.
为了与变量区别,常量取名一般都用大写字符.
代码演示:
代码总结
You can try typing the following code,Advice for beginners,切勿直接复制粘贴,This is fake learning,It takes time to gain,Don't waste your study time,Since time and experience,Then you must gain,Get it done, brothers!!!.
结语
This article introduces you to constants and data types,beginner brothers,Can be practical,In the middle of the case,try to do it yourself,The code must be typed by yourself to be rewarded.
有什么问题,You can leave a message in the comment section below,I will reply you as soon as I see it!
Thank you very much for your continued attention!
边栏推荐
- 3款不同类型的自媒体免费工具,有效提高创作、运营效率
- Clicking Exercise - 64 Longest Harmonic Subsequences
- 今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
- LeetCode 109. 有序链表转换二叉搜索树
- A case of violent parameter tuning in machine learning
- 开源的作者,也有个生活问题
- HDU 6040 Hints of sd0061 (技巧)
- 力扣练习——58 验证二叉搜索树
- codevs 2370 小机房的树 (LCA)
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
猜你喜欢
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
gpu-admission 源码分析
怎么加入自媒体,了解这5种变现模式,让账号快速变现
Where can I view the version record of WeChat applet submission review history?
OPNsense安装配置Zenarmor
从脚本到剪辑,影像大师亲授的后期制作秘籍
Some tips for using Unsafe
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Network sockets (UDP and TCP programming)
Flutter气泡框实现
随机推荐
皕杰报表在传参乱码
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
LeetCode 19. 删除链表的倒数第 N 个结点
项目部署、
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
力扣练习——62 有效的数独
推荐6个自媒体领域,轻松易上手
不止跑路,拯救误操作rm -rf /*的小伙儿
Redis设计与实现
ENVI 5.3软件安装包和安装教程
10 个 Reduce 常用“奇技淫巧”
Buckle Exercise - 61 Sort by frequency of characters
OPNsense安装配置Zenarmor
Network Fundamentals (Section 1)
Redis常用命令
LCD驱动端与设备端名称匹配过程分析(Tiny4412)
负载均衡原理分析与源码解读
电脑怎么设置屏幕息屏时间(日常使用分享)
使用哈工大LTP测试分词并且增加自定义字典
软件架构简介