Java learning diary (two): Java syntax

This series is a record of my learning situation, part of the content comes from the teacher's courseware. This article is only used as my own study notes, all the content only represents personal opinions and may not be correct. Dialectics are welcome.



Today's learning content:

标识符和关键字
二进制及转换 
常量和变量
数据类型
运算符
Scanner的简单使用

After sorting out several important knowledge points:

标识符作用及命名规则
关键字及保留字 
基本数据类型及其特点
基本数据类型的转化特点
常量和变量的区别
局部变量和成员变量的区别
运算符的优先级排序

1. The role of identifiers and naming rules

The role of identifiers : used to define the names of constants, variables, methods, classes, and packages.
Naming rules for identifiers:

必须以字母、下划线_、美元符$开头
由字母、下划线_、美元符$、数字组成
不能使用关键字和保留字
大小写敏感
命名最好见名知意
类名和接口命每个单词首字母大写
方法名和变量名第一个单词全小写,其余首字母大写
常量全部单词大写
包名全小写

2. Keywords and reserved words

Keywords : Some strings have specific meanings and special purposes. Java keywords are all lowercase .

Keyword
abstract boolean break byte case catch char class const continue
default do double else extends final finally float for goto
if implements import instanceof int interface long native new package
private protected public return short static strictfp super switch synchronized
this throw throws transient try void volatile while assert enum

Reserved words : no defined purpose, but reserved for future use. goto, const


3. Basic data types and their characteristics

Serial number type of data Size/bit Representable data range
1 byte 8 -128~127
2 short (short integer) 16 -215~215-1
3 int (integer) 32 -231~231-1
4 Long (long integer) 64 -263~263-1
5 loat (single precision) 32 -231~231-1
6 double (double precision) 64 -263~263-1
7 boolean (Boolean type) If the declared Boolean type is a variable, it occupies 4 bytes (32 bits), if it is an array type, it occupies 1 byte (8 bits) true、false
8 char (character) 16 0~232

Characteristics of char character type:

用单引号包裹,’a’、’王’,字符串使用双引号
是无符号型的,可以表示一个整数,不可以表示负数
表示任意在Unicode编码表中存在的单个有效字符符号

Characteristics of boolean type:

Boolean类型在java中不能和其他类型进行转换,在js中可以转化为01
如果声明的布尔类型是变量,占4字节(32位),如果是数组类型,占1字节(8位)
用于判断,只表示true、false

Fourth, the conversion characteristics of basic data types

取值范围小的自动转化为取值范围大的
boolean类型不能和其他类型进行转换
自动转换的顺序:byte--short--int--long--float--double
两个数据类型不同的变量相加时,会自动统一数据类型,遵循小转大规则
取值范围大的转化为取值范围小的需要强制转换,若值超过取值范围小的数据类型,数据可能精度损失或溢出,结果不可控

Five, the difference between constants and variables

Constant:
Constant is the amount that the value will not change during the running of the program;

Variables:
Variables are actually a piece of storage space in the memory. Use the variable name to access this storage space. When defining a variable, you need to declare the data type and name of the variable first. The system will allocate corresponding storage space to the variable according to different data types. To assign a value to a variable is to store data in this existing space.


Six, the difference between local variables and member variables

Local variables: The variables defined in the method are called local variables.
Member variables: Outside the method, the variables defined inside the class are called member variables.
Static variables: refers to the difference between static member variables,
local variables and member variables:

*定义的范围不一样
	局部变量在方法体内,属于方法内部的变量
	成员变量在类中、方法外部,属于类的变量
*成员变量的默认值要依据成员变量的类型来(int = 0,boolean=false ,float = 0.0F ,double =0.0 )
*局部变量没有默认值,必须赋值或初始化。
*作用范围不一样:
	局部变量只能在它所属的方法体内被使用
	成员变量的作用范围在整个类中任何方法中都可以调用。
*局部变量和成员变量如果同名,他们其实是不同的变量,符合各自的运行规则

Seven, operator priority sorting

Monocular>Arithmetic>Relation>Logic>Trinocular>Assignment
Arithmetic operator (++ --)> (* / %)> (+ -)
Relational operator (> <>= <=)> (== != )
Logical operator!> & >^ >|

Eight, some points about operators

关系运算符返回的是布尔类型
关系运算符(> < >= <=)只针对数值类型,(== !=)针对所有类型
逻辑运算符的操作数是boolean类型
| & ^ 既是逻辑运算符,也是位运算符,根据操作数的类型进行区分
三目条件运算符 X ? Y : Z经常用来代替简单的if-else判断
扩展运算符是指,+=-=/=%=*=,且扩展运算符自带强转

Guess you like

Origin blog.csdn.net/qq_37733862/article/details/109236757