Java常用运算符介绍及使用方法

    科技2024-01-31  93

    public class dome01 { 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 iptpackage operator; public class dome02 { public static void main(String[] args) { long a=51132131315L; int b=123; short c=10; byte d=1; System.out.println(a+b+c+d);//输出的是Long类型 有long是long型,有double就是输出double型 System.out.println(b+c+d);//输出是int型 System.out.println(c+d);//输出是int型 } } //51132131449 //134 //11 package operator; public class dome03 { public static void main(String[] args) { //关系运算符的返回结果:正确 错误 通过布尔值体现 int a=10; int b=20; int c=21; System.out.println(c%a); //取余数或者叫模运算 System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); } } //1 //false //true //false //true package operator; public class dome04 { public static void main(String[] args) { //++ -- 自增 自减 int a=3; int b=a++;//执行完这行代码后,先给b赋值,再自增 //a=a+1 System.out.println(a); //a=a+1 int c=++a;//执行代码前,先自增,再给b赋值 System.out.println(a); System.out.println(b); System.out.println(c); //幂运算 2^3=8 很多运算我们会使用工具类来操作 double pow =Math.pow(2,3); System.out.println(pow); } } //4 //5 //3 //5 //8.0 package operator; public class demo06 { public static void main(String[] args) { /* A=0011 1100 B=0000 1101 A&B=0000 1100 A|B=0011 1101 A^B=0011 0001 相同为0,不相同为1 ~B=1111 0010 2*8=16 2*2*2*2=16 << *2 >> /2 0000 0000 0 0000 0001 1 0000 0010 2 0000 0100 4 0000 1000 8 0001 0000 16 */ } } package operator; public class demo07 { public static void main(String[] args) { int a=10; int b=20; a+=b; //a=a+b a-=b; //a=a-b System.out.println(a); System.out.println(b); //字符串连接符 在加号运算符两侧 有一个“String”类型,会把另外一个或者其他操作数都变为String类型 System.out.println(""+a+b); System.out.println(a+b+""); //如果在前面还是会运算 } } 10 20 1020 30 package operator; public class demo08 { public static void main(String[] args) { //三元运算符 /* x ? y : z 如果x==true,则结果为y,否则为z */ int score=60; String type=score <60?"不及格":"及格"; System.out.println(type); } } 及格
    Processed: 0.009, SQL: 8