ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

运算符的基本运算

2022-09-12 22:06:05  阅读:240  来源: 互联网

标签:基本 运算 int System 运算符 println public out


运算符

Java支持如下运算符

  • 算术运算符:+,-,*,/,%(取余,又叫模),++,--

  • 赋值运算符:=

  • 关系运算符:>,<,>=,<=,==,!=,instanceof

  • 逻辑运算符:&&,||,!

  • 位运算符:&,|,^,~,>>,<<,>>>(了解就行)

  • 条件运算符:?,:

  • 拓展运算符:+=,-=,*=,/=


运算符举例

package Demo;

public class Demo01 {
   public static void main(String[] args) {
       //二元运算符
       int a = 10;
       int b = 20;
       int c = 30;
       int d = 40;

       System.out.println(a+b);
       System.out.println(a-b);
       System.out.println(a*b);
       System.out.println(a/(double)b);  // int/int不等于double, 需要将其中一个强制转换
  }
}


===============================================================
//执行结果
30
-10
200
0.5

package Demo;

public class Demo02 {
   public static void main(String[] args) {
       long a =1231231234L;
       int b = 123;
       short c = 10;
       byte d = 8;
       
       //转型问题
       //有long为long,无long都为int,无论有无int操作
       //有double一定转换为double
       System.out.println(a+b+c+d);  //Long
       System.out.println(b+c+d);    //int
       System.out.println(c+d);      //int
       System.out.println((double)c+d);  //double
  }
}


===============================================================
//执行结果
1231231375
141
18
18.0

package Demo;

public class Demo03 {
   public static void main(String[] args) {
       //关系运算符返回结果: 正确,错误   布尔值
       //常与if使用
       int a = 10;
       int b = 20;

       System.out.println(a>b);
       System.out.println(a<b);
       System.out.println(a=b);
       System.out.println(a!=b);

       //取余运算符号%
       System.out.println(b%a);
  }
}

===================================================================
//执行结果
false
true
20
false
0

package Demo;

public class Demo04 {
   public static void main(String[] args) {
       // ++   --   自增,自减   一元运算符
       int a = 3;

       int b = a++;   //a+1之前先将a原来的值赋予b

       //上一步操作后a值为4
       System.out.println(a);
       System.out.println(b);

       int c = ++a;   //将a赋予c之前先将a+1,再赋值给c

       //上一步执行后a值为5
       System.out.println(a);
       System.out.println(c);

       //=====================================================
       //幂运算2^3   2*2*2 = 8   很多运算,我们会使用一些工具类来操作!
       double pow = Math.pow(2,3);
       System.out.println(pow);
  }
}

================================================================
//执行结果
4
3
5
5
8.0

package Demo;

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);  //(c<4)已经为false,故后面不执行,此时c仍为5
       System.out.println(d);
       System.out.println(c);
  }
}


=============================================================
//执行结果
a && b :false
a || b :true
!(a || b) :true
false
5
package Demo;

public class Demo06 {
   public static void main(String[] args) {
       /*
       A = 0011 1100
       B = 0000 1101
       --------------------------------------------------
       //位运算
       A&B = 0000 1100 //A与B:两个都是1才为1,其余全为0
       A/B = 0011 1101 //A或B:如果对应位都是0,则为0,否则为1
       A^B = 0011 0001 //异或:相同则为0,不同为1
       ~B = 1111 0010
       
       ===================================================
       面试题:如何计算2*8最快?
       
        2*8 = 16   2*2*2*2
        左移:<<
        右移:>>

        0000 0000   0
        0000 0001   1
        0000 0010   2
        0000 0011   3
        0000 0100   4
        0000 0101   5
        0000 0110   6
        0000 0111   7
        0000 1000   8
        0001 0000   16

        */
       System.out.println(2<<3);
  }
}

======================================================================
//执行结果
16

package Demo;

public class Demo07 {
   public static void main(String[] args) {
       int a = 10;
       int b = 20;

       a+=b;   // a = a+b,此时a值为30
       System.out.println(a);

       a-=b;   // a = a-b,此时a值为30-20
       System.out.println(a);

       //字符串连接符 + ,String,只要有其中一个加数为字符串类型,其余则转为字符串连接
       System.out.println(a+b);
       System.out.println(""+a+b);  //字符串在前面=字符串连接
       System.out.println(a+b+"");  //字符串在后面=整型相加
  }
}

==================================================================
//执行结果
30
10
30
1020
30

package Demo;

//三元运算符
public class Demo08 {
   public static void main(String[] args) {
       // x ? y : z
       //如果x==true,则结果为y,否则结果为z

       int score1 = 80;
       int score2 = 59;
       String type1 = score1 <60 ?"不及格":"及格";
       String type2 = score2 <60 ?"不及格":"及格";

       System.out.println(type1);
       System.out.println(type2);
       //通常用if,但是以上方法必须掌握
  }
}

==================================================================
//执行结果
及格
不及格

狂神说该节链接

狂神说--运算符

免费课程哦!

 

标签:基本,运算,int,System,运算符,println,public,out
来源: https://www.cnblogs.com/bobocha/p/16687368.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有