Java运算符

运算符(operotor):

java支持如下运算符

  1. 算数运算符:+、-、*、/、%(取余数,模运算)、++、--
  2. 赋值运算符:==
  3. 关系运算符:>,<,>=,!=instanceof
  4. 逻辑运算符:&&,||,!
  5. 位运算符:&,|,^,~,>>,<<,>>>(了解)
  6. 条件运算符:?,:
  7. 拓展运算符:+=,-=,*=,/=
public class Demo05 {
    public static void main(String[] args) {
        //++   —— 自增  自减  一元运算符
        int a = 3;
        int b = a++;
        //a = a+1;
        System.out.println(a);//输出值为4,执行完这行代码后,先给b赋值,再自增
        int c = ++a;//执行完这行代码前,先自增,再给c赋值;
        System.out.println(a);//输出值为5
        System.out.println(b);//输出值为3
        System.out.println(c);//输出值为5
        //幂运算 2^3  很多时候我们会使用一些工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);


    }
}

++在前,先+后用;++在后,先用后加

public class Demo06 {
    public static void main(String[] args) {
        //二元运算符
        //ctrl +D:复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
    }
}

逻辑运算符

public class Demo07 {
    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));
        //如果是真,则变为假;如果是假,则变为真;


    }
}
public class Demo08 {
    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);
        //字符串连接符   +  ,String
        System.out.println(""+a+b);//输出1020,把a和b连接起来
        System.out.println(a+b+"");
    }
}

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

一般利用公司域名倒置作为包名

为了能够实用某一个包的成员,我们需要在Java程序中明确代入该包。使用import语句可完成此功能;

//import com.kuang.base.*

//导入这个包所有的类。.*(通配符)

posted @ 2020-06-19 20:05  一路向北zzz  阅读(87)  评论(0编辑  收藏  举报