【Java学习笔记】JavaSE入门(4)

运算符

在这里插入图片描述

算术运算符

拓展:ctrl+D: 复制当前行到下一行。

+、—、*、/

package operator;
//算术运算符:+、—、*、/
public class Demo01 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 35;
        int d = 25;

        System.out.println(a+b); //30
        System.out.println(c-a); //25
        System.out.println(c*b); //700
        System.out.println((double)a/d); //0.4
        System.out.println(a/(double)d); //0.4
       /*
       注意:
       1.使用来运算的时候要注意结果是否有小数
       2.结果有小数时,不加double会自动舍去小数,取整数
        */
        }
}

%(取余)

package operator;
//算术运算符:%、++、--
public class Demo02 {
    public static void main(String[] args) {
    // %又称为取余
        int a1 =10;
        int a2 =21;
        System.out.println(a2%a1);
        //a2%a1   21/10=2...1
        // 取余为1
       

++(自增)

int a = 5;
System.out.println(a); //5

int b = a++; //执行完这行代码后,先给b赋值,再自增
//a++ a = a+1(待执行)
System.out.println(a); //6(执行后的结果)

//a++ a = a=1(待执行)
int c = ++a; //执行完在行代码后,先自增,再给c赋值
System.out.println(c); //7(执行后的结果)

System.out.println(a); //7
System.out.println(b); //5
System.out.println(c); //7

–(自减)

与自增一样

int c1 = 5;
int c2 = c1--;
System.out.println(c1);//4
int c3 = --c1;

System.out.println(c1); //3
System.out.println(c2); //5
System.out.println(c3); //3

关系运算符

>、<、>=、<=、==(等于)、!=(不等于)

package operator;

public class Demo03 {
    public static void main(String[] args) {

        int a =10;
        int b =20;

        System.out.println("a>b"); //false
        System.out.println("a<b"); //true
        System.out.println("a>=b"); //false
        System.out.println("a<=b"); //true
        System.out.println("a==b"); //false
        System.out.println("a!=b"); //true
    }
}

逻辑运算符

&& (与\and)

逻辑与运算:两个变量都为真,结果才为true

boolean a = true;
boolean b = false;

   System.out.println("a && b:"+(a&&b)); //false

|| (或\or)

逻辑或运算:两个变量有一个为真,则结果才为true

boolean a = true;
boolean b = false;

  System.out.println("a || b:"+(a||b)); //true

! (非/取反)

如果是真,则变为假;如果是假,则变为真

boolean a = true;
boolean b = false;

   System.out.println("!(a && b):"+!(a&&b)); //true

注意点:短路运算

第一个值被检测为错误,直接中断,不执行检查后一项。

int a1 =3;
boolean a2 = (a1>6)&&(++a1<100);
   System.out.println(a2); //false(因为短路,未被执行)
   System.out.println(a1); //3

位运算符(目前了解就好)

public class Demo05 {
 public static void main(String[] args) {
 /*
 &(与) |(或) ^(异或) ~(取反)
 A = 0100 0011
 B = 1100 1010
 --------------------------
 A&B = 0100 0010 (对应相乘得结果)
 A|B = 1100 1011 (有1取1,都是0取0)
 A^B = 1000 1001 (相同取0,不同取1)
 ~A = 1011 1100  (相反)
 ~B = 0011 0101
 << (左移):*2
    >>(右移):/2

    0000 0000  0
    0000 0001  1
    0000 0010  2
    0000 0011  3
    0000 0100  4
    0000 1000  8
    0001 0000  16
    0010 0000  32

例:如何最快算出2*8=16?
     */
        System.out.println(2<<3); //16
    }
}

三元运算符

条件运算符(必须掌握)

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

     int score =115;
     String type = score <90?"不及格":"及格";
        System.out.println(type);
        //及格
    }
}

扩展(符)

+=、-=(偷懒用的)

public class Demo06 {
    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+=b); //30
        System.out.println(a-=b); //10
    }
}

字符串连接符 + , string

public class Demo06 {
    public static void main(String[] args) {
int c = 30;
int d = 40;
    System.out.println(""+c+d); //3040(连接)
    System.out.println(c+d+""); //70
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值