首页 > 编程语言 >java支持的运算符以及作用

java支持的运算符以及作用

时间:2022-09-30 14:33:39浏览次数:54  
标签:java 运算 int System 支持 运算符 println out

java语言支持如下运算符,优先级使用括号(),

  • 算数运算符:+ ,- ,*, / ,% (取余运算,或模运算),++ (自增),--(自减)

  • 赋值运算符:= int a = 10(把10赋值给a)

  • 关系运算符:>,<,>=,<=,==(java里等于用两个==),!=instanceof(!=表示的是不等于)

  • 逻辑运算符:&&(与意思大概and),||(或意思or),!(非,非你即我)

  • 位运算符:&,|,^(异或),~(取反),>>(右移除2),<<(左移乘2),>>>(了解)

  • 条件运算符:?, :

  • 扩展复制运算符:+=,-=,*=,/=

//关系运算符返回结果:正确,错误,布尔值,false或者true

A = 0011 1100

B = 0000 1101

A&B = 0000 1100 AB上对照,都有1为1,缺一不可均为0

A|B = 0011 1101 AB上对照,有1为1,没有为0

A^B = 0011 0001 AB上对照,相同为0不同为1

~B = 1111 0010 取B的相反,0取1,1取0

位运算效率极高

位运算常见的面试题,28怎么运算最快 2222 直接用位运算左移箭头朝向<<,右移<< System.out.println(2<<3);结果为16最快

字符串连接符,问你有什么区别

int a = 10; int b = 20;

System.out.println(""+a+b);//结果为1020,字符串就是那个”“在前面他会拼接

System.out.println(a+b+"");//结果为30,字符串在后面他就按照前面相加运算

重要的点

package operator;

public class Demo04 {
public static void main(String[] args) {
// ++ (自增),--(自减)
int a = 3;
int b = a++;//执行完这行代码后,献给b赋值,再自增
//a++ a = a +1
System.out.println(a);
int c = ++a;//执行完这行代码后,再自增,献给c赋值,
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 8,很多运算,我们会使用一些工具来操作
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}

与,或,非

package operator;

public class Demo05 {
public static void main(String[] args) {
//与&&(and) 或||(or) 非!(取反)
boolean a = true;
boolean b = false;
boolean c = a&&b;
System.out.println(c);//false
//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a && b:"+(a&&b));//false
//逻辑或运算:两个变量有一个为真,则结果为true
System.out.println("a || b:"+(a||b));//true
//如果市镇,则变为假,如果是假,则变为真
System.out.println("!(a && b):"+!(a&&b));//true
//短路运算
System.out.println("====================");
int c1 = 5;
boolean d = (c1<4)&&(++c1<4);//false
System.out.println(d);
System.out.println(c1);
int e = c1++;
System.out.println(e);
}
}

三元运算符

优先级运算优先级,使用()
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
//如果x==ture,则结果为y,否则为z
int score = 80;
String type = score<60?"不及格":"及格";//必须掌握
System.out.println(type);
int a = 1;
int b = 2;
int c = 3;
int d = (a+b)*c;
System.out.println(d);
}
}

标签:java,运算,int,System,支持,运算符,println,out
From: https://www.cnblogs.com/wang1999an/p/16744795.html

相关文章