ICode9

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

基本运算符

2022-01-02 21:31:12  阅读:109  来源: 互联网

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


运算符

Java语言支持的运算符

  • 算术运算符:+、-、*、/、%、++、--
  • 赋值运算符:=
  • 关系运算符:>、<、>=、<=、==、!=、instanceof
  • 逻辑运算符:&&、||、!
  • 位运算符:&、|、^、>>、<<、>>>
  • 条件运算符:? :
  • 扩展赋值运算符:+=、-=、*=、/=

自增自减运算符

一元运算符 ++、--

public class Demo03 {
    public static void main(String[] args) {
        int a = 3;
        int b = a++;//a++ a = a+1;执行完这行代码后,先给b赋值,再自增
        System.out.println(a);
        int c = ++a;//++a  a = a+1;执行完这行代码前,先自增,再给c赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(b);
        System.out.println(c);
        System.out.println(c);

    }
}

1640843847781

Math类

 double s = Math.pow(2,8);
        System.out.println((int)s);

得到结果 256

逻辑运算符

public class Demo04 {
    public static void main(String[] args) {
        //与或非 and/or/取反
        boolean a = true;
        boolean b = false;
        System.out.println("a&&b:"+(a&&b));
        System.out.println("a||b:"+(a||b));
        System.out.println("!(a&&b):"+!(a&&b));
    }
}

1640844276091

位运算

public class Demo05 {
    public static void main(String[] args) {
        /**
         * A = 0011 1100
         * B = 0000 1101
         *
         * A&B = 0000 1100  交
         * A|B = 0011 1101  并
         * A^B = 0011 0001 异或
         * ~B = 1111 0010  取反
         *
         * 2*8=16  2*2*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);
    }
}
  • 位运算优点:效率高

三元运算符

public class Demo07 {
    public static void main(String[] args) {
        //x ? y : z
        //如果x==true,则结果为y,否则结果为z
        int score = 80;
        String type = score < 60 ?"不及格":"及格";
        System.out.println(type);
    }
}

结果为:及格

+=、-=、*=、/=

public class Demo06 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;
        System.out.println(a);

        //字符串连接符 +,String
        System.out.println(""+a+b);
        
        System.out.println(a+b+"");
    }
}

1640845128927

标签:基本,0000,System,运算符,println,public,out
来源: https://www.cnblogs.com/lunai/p/15758386.html

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

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

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

ICode9版权所有