JAVA入門教程運算符和表達式

JAVA入門教程運算符和表達式

, ( ++
-),
( + ) ( ?:),
, ( ++i ) ( i++),
( a+b) , :
1.
(+,-,*,/,%,++,--)
2.
(,,=,=,==,!=)
3.
(!,&&,||)
4.
(>>,<<,>>>,&,|,^, )
5.
(=, +=)
6.
( ?:)
7.
( · , [], instanc eof, new,
( ), 調 () )
6
§ 3.1

,
,
運算符 用法 描述
+ op1+op2

- op1-op2

* op1*op2

/ op1/op2

% op1%op2
取模(求餘)
Java
,使 , "abc"+"de", "abcde"

C C++ , % , , 37.2%10=7.2
, :
運算符 用法 描述
+ +op
正值
- -op
負值
++ ++op,op++
1
-- --op,op--
1
i++
++i
i++
使 i ,使 i 1, i++ , i, i i+1
++i
使 i ,使 i 1, ++i , i i+1
i-- --i
3.1. 使
public class ArithmaticOp{
public static void main( String args[] ){
int a=5+4; //a=9
int b=a*2; //b=18
int c=b/4; //c=4
int d=b-c; //d=14
int e=-d; //e=-14
int f=e%4; //f=-2
double g=18.4;
double h=g%4; //h=2.4
int i=3;
int j=i++; //i=4,j=3
int k=++i; //i=5,k=5
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
System.out.println("d = "+d);
System.out.println("e = "+e);
System.out.println("f = "+f);
System.out.println("g = "+g);
System.out.println("h = "+h);
System.out.println("i = "+i);
System.out.println("j = "+j);
System.out.println("k = "+k);
}
}
其結果爲:
C:\
java ArithmaticOp
a = 9
b = 18
c
= 4
d = 14
e = -14
f
= -2
g
= 18.4
h = 2.4
i = 5
j = 3
k = 5

§ 3.2

, true false
, :
運算符 用法 返回true的情況
op1op2 op1大於op2
+ op1=op2 op1大於或等於op2
op1op2 op1小於op2
= op1=op2 op1小於或等於op2
== op1==op2 op1
op2相等
!= op1!=op2 op1
op2不等
Java
, ( ) == !=
( C C++ )
true false, C C++ 1 0
使 ,
if( a
b && b==c)

參考文獻http://wenku.it168.com/d_000068638.shtml

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章