Day5 2.8

一、运算符

1.算术运算符

+,-,*,/,%(取余),++,--

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

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

        //乘方 2^3=2*2*2=8
        double pow =Math.pow(2,3);
        System.out.println(pow);

    }
}
输出结果:
      4
      5
      3
      5
      8.0

2.赋值运算符

=

3.关系运算符

<,>,<=,>=,==,!=

4.逻辑运算符

&&,||,!

public class Class02 {
    public static void main(String[] args) {
        boolean a=true;
        boolean b=false;

        System.out.println("a&&b:"+(b&&a));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a||b:"+(a||b));//逻辑或运算:两个变量有一个为真,则结果为ture
        System.out.println("!(a&&b):"+!(a&&b));//非  (取反)

        //短路运算
        int c = 5;
        boolean d=(c<4)&&(c++<4); //当程序运行到从c<4时,结果必为false,所以不会执行c++<4,c的数值仍为5
        System.out.println(d);
        System.out.println(c);
      }
}

5.位运算符

&,|,^,~,>>,<<

public class Class03 {
    public static void main(String[] args) {
        /*
    A = 0011 1100
    B = 0000 1101
    --------------
  A&B = 0000 1100 (与)
  A|B = 0011 1101 (或)
  A^B = 0011 0001 (相同为0,不同为1)
  ~B  = 1111 0010 (非)


        2*8怎么最快?  2*2*2*2
        效率极高!
        << 左移   *2
        >> 右移   /2

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

     */
        System.out.println(2<<3);
    }

}
输出结果:
    16

6.条件运算符

?:

7.扩展赋值运算符

+=,-=,*=,/=

public class Class04 {
    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); //转换为字符并连接
        System.out.println(a+b+"");
        
        //三元运算符  ?:
        /*
        x ? y : z
        如果x==true,则结果为y,否则结果为z
         */
    }
}

二、包机制

本质就是文件夹

1.包的定义

package pkg1[.pkg2[.pkg3...]]

  • 一般利用公司域名倒置作为包名; com.baidu.www

2.包的导入

import package1[.package2...].(classname|*);

* :通配符 ,会导入类中所有class

posted on 2021-02-08 17:54  羲和i  阅读(60)  评论(0)    收藏  举报