Java运算符

本篇介绍Java的运算符:算术运算符、关系运算符、逻辑运算符、位运算符、赋值运算符、条件运算符、instanceof运算符。

package com.example.javatest;
/**
 *  Author:W
 *  运算符
 *  1.算术运算符
 *  2.关系运算符
 *  3.位运算符
 *  4.逻辑运算符
 *  5.赋值运算符
 *  6.其他运算符:条件运算符、instanceof运算符
 */
public class OperatorTest {

    //算术运算符
    public  void  OperatorByArithmetic()
    {
        System.out.println("====算术运算符====");
        int a = 10;
        int b = 3;

        System.out.println("a+b = "+(a+b));
        System.out.println("a-b = "+(a-b));
        System.out.println("a*b = "+(a*b));
        System.out.println("a/b = "+(a/b));
        System.out.println("a%b = "+(a%b));
        System.out.println("a++ :"+a++);
        System.out.println("a-- :"+a--);
        System.out.println("++b : "+(++b));
        System.out.println("--b : "+(--b));

    }

    //关系运算符
    public  void OperatorByRelation()
    {
        System.out.println("====关系运算符====");

        int a = 3;
        int b = 5;

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

    }

    //位运算符
    public void  OperatorByByte()
    {
        System.out.println("====位运算符====");

        int a = 6;
        int b = 5;

        System.out.println("a&b="+(a&b));//与
        System.out.println("a|b="+(a|b));//或
        System.out.println("a^b="+(a^b));//异或
        System.out.println("~a="+(~a));//取反
        System.out.println("a>>2 ="+(a>>2));//右移
        System.out.println("a<<2 ="+(a<<2));//左移
        //按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。
        System.out.println("a>>>2 ="+(a>>>2));
    }

    //逻辑运算符
    public  void OperatorByLogic()
    {
        System.out.println("====逻辑运算符====");

        boolean a = true;
        boolean b = false;

        System.out.println("a&&b="+(a&&b));//与
        System.out.println("a||b="+(a||b));//或
        System.out.println("!a="+(!a));//非
    }

    //赋值运算符
    public  void OperatorByAssign()
    {
        System.out.println("====赋值运算符====");

        int a  =15;
        int b = 4;
        int c = 0;

        c = a + b;
        System.out.println("= :"+c);
        c += b;
        System.out.println("+= :"+c);
        c -= b;
        System.out.println("-= :"+c);
        c *= b;
        System.out.println("*= :"+c);
        c /= b;
        System.out.println("/= :"+c);
        c %= b;
        System.out.println("%= :"+c);
        c <<= 2;
        System.out.println("<<= :"+c);
        c >>= 2;
        System.out.println(">>= :"+c);
        c &= 2;
        System.out.println("&= :"+c);
        c ^=2;
        System.out.println("^= :"+c);
        c |= 2;
        System.out.println("|= :"+c);
    }

    //条件运算符
    public void OperatorByCondition()
    {
        System.out.println("====条件运算符====");
        int a = 5;
        int b = 3;
        int c = (a>b)?a:b;
        System.out.println("c="+c);
    }

    //对象类型检查
    public  void OperatorByInstance()
    {
        System.out.println("====instanceof运算符====");
        String  str = "W";
        System.out.println("str is String类型:"+(str instanceof String));
    }
}

测试脚本

package com.example.javatest;
/*
 *Author:W
 * 运算符测试
 */

public class MainTest {

    public static void main(String[] args)
    {
        OperatorTest operatorTest = new OperatorTest();
        operatorTest.OperatorByArithmetic();
        operatorTest.OperatorByRelation();
        operatorTest.OperatorByLogic();
        operatorTest.OperatorByByte();
        operatorTest.OperatorByCondition();
        operatorTest.OperatorByInstance();
        operatorTest.OperatorByAssign();
    }


}

运行结果如下:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值