Operators

Short-circuiting

Used in logical operation that the expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined. As a result, the latter parts of  a logical expression might not be evaluated.

即逻辑判断时,从左往右,在这个过程中一旦结果被确定,停止以后的判断,直接返回结果。

Bitwise Operators

  • & (AND operator): 1 & 1 = 1, 1 & 0 = 0
  • | (OR operator): 1 | 0 = 1, 0 | 0 = 0
  • ^ (XOR operator): 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1
  • ~ (NOT operator): ~1 = 0, ~0 = 1

Shift Operators

  • << (left-shift operator): shift the operand to left by the number of specified bits, inserting 0s at the lower-order.
  • >> (signed right-shift operator): shift the operand to right by the number of specified bits, inserting 0s (positive value) or 1s (negative value) at the upper-order.
  • >>> (unsigned right-shift operator): same as ">>" except only use 0 extension.

Promotion

If you perform any mathematical or bitwise operations on primitive data types that are smaller than an int (that is char byte or short), those values will be promoted to int before performing the operations, the the resulting value will be of type int. In such case, casting is a must if you want to assign back into the smaller type.

In general, the largest data type in an expression is the one that determines the size of the result of that expression. (double = float + double)

Casting

  • Widening conversion: cast from a data type that hold less information to one that hold much more (int -> float)
  • Narrowing conversion: cast from a data type that can hold more information to one that doesn't hold as much (float ->)

With widening conversion an explicit cast is not needed, while for the narrowing conversion the explicit cast is a must.

Java allows you cast any primitive type to any other primitive type, except for boolean.

Java Has No "sizeof"

In C and C++, the sizeof() operator tells you the number of bytes allocated for data items. This is for portability, since different data types might be different sizes on different machines. Thus portability is a huge headache for C and C++.

While in Java, because all the data types are the same size on all machines, so you don't have to think about portability on this level.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值