第三章: Controlling Program Flow

第三章: Controlling Program Flow

1Mathematical operators(数学运算符)

addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result

2Relational operators(关系运算符)

The relational operators are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equivalent (==) and not equivalent (!=).

3Logical operators(逻辑运算符)

AND (&&), OR (||) and NOT (!) produces a boolean value of true or false based on the logical relationship of its arguments

4Bitwise operators(位运算符)

The bitwise AND operator (&)OR operator (|)EXCLUSIVE OR, or XOR (^)NOT (~)

5Shift operators移位运算符

The left-shift operator (<<)right-shift operator (>>)

the unsigned right shift >>>left shift <<<

If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an int. If you’re operating on a long, you’ll get a long result. Only the six low-order bits of the right-hand side will be used,

  static void printBinaryInt( int i) { // take an int then

    for(int j = 31; j >= 0; j--)       // print it out in binary format

      if(((1 << j) &  i) != 0)

        System.out.print("1");

      else

        System.out.print("0");

    System.out.println();

  }

6Literals(常量)

Hexadecimal (base 16), which works with all the integral data types, is denoted by a leading 0x or 0X followed by 0-9 or a-f either in uppercase or lowercase

  char c = 0xffff; // max char hex value

  byte b = 0x
    
    7f; // max byte hex value

  short s = 0x7fff; // max short hex value

  int i1 = 0x
    
    2f; // Hexadecimal (lowercase)

  int i2 = 0X
    
    2F; // Hexadecimal (uppercase)

  int i3 = 0177; // Octal (leading zero)

  // Hex and Oct also work with long.

  long n1 = 
    
    200L; // long suffix L/l大小写无关

long n2 = 
    
    200l; // long suffix (but can be confusing)

1.39 e -47f  in Java it means 1.39 x 10-47

7Precedence revisited(重访优先级)

commentary: “Ulcer Addicts Really Like C A lot.”

Mnemonic

Operator type

Operators

Ulcer

Unary

+ - ++--

Addicts

Arithmetic (and shift)

* / % + - << >>

Really

Relational

> < >= <= == !=

Like

Logical (and bitwise)

&& || & | ^

C

Conditional (ternary)

A > B ? X : Y

A Lot

Assignment

= (and compound assignment like *=)

 

8The infamous “goto”

Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism: a label.

label1: 

outer-iteration {

  inner-iteration {

    //...

    break; // 1

    //...

    continue;  // 2

    //...

    continue label1; // 3

    //...

    break label1;  // 4

  }

}

The same rules hold true for while:

  1. A plain continue goes to the top of the innermost loop and continues.

  2. A labeled continue goes to the label and reenters the loop right after that label.

  3. A break “drops out of the bottom” of the loop.

  4. A labeled break drops out of the bottom of the end of the loop denoted by the label.

9switch

it requires a selector that evaluates to an integral value, such as int or char.

asting from a float or double to an integral value always truncates the number.

It turns out that 0.0 is included in the output of Math.random( ). Or, in math lingo, it is [0,1).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值