java 運算子參考手冊

來源:互聯網
上載者:User

Precedence     Operator     Description                          Association
1              ++,--        Postincrement, Postdecrement         R -> L
2              ++,--        Preincrement, Predecrement           R -> L
               +,-          Unary plus, unary minus              R -> L
               ~            Bitwise compliment                   R -> L
               !            Boolean NOT                          R -> L
3              new          Create object                        R -> L
               (type)       Type cast                            R -> L
4              *,/,%        Multiplication, division, remainder  L -> R
5              +,-          Addition, subtraction                L -> R
               +            String concatenation                 L -> R
6              <<, >>, >>>  Left shift, right shift, unsigned right shift     L -> R
7              <, <=, >, >=                                      L -> R
               instanceof   Type comparison                      L -> R
8              ==, !=       Value equality and inequality        L -> R
               ==, !=       Reference equality and inequality    L -> R
9              &            Boolean AND                          L -> R
               &            Bitwise AND                          L -> R
10             ^            Boolean XOR                          L -> R
               ^            Bitwise XOR                          L -> R
11             |            Boolean OR                           L -> R
               |            Bitwise OR                           L -> R
12             &&           Conditional AND                      L -> R
13             ||           Conditional OR                       L -> R
14             ?:           Conditional Ternary Operator         L -> R
15             =,+=,-=,     Assignment Operators                 R -> L
               *=,/ =,%=,
               &=,^=, |=,
               <<=, >> =,
               >>>=

Operator Precedence Group Associativity Operator Precedence
(), [], postfix ++, postfix -- left Highest
unary +, unary -, prefix ++, prefix --, ~, ! right 
(type), new left 
*, /, % left 
+, - left 
<<, >>, >>> left 
< ,<= , >, >=, instanceof  
==, !=  
& left 
^ left 
| left 
&& left 
|| left 
?: left 
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= right lowest

簡單加號運算

public class MainClass {

  public static void main(String[] arg) {
    int count = 1;

    count += 5;
    System.out.println(count);

    count = count + 5;
    System.out.println(count);

  }

}

條件運行與逗號運算子

if(value > conditionValue){
  result = result1;
}else{
  result = result2;
}

 logical_expression ? expression1 : expression2
 
public class MainClass {
  public static void main(String[] args) {
    int v = 1;
    System.out.println(v == 1 ? "A" : "B");

    v++;
    System.out.println(v == 1 ? "A" : "B");
  }
}
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.