5.Operators and Assignments

1. ++/--前缀 是先加+/-1 ,再赋值 .如 
   int  x ;
   log.info( " ++x= "+ ++x ) ;  //  ++x = 1
   log.info( " x = "+ x );        //  x  = 1

2. 后缀++/--  是先赋值,再+/-1 如; 
   int  x ;
   log.info( " x++ = " + x++  ) ;  //  x++  = 0 
   log.info( " x = "+ x );             //  x  = 1
Remember these rules for primitive types:
2.1. Anything bigger than an int can NEVER be assigned to an int or anything smaller than int ( byte, char , short) without explicit cast.
2.2. CONSTANT values up to int can be assigned (without cast) to variables of lesser size ( eg. short to byte) if the value is representable by the variable.( ie. fits into the size of the variable).
2.3. operands of mathematical operators are ALWAYS promoted to ATLEAST int. (ie. for byte * byte both bytes will be first promoted to int.) and the return value will be ATLEAST int.
2.4. Compound assignment oprators ( +=, *= etc)  have strange ways so read this carefully:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
Note that the implied cast to type T may be either an identity conversion or a narrowing primitive conversion.
For example, the following code is correct:
short x = 3;
x += 4.6;
and results in x having the value 7 because it is equivalent to:
short x = 3;
x = (short)(x + 4.6);

3.continue can be used only inside a 'for', 'while' or 'do while' loop.

4.从左向右运作,包括赋值和运算
    int a = 10;  a += (a = 4);  // a = 14

5.System.out.println(~false ); will print true.
  ~ cannot be applied to booleans.
  System.out.println(!5 ); will not compile.
  !, && and || can only be applied to booleans.

6 Only + is overloaded for String. a+=x is actually converted to a = a + x. so it is valid for Strings. dot (.) operator accesses members of the String object. There is only one member variable though: CASE_INSENSITIVE_ORDER. It is of class Comparator (actually an interface).

7 if i is an int then
 i << or >> or >>> n, i is actually shifted only by n mod 32. So, if n is 32 or 64 then  it is not shifted at all.
So i in the above question remains unchanged.
if i is a long then,
 i << or >> or >>> n, i is actually shifted only by n mod 64. So, if n is 64 or 128 then i is not shifted at all.
When j right shifts even by 1bit. it becomes 0. And shifting it by 30 more bits doesn't make a difference.
Note that, shifting an int by 32 bits and by 31 and then again by 1 bit is not same. Because 32%32 is 0, so it will not be shifted at all in the first case.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值