在java中支持四则运算:+、-、*、/、%。
在java"="用于进行赋值操作。
而且在java中存在自增和自减的操作。
1、四则运算
public class TestDemo01
{
public static void main(String args[])
{
int x = 10;
int y = 20;
System.out.println(x + "+" + y + "=" + (x+y));
System.out.println(x + "-" + y + "=" + (x-y));
System.out.println(x + "*" + y + "=" + (x*y));
System.out.println(x + "/" + y + "=" + (x/y));
System.out.println(x + "%" + y + "=" + (x%y));
}
}
2、简便运算
简便运算指的是:++、--,只要的目的是当时为了解决运算的空间而是用的。
public class TestDemo02
{
public static void main(Sting args[])
{
int x = 10;
System.out.println(x++);
System.out.println(x);
}
}
第一个的值是10,第二个的值是11。
++放在后面表示先进行计算,之后进行自增的操作。
public class TestDemo03
{
public static void main(Sting args[])
{
int x = 10;
System.out.println(++x);
System.out.println(x);
}
}
第一个的值是11,第二个的值是11。
++放在前面表示先进行自增,之后再进行计算。
3、关系运算
关系运算:<=、>=、|=、>、
public class TestDemo04
{
public static void main(String args[])
{
int x = 10;
int y = 20;
System.out.println(x>y);
}
}
运行输出的结果是:false。
4、逻辑运算符
在java中可以将多个关系运算符进行统一的操作,使用逻辑运算:
1)与:&、&&
&:表示的是正常的与,所有的条件都必须判断。
&&:表示的是短路与,如果第一个条件为假,则后续条件不再判断。
2)或:|、||
|:表示的是正常的或,所有的条件都必须判断。
||:表示的是短路或,如果第一个条件为真,则后续的条件都将不再判断。
先来看一下以下的操作代码:
public class TestDemo05
{
public static void main(String args[])
{
System.out.println(10/0);
}
}
以上的程序一旦执行之后将出现错误,那么下面的所有程序将按照以上的格式进行操作。
public class TestDemo06
{
public static void main(String args[])
{
if(1==2&10/0==0)
{
System.out.println("条件满足!");
}
}
}
此时,第一个条件不满足之后,程序继续判断第二个条件,所以程序造成了错误。那么如果现在使用了短路与(&&)呢?
public class TestDemo07
{
public static void main(String args[])
{
if(1==2&&10/0==0)
{
System.out.println("条件满足!");
}
}
}
因为,现在第一个条件根本就不满足,所以,后面的内容是否满足并不会影响最后的结果,那么这就属于短路与的操作。
同理,短路或也是同样的操作,下面不使用短路或进行操作:
public class TestDemo08
{
public static void main(String args[])
{
if(1==1|10/0==0)
{
System.out.println("条件满足!");
}
}
}
此时,属于非短路操作,所以即使第一个条件满足了,那么也要将全部的条件判断完整,如果现在使用了短路或(||)呢?
public class TestDemo09
{
public static void main(String args[])
{
if(1==1||10/0==0)
{
System.out.println("条件满足!");
}
}
}
第一个条件满足了,这样,不管后面的条件是否满足,其最终的结果并不会受到任何的影响。
5、位运算
在普通的开发中,位运算的操作确实很少出现。位运算的操作符:|、&、>>、<>>。
或的操作:
public class TestDemo10
{
public static void main(String args[])
{
int x = 3 ;
int y = 5;
System.out.println(x|y);
}
}
最终的操作结果是7,那么实际上这里就采用了位运算。
3        -->           00000000     00000000     00000000      00000011
5        -->           00000000     00000000     00000000      00000101
|        -->            00000000     00000000     00000000      00000111
将以上的结果变为十进制的数据是:7
与操作:
public class TestDemo10
{
public static void main(String args[])
{
int x = 3 ;
int y = 5;
System.out.println(x&y);
}
}
3        -->           00000000     00000000     00000000      00000011
5        -->           00000000     00000000     00000000      00000101
&       -->           00000000     00000000     00000000      00000001
将以上的结果变为十进制的数据是:1
左移位操作:
public class TestDemo11
{
public static void main(String args[])
{
int x = 3;
System.out.println(3<<2);
}
} 3        -->           00000000     00000000     00000000      00000011
<<2   -->           00000000     00000000     00000000      00001100
将以上的结果变为十进制的数据是:12。
右移位操作:
public class TestDemo12
{
public static void main(String args[])
{
int x = 3;
System.out.println(3>>2);
}
}
3        -->           00000000     00000000     00000000      00000011
>>2   -->           00000000     00000000     00000000      00000000
所以最终结果是:0。
无符号右移操作:
public class TestDemo13
{
public static void main(String args[])
{
int x = 3;
System.out.println(3>>>2);
}
}
操作过程和之前是一样的。
如果现在操作的是一个负数的话,则必须注意的是,负数的操作=证书的反码+1。
思考:
现在要求完成一个2^3的操作,问,如何操作性能最高?
public class TestDemo14
{
public static void main(String args[])
{
int x = 2;
System.out.println(2<<2);
}
}
2        -->           00000000     00000000     00000000      00000010
<<2   -->           00000000     00000000     00000000      00001000              -->8