java基本操作符


java操作字符
前缀
postfix
++ –

i++; // i = i+1
i–; // i = i-1

unary

++expr –expr +expr -expr ~ !

加减乘除
additive,multiplicative
+ – * / %

移位
shift
<< >> >>>

条件
relational
< > <= >= instanceof

等于
equality
== !=

按位AND运算符
bitwise AND
&

按位XOR(^,异或)
bitwise exclusive OR
^

按位NOT(~,也叫作“非”运算符)属于一元运算符,生成与输入位的相反的值
~

按位OR运算符
bitwise inclusive OR
|

逻辑运算符 “与”
logical AND
&&

逻辑运算符 “或”
logical OR
||

if then else结构的缩写
ternary (shorthand for if-then-else statement)
? :

赋值运算
assignment
= += -= *= /= %= &= ^= |= <<= >>= >>>=

/**
java-er.com
learn java is so easy
*/
 
public class Operators
{
	public static void main(String[] args){
		int i = 10;
		System.out.println("+ - ");		
		System.out.println("---------------------------------------");		
		//++ --
		i++;
		System.out.println("i++ "+i);
 
		i--;
		System.out.println("i-- " + i);
 
		++i;
		System.out.println("++i " + i);
 
		--i;
		System.out.println("--i " + i);
 
		//+ - * / %
		System.out.println("---------------------------------------");		
		System.out.println("additive,multiplicative ");
		System.out.println("---------------------------------------");		
		int a = 5,b = 2,c;
		c = a+b;
		System.out.println(c);
		c = a-b;
		System.out.println(c);
		c = a*b;
		System.out.println(c);
		c = a/b;
		System.out.println(c);
		c = a%b;
		System.out.println(c);
 
 
 
		//shift	As a beginner, you will rarely use this
		System.out.println("---------------------------------------");		
		System.out.println("shift ");
		System.out.println("---------------------------------------");		
		c = a<<b;
		System.out.println(c);
 
		c = a>>b;
		System.out.println(c);
 
		c = a>>>b;
		System.out.println(c);
 
		//relational
		System.out.println("---------------------------------------");		
		System.out.println("relational ");		
		System.out.println("---------------------------------------");		
		System.out.println(a>b);
		System.out.println(a<b);
		System.out.println(a>=b);
		System.out.println(a<=b);
 
		//equality
		System.out.println("---------------------------------------");		
		System.out.println("equality ");		
		System.out.println("---------------------------------------");		
		System.out.println(a==b);
		System.out.println(a!=b);
 
		//bitwise
		System.out.println("---------------------------------------");		
		System.out.println("bitwise ");		
		System.out.println("---------------------------------------");	
		System.out.println(a&b);		
		System.out.println(a^b);		
		System.out.println(a|b);	
 
		//logical
		int d = 4,e=8;
		System.out.println("---------------------------------------");		
		System.out.println("logical ");		
		System.out.println("---------------------------------------");	
		System.out.println(a > b && d > e);
		System.out.println(a > b || d >e);
		c = a>b?a:b;
		System.out.println(c);
		/*ia > b ? a: b   euqals  
			if(a > b){
				c = a;
			}else{
				c = b;
			}
		*/
 
		//assignment
 
		System.out.println("---------------------------------------");		
		System.out.println("assignment");		
		System.out.println("---------------------------------------");	
		c = a;
		c += b; //c = c + b
		System.out.println(c);
		c -= b; //c = c - b
		System.out.println(c);
		c *= b; //c = c * b
		System.out.println(c);
		c /= b; //c = c / b
		System.out.println(c);
		c %= b; //c = c % b
		System.out.println(c);
		c &= b; //c = c & b
		System.out.println(c);
		//you can test the remaining	 ^= |= <<= >>= >>>=
 
	}
}
如果你是一名技术人员可加我QQ 2651-0442-02,如果你是java技术人还可以加入QQ群 1784-9136-0
你将得到的不仅仅是技术的交流,还有职业机会,人生解惑.
首发地址:月小升博客http://java-er.com/blog/java-operators/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
此条目发表在 JAVA 分类目录,贴了 标签。将固定链接加入收藏夹。
既然来了,就评论一下,不会怀孕的

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">