Java运算符应用以及例子

    技术2022-09-01  86

    运算符

    Java 语言支持如下运算符: 优先级() 算术运算符: +,-,*,/,%,++,–赋值运算符=关系运算符:>,<,>=,<=,==,!= instancepof逻辑运算符:&&,||,!位运算符:&,|,^,~,>>,<<,>>>(了解!!!)条件运算符?:扩展赋值运算符:+=,-=,*=,/=

    二元运算符

    package operator; public class Demo01 { public static void main(String[] args) { //二元运算符 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((double)a/b);//等于0.5,四舍五入后取0了 } } package operator; public class Demo02 { public static void main(String[] args) { long a = 131356465464465L; int b = 123; short c = 10; byte d =8; System.out.println(a+b+c+d);//Long System.out.println(b+c+d);//Int System.out.println(c+d);//Int //如果在一个数中或者多个数字有一个为Long那结果也为Long型,如果没有Long,那结果都为Int型,如果有一个数为double那结果一定为double。 } } package operator; public class Demo02 { public static void main(String[] args) { long a = 131356465464465L; int b = 123; short c = 10; byte d =8; System.out.println(a+b+c+d);//Long System.out.println(b+c+d);//Int System.out.println(c+d);//Int //如果在一个数中或者多个数字有一个为Long那结果也为Long型,如果没有Long,那结果都为Int型,如果有一个数为double那结果一定为double。 } } package operator; public class Demo02 { public static void main(String[] args) { long a = 131356465464465L; int b = 123; short c = 10; byte d =8; System.out.println(a+b+c+d);//Long System.out.println(b+c+d);//Int System.out.println(c+d);//Int //如果在一个数中或者多个数字有一个为Long那结果也为Long型,如果没有Long,那结果都为Int型,如果有一个数为double那结果一定为double。 } }

    逻辑运算符

    package operator; //逻辑运算符 public class Demo05 { public static void main(String[] args) { // 与(and) 或 (or) 非(取反) boolean a =true; boolean b = false; System.out.println("a && b:"+(a&&b));//逻辑与运算: 两个变量都为真,结果才为true System.out.println("a || b:"+(a||b));//逻辑或者运算:两个变量有一个变量为真,结果为true System.out.println("!(a && b):"+!(a&&b));//结果取反 //短路运算 int c = 5; boolean d = (c<4)&&(c++<4); System.out.println(d); System.out.println(c);//所以C++没有执行,因为当程序判断c<4不成立的时候已经停止执行剩下的代码了。 } }

    位运算

    package operator; //位运算 public class Demo06 { public static void main(String[] args) { /* A = 0011 1100 B = 0000 1101 ------------------------- A&B = 0000 1100 2个都为1的时候才为1 A|B = 0011 1101 2个只要有一个为1的时候那就是1 A^B = 0011 0001 两个相同为为0,不相同为1 ~B = 1111 0010 与两个完全相反,两个是0的时候为1,两个为1的时候为0,1 0 取1, 0 1 取 0. 面试题:2*8 =16 计算机 2*2*2*2 位运算效率极高 <<左移 *2 >>右移 /2 */ System.out.println(2<<3);//输出等于16,因为0000 0010 向左移动三位所以是0001 0000 /* 原因 0000 0000 0 0000 0001 1 0000 0010 2 0000 0011 3 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); //字符串连接符 + , String System.out.println(a+b); System.out.println(""+a+b);//都转换为String,然后进行连接 //面试题 System.out.println(""+a+b); System.out.println(a+b+""); //会问你有什么区别 //果然“”(字符串)在前面,那它就会都转化为String,然后进行连接,然后“”在后面,那它就会先进行运算。 } }

    三元运算符

    package operator; //三元运算符 public class Demo08 { public static void main(String[] args) { // x ? y : Z //如果x==true,则结果为y,否则结果为z int score = 50; String type = score <60 ?"不及格":"及格"; // 后面我们需要学习 if System.out.println(type); } }
    Processed: 0.009, SQL: 9