JAVA学习:基本运算符,自增自减运算符并初识Math类,位运算符,三元运算符
通过网络各种资源学习整合的Java资料
运算符
- 算术运算符:+ - * / % ++ –
注意:%表示取余,即模运算:11%5=1 - 赋值运算符:=
- 关系运算符:> < >=(大于等于) <=(小于等于) ==(等于) !=(不等于) instanceof(面向对象的时候再细说)
- 逻辑运算符:&&(与 and) ||(或 or) !(非)
- 位运算符:& | ^ ~ >> << >>>
- 条件运算符:? :
- 扩展赋值运算符:+= -= *= /=
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//ctrl+D 复制当前行到下一行
int a=10;
int b=20;
int c=25;
int d=25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
输出为:30 -10 200 0.5
public class Demo01 {
public static void main(String[] args) {
//cast表示转换
long e=123123123123123L;
int f=123;
short g=10;
byte h=8;
System.out.println(e+f+g+h);
System.out.println(f+g+h);
System.out.println(g+h);
}
}
输出为:123123123123264 为long类型
141 为int类型
18 为int类型
注意:在运算的时候,如果含有long类型操作数,则输出结果也为long类型(double类型同理);其余情况均输出为int类型,无论是否含有int类型操作数
public class Demo01 {
public static void main(String[] args) {
//关系运算符返回的结果:布尔值true false
int a1=10;
int b1=20;
System.out.println(a==b);
}
}
输出为:false
自增自减运算符
public class Demo01 {
public static void main(String[] args) {
//++ -- 自增自减 一元运算符
int i=3;
int j=i++;//执行完这一行代码后,先给b赋值,再自增
//隐含i=i+1;
System.out.println(i);//此处i经过了+1 所以输出为4
//隐含i=i+1;
int k=++i;//执行这行代码前,先自增,再给b赋值
System.out.println(i);//此处i经过了+1 所以输出为5
System.out.println(j);
System.out.println(k);
}
}
输出为:4 5 3 5
初识Math类
public class Demo01 {
public static void main(String[] args) {
//幂运算 例如3^2 即3*3=9
double p=Math.pow(3,2);
System.out.println(p);
}
}
输出为:9.0
逻辑运算符
public class Demo01 {
public static void main(String[] args) {
//逻辑运算符
//与and 或or 非
boolean m=true;
boolean n=false;
System.out.println("a && b:"+(m&&n));
//逻辑与运算:两个变量都为true,结果为true;两个变量都为false,结果为false
System.out.println("a || b:"+(m||n));
//逻辑或运算:两个变量有一个为true,结果为true
System.out.println("!(a && b):"+!(m&&n));
//如果是true,则为false;反之
}
}
输出为:
a && b:false
a || b:true
!(a && b):true
public class Demo01 {
public static void main(String[] args) {
//存在短路运算
int aa=5;
boolean bb=(aa<4)&&(aa++<4);
System.out.println(bb);
System.out.println(aa);
}
}
输出为:false 5
因为(aa<4)&&(aa++<4),当最先判断aa<4为false之后,就能得出:bb为false,所以就没必要再继续执行aa++<4的判断了 故而aa还是5,没有被+1
位运算符
public class Demo01 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
- ----------------------------------------
A&B(与) AB上下每一位比较,若两个都是1,才是1;否则就是0 故为0000 1100
A|B(或) AB上下每一位比较,若两个都是0,才是0;否则就是1 故为0011 1101
A^B(异或) AB上下每一位比较,若两个相同,才是0;否则就是1 故为0011 0001
~B 与B每一位都相反 故为1111 0010
<<左移 相当于*2
>>右移 相当于/2
利用位运算效率很高!!!
2*8=16 = 2*2*2*2(2*3个2)
0000 0000 0
0000 0001 1
0000 0010 2
0000 0100 4
0000 1000 8
0001 0000 16
2到16就是1每次往前移一位
*/
System.out.println(2<<3);
}
}
输出为:16
扩展赋值运算符
public class Demo01 {
public static void main(String[] args) {
int q=10;
int r=20;
q+=r;//q=q+r
System.out.println(q);
//字符串连接符 + 若+的两侧有一侧出现了String类型 那么就会把其他操作数改为String类型型再进行连接
System.out.println(q+r);
System.out.println(""+q+r);
System.out.println(q+r+"");//String类型在后面,故先运算q+r,但String后面没有东西可以拼接了所以输出50
}
}
输出为:30 50 3020 50
三元运算符
public class Demo01 {
public static void main(String[] args) {
/*
x?y:z
如果x为true,则结果为y;反之为z
*/
int score=80;
String type=score<60?"不及格":"及格";
System.out.println(type);
}
}
输出为:及格
优先级
优先级由高到低依次往下
- ()
- ! +(正) -(负) ++ –
- *(乘) / %
- < <= > >=
- == !=
- ^
- &&
- ||
- ?:
- = += -= *= /= %=
注意:推荐多多使用()来使得条例更加清晰