Java Basic Grammar (1) - Keywords, Identifiers, Comments, Constants, Variables, Operators
1. Keywords
1.1 Definition and characteristics of keywords
- Definition: A word given a special meaning by the Java language.
- Features: All letters in the keyword are lowercase.
1.2 Keyword List
Keywords used to define data types:
class、interface、byte、short、int、long、float、double、char、boolean、void
Keywords used to define data type values:
true、false、null
Keywords used to define flow control:
if、else、switch、case、default、while、do、for、break、continue、return
Keywords used to define access modifiers:
private、protected、publlic
Keywords used to define class, function, variable modifiers:
abstract、final、static、synchronized
Keywords used to define class-to-class relationships:
extends、implements
The keywords used to define the creation instance and reference instance, and judge the instance:
new、this、super、instanceof
Keywords for exception handling:
try、catch、finally、throw、throws
Keywords for packages:
package、import
Other modifier keywords:
native、strictfp、transient、volatile、assert
2. Identifier
2.1 Definition and Features
- Definition: Some nouns that are customized in the program.
- Features: It consists of 26 uppercase and lowercase English letters, numbers 0-9, symbols: _, $.
2.2 Define legal identifier rules
- Numbers cannot start.
- Keywords cannot be used.
- Special symbols other than _ and $ cannot be included.
2.3 Name specification in java
Package name: All letters are lowercase when there are multiple words.
For example: xxyyzz
Class name interface name: When composed of multiple words, the first letter of all words is capitalized.
For example: XxYyDd
Variable name and function name: When composed of multiple words, the first letter of the first word is lowercase, and the first letter of the second word is uppercase.
For example: xxTtWw
Constant names: All initials are capitalized. When there are multiple words, each word is connected with an underscore.
E.g:
XX_YY_ZZ
3. Notes
3.1 Definition and Features
- Definition: Text used to annotate a program is a comment.
- Features: Improve the readability of the code.
3.2 Java annotation format
- One-line comment:
//注释文字
- Multi-line comments:
/*注释文字*/
- Documentation Notes:
/**注释文字*/
4. Constants
4.1 Definition of Constants
- Definition: A value whose size cannot be changed.
4.2 Origin of base
- Binary: 0-1, full 2 into 1.
- Octal: 0-7, full 8 into 1, represented by 0 at the beginning.
- Decimal: 0-9, full 10 into 1.
- Hexadecimal: 0-9, AF, full 16 into 1, represented by 0X at the beginning.
5. Variables
5.1 The concept of variables
- Definition: A storage area in memory, this area has its own name (variable name) and type (data type), the data in this area can change continuously within the same type range.
- Features: Open up a space in memory to store uncertain values.
5.2 Data Types in the Java Language
- Basic data types (3 types)
- Numerical
- Integer types (4 types):
byte
(1 byte): range (-128~127);short
(2 bytes): range (-32768~32767);int
(4 bytes);long
(8 bytes): If the data is defined as a long type that exceeds the int range, add "L" (recommended) or "l" (the default is int, an error will be reported if the data exceeds the int range, and it will not);
- Floating point types (2 types):
float
(4 bytes): The first bit is the sign bit, the next 8 bits represent the exponent, and the next 23 bits represent the mantissa; if the specified floating-point type is float, add "F" or "f" (the default is double, otherwise compile hints may lose precision);double
(8 bytes): The first bit is the sign bit, the next 11 bits represent the exponent, and the next 52 bits represent the mantissa;
- Integer types (4 types):
- Character type:
char
(2 bytes) - boolean:
boolean
(1 byte)
- Numerical
- reference data type
- class: (
class
) - Interface: (
interface
) - Array: (
[]
)
- class: (
6. Operators
arithmetic operators
Signs: plus (+), minus (-), plus (+), minus (-), multiply (*), divide (/), modulo (%), increment (++), decrement (-) -), string concatenation (+)
assignment operator
Symbols: =, +=, -=, *=, /=, %=
comparison operator
Operators: ==, ! =, <, >, <=, >=, instance of
Logical Operators
Operators: & (and), | (or), ! (not), ^ (exclusive or), || (or, short circuit), && (and, short circuit)
bitwise operators
- << (left shift): 0 is added to the vacant position, the removed high position is discarded, and the vacant position is filled with 0.
- >> (Shift right): If the highest bit of the shifted binary is 0, after shifting to the right, the vacant bit will be filled with 0, and if the highest bit is 1, the vacant bit will be filled with 1.
- >>> (unsigned right shift): Whether the highest bit to be shifted is 0 or 1, the vacant bits are filled with 0.
- & (and operation): Binary bits are & operation, only the result of 1&1 is 1, otherwise it is 0.
- | (OR operation): Binary performs | operation, only the result of 0|0 is 0, otherwise it is 1.
- ^ (exclusive or operation): ^ operation with the same binary bit, the result is 0: 1^1=0, 0^0=0; the result of ^ operation with different binary bits is 1: 1^0=1, 0^1 =1
- Ternary operator
- Format:
(条件表达示)?表达示1(true):表达示2(false);
- Example:
- Format:
//取a,b中的最大值
int a=4;
int b=3;
int s=(a>b)?a:b;