java入门 (三) 运算部分

java基础运算部分(三)

基本运算符,关系运算符

自增自减运算符,初始Math类

逻辑运算符,位运算符

三元运算符

包机制

javaDoc生成文档

1.基本运算符

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl+D 复制当前行到下一行
        int a = 10;
        int b = 20;
        int c = 10;
        int d = 10;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//从高到低,需要强转
    }
}
public class Demo2 {
    public static void main(String[] args) {
        Long a = 11111111111L;
        int b =  123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);//有Long转为最高级别Long

        //不同类型混合运算无论是赋值还是运算都向最高位转
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
    }
}

2.关系运算符:

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符结果位布尔值  false 或 true
        int a = 10;
        int b = 20;
        int c = 22;

        //取余,模运算
        System.out.println(c%a);
        
        
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
    }
}

3.自增自减运算符

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


        // a = a + 1
        int c = ++a;//执行这段代码前,先自增,再给c赋值
        System.out.println("a="+a);
        System.out.println("b="+b);
        System.out.println("c="+c);
        
        //幂运算 2的三次方 2*2*2 = 8 很多运算,我们会用工具类
        double pow = Math.pow(3,2);
        System.out.println(pow);
    }
}

4.逻辑运算符

public class Demo05 {
    public static void main(String[] args) {
        //  &&与(and)  ||或(or)   !非(取反)
        boolean a = true;
        boolean b = false;
        System.out.println("a&&b="+(b&&a));//逻辑运算,两个变量都为真,结果才为true
        System.out.println("a||b="+(a||b));//逻辑运算,两个变量有一个为真,结果为true
        System.out.println("!(a&&b)="+!(a&&b));//逻辑运算,如果为真,则为假。如果为假,则为真


        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);//代码先执行&&前的代码,c<9为false,故c++<10不会执行
        System.out.println(c);
        System.out.println(d);
    }
}

5.位运算

二进制打交道,简单理解,目前用不到,记住左移右移就好

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
        --------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B  = 1111 0010

        扩展面试题:
        2*8=16 2*2*2*2
        效率及高!
        << 左移 *2
        >> 右移 /2
        0000 0000 0
        0000 0001 1
        0000 0010 2
        0000 0011 3
        0000 0100 4
        0000 0101 5
        0000 0110 6
        0000 0111 7
        0000 1000 8
        0001 0000 16

        */
        System.out.println(2<<3);//输出为16
    }
}

6.拓展赋值运算符

public class Demo07 {
    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);//String在前,会拼接,结果为1020
        System.out.println(a+b+"");//结果为正常30
    }
}

7.三元运算

//三元运算符
public class Demo08 {
    public static void main(String[] args) {
        // x ? y :z
        //如果x==true,则结果为y,否则结果为z
        // 学好java ? 娶妻生子 :工地搬砖
        int score = 50;
       String type= score > 60 ?"及格":"不及格";
        System.out.println(type);
    }
}

()括号里面的是优先级。

8.包机制

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

包名小写

其他的太简单了,自己看一遍就会

9.javaDoc生成文档

javaDoc命令是用来生成自己API文档的

参数信息:

@author 作者名

@version 版本好

@since 指明需要最早使用的JDK版本

@param 参数名

@return 返回值情况

@throws 异常抛出情况

学习笔记,有认真按照视频总结,下边附上链接

点击此处进入B站链接

posted @ 2020-12-23 15:02  Godwin_Zhang  阅读(153)  评论(0编辑  收藏  举报