Java - 表达式和赋值语句(Expressions & Assignment Statements)

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值