Java - 表达式和赋值语句(Expressions & Assignment Statements)
标识符(Identifier)
The name of a variable(or other items you might define like method names, class names, etc) is called an identifier.
An identifier can contain digits, letters and underscore(_) symbol and not start with a digit. Other characters(such as @, #, %, ^, &, *, ?, etc) are not allowed.
Why can not an identifier start with a digit? - Because it is a pain for human to understand. Suppose we declare a variable with this kind of identifier. For example:
int 555 = 12;//555 is a varible
float 666f = 15.8f;
char 0xFF = 'w';
int a = 555;
float b = 666f;
char c = 0xFF;
Int variable 555 conflicts with constant 555(a whole number).
Float variable 666f conflicts with floating-point number 666f.
Char variable 0xFF reminds us of hexadecimal number.
All of these make identifiers hard to understand and more ambiguous.
The following are all valid identifiers:
x x1 x_1 _x2 rate reminder sum ABCabc_xyz211
PS: (1) Java is a case-sensitive language. For example, xxx and XXX are two different identifiers.
(2) Some special identifiers are reserved for Java use, which is called keywords. Such as class, public, private, final, static, etc. You can check all keywords Java are using,
变量(Variable)
All variables in a Java program must be declared before it is used. When you declare a variable, you’re actually telling the compiler what type of data you should store in the variable.
There are all primitive types that Java has,
For a boolean type variable, it can hold two values: true and false. In memory, 00000000 represents false and 00000001 represents true.
For a char type variable, it holds one character. Java use Unicode to encode for all characters. You can check the default encoding method in Java,
Byte, short, int and long types are storing different kinds of integer. The memory space used for each type is different. Because of the difference, the range of integers becomes quite different(Read more about range of numbers). Byte and short are rarely used. We usually use int and long.
A floating-point number is a number with decimal point. The differences between float and double are accuracy and memory space. Double is more accurate and needs 64 bits. In fact, Java converts float into double in JVM. Thus, we use double rather than float in program.(Read more about binary representation of floating-point number in memory)
赋值(Assignment)
算术运算符(Arithmetic Operator)
+ addition
- subtraction
* multiplication
/ division
% modulo
PS: c = a / b. If both a and b are int type, then c is int type. For example. 9 / 2 = 4. If one or both of a and b is floating-point type, then c is floating-point type. For example, 9 / 2.0 = 4.5. (Other operations are the same)
括号和优先级(Parentheses and Precedence Rules)
Java will automatically follow precedence rules to add parentheses.
For example,
a+b*c-d/e
Java adds parentheses,
((a+(b*c))-(d/e))
强制类型转换(Type Casting)
byte —> short —> int —> long —> float —> double
For example,
int a = 10;
double b = 22.23;
b = a;
a = (int)b;
自增和自减运算符(Increment and Decrement Operators)
If the ++ is before the variable, then the incrementing is done before the value is returned. For example,
int a = 10;
int b = --a;
result:
a = 9;
b = 9;
if the ++ is after the variable, then the incrementing is done after the value is returned. For example,
int a = 10;
int b = a--;
result:
a = 9;
n = 10;
We prefer to not use increment or decrement operators inside of expression, but to only use them as simple statements. Such as i++, j- -.
最后再说两句(PS)
Have a deep understanding of those basic concepts.
Welcome questions always and forever.