Java的运算符


运算符可以是一元、二元或三元的。
(1)一元运算符有1个操作数。例如,递增运算符"++"就是一元运算符。

 /** Unary operators, of type Unary.
     */
    public static final int POS = ERRONEOUS + 1;             // +
    public static final int NEG = POS + 1;                   // -
    public static final int NOT = NEG + 1;                   // !
    public static final int COMPL = NOT + 1;                 // ~ 按位取反
    public static final int PREINC = COMPL + 1;              // ++ _
    public static final int PREDEC = PREINC + 1;             // -- _
    public static final int POSTINC = PREDEC + 1;            // _ ++
    public static final int POSTDEC = POSTINC + 1;           // _ --

    /** unary operator for null reference checks, only used internally.
     */
    public static final int NULLCHK = POSTDEC + 1;

 


(2)二元运算符有2个操作数。例如,除法运算符"/"有2个操作数。

  /** Binary operators, of type Binary.
     */
    public static final int OR = NULLCHK + 1;                // ||
    public static final int AND = OR + 1;                    // &&
    public static final int BITOR = AND + 1;                 // |
    public static final int BITXOR = BITOR + 1;              // ^
    public static final int BITAND = BITXOR + 1;             // &
    public static final int EQ = BITAND + 1;                 // ==
    public static final int NE = EQ + 1;                     // !=
    public static final int LT = NE + 1;                     // <
    public static final int GT = LT + 1;                     // >
    public static final int LE = GT + 1;                     // <=
    public static final int GE = LE + 1;                     // >=
    public static final int SL = GE + 1;                     // <<
    public static final int SR = SL + 1;                     // >>
    public static final int USR = SR + 1;                    // >>>
    public static final int PLUS = USR + 1;                  // +
    public static final int MINUS = PLUS + 1;                // -
    public static final int MUL = MINUS + 1;                 // *
    public static final int DIV = MUL + 1;                   // /
    public static final int MOD = DIV + 1;                   // %

    /** Assignment operators, of type Assignop.
     */
    public static final int BITOR_ASG = MOD + 1;             // |=
    public static final int BITXOR_ASG = BITOR_ASG + 1;      // ^=
    public static final int BITAND_ASG = BITXOR_ASG + 1;     // &=

    public static final int SL_ASG = SL + BITOR_ASG - BITOR; // <<=
    public static final int SR_ASG = SL_ASG + 1;             // >>=
    public static final int USR_ASG = SR_ASG + 1;            // >>>=
    public static final int PLUS_ASG = USR_ASG + 1;          // +=
    public static final int MINUS_ASG = PLUS_ASG + 1;        // -=
    public static final int MUL_ASG = MINUS_ASG + 1;         // *=
    public static final int DIV_ASG = MUL_ASG + 1;           // /=
    public static final int MOD_ASG = DIV_ASG + 1;           // %=

  

 


(3)三元运算符有3个操作数。例如,条件运算符"?:"具有3个操作数。


运算符的行为还可能因所提供的操作数的数量而异。减法运算符"-"既是一元运算符又是二元运算符。对于减法运算符,如果只提供一个操作数,
则该运算符会对操作数取反并返回结果;如果提供两个操作数,则减法运算符返回这两个操作数的差。

 

java只有单目运算符、赋值运算符和三目运算符是从右向左结合的,也就是从右向左运算。

 

运算符的优先级(从高到低)

优先级

描述

运算符

1

括号

()  []

2

正负号

+  -

3

自增自减,非

++    --  !

4

乘除,取余

*   /   %

5

加减

+   -

6

移位运算

<<   >>   >>>

7

大小关系

>   >=   <  <=

8

相等关系

==   !=

9

按位与

&

10

按位异或

^

11

按位或

|

12

逻辑与

&&

13

逻辑或

||

14

条件运算

?:

15

赋值运算

=  +=  -=  *=  /=  %=

16

位赋值运算

&=  |=  <<=  >>=  >>>=

 

int a = 2;
int b = a +=1; // 这样的语句是合法的

三元运算符的类型转换:

https://blog.csdn.net/yu_rong/article/details/49474141

重要的还是参考JLS文档:https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25

 

 

String a,b;
public void md(){
	a += b;
} 

 

生成的字节码如下:  

 

 

stack=3, locals=1, args_size=1
0: new           #2  // class java/lang/StringBuilder
3: dup
4: invokespecial #3  // Method java/lang/StringBuilder."":()V
7: aload_0
8: dup_x1
9: getfield      #4   // Field a:Ljava/lang/String;
12: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
15: aload_0
16: getfield      #6  // Field b:Ljava/lang/String;
19: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
22: invokevirtual #7  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
25: putfield      #4  // Field a:Ljava/lang/String;
28: return

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Java的运算符)