运算符

一、算术运算符

 +、-、*、/、%、++、--

1. “+”运算符

package Variable;

public class Demo05Plus {
    public static void main(String[] args){
        /*
        1.对于数值,就是普通的加法
        2.对于char,short、byte会转为int再加
        3.对于字符串String,表示字符串连接操作;
            与数字连接也是变为字符串。
         */
        String str1="hello";
        String str2 = "world";
        System.out.println(str1+" "+str2);
        System.out.println(str1+20); //hello20


    }
}

2. “++” 自增

package Variable;

public class Demo06Operator {
    public static void main(String[] args){
        /*
        自增:++
        自减:--
        使用方式:
            1.单独使用:不与其他操作混合
            2.混合使用:与其他操作一起作为代码行一部分,num++、++num运算需注意顺序

         */
        int num1 = 10;
        System.out.println(num1++); //10 先打印再加

        System.out.println(num1); //11
        System.out.println(++num1); //12 先加再打印

        int x = 10;
        int y = 20;
        int r = ++x + y--;
        System.out.println(x); //11
        System.out.println(y); //19
        System.out.println(r); //31
    }
}

二、赋值运算符

=、+=、-=、*=、、/=、%=

 

三、比较运算符

==、<、>、<=、>=、!=

 

四、逻辑运算符

&&、||、| (非、取反)

package Variable;

public class Demo07Operator {
    public static void main(String[] args){
        System.out.println( true && false);
        int a = 10;
        System.out.println(true || (a++ == 1)); //短路或 ,遇到true,后面的不执行
        System.out.println(a); //10
        //"&&" "||"都是短路
        /*
        注意:逻辑运算符只能用于布尔值
         */
        System.out.println(!true);
    }
}

五、三元运算符

package Variable;

public class Demo10Operator {
    public static void main(String[] args){
        /*
        一元运算符
        二元运算符
        三元运算符:三个数据
        格式:
            数据类型 变量名称 = 条件判断 ? 表达式A : 表达式B
         */
        int a = 10;
        int b = 20;
        System.out.println(a > b ? a:b);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值