Java基础知识3

2020-03-07  本文已影响0人  cheerfish

Java基础知识3

运算符

Java语言支持如下运算符

package operator;

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

        System.out.println(a+b); //30
        System.out.println(a-b); //-10
        System.out.println(a*b); //200
        System.out.println(a/b); //0
        System.out.println(a/(double)b); //0.5

        System.out.println("===================================");

        long c = 100000000000000000L;
        int d = 1000000000;
        short e = 32760;
        byte f = 127;

        System.out.println(c+d+e+f); //100000001000032887  Long (有long结果为long)同样理解double
        System.out.println(d+e+f);  //1000032887 Int
        System.out.println(e+f);  //32887 Int   (结果默认为Int)

        //关系运算符返回的结果:true false 布尔值
        //if

        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(f%a);  // 7 (127/10=12余7)


    }
}


package operator;

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

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

        //幂运算  2^3 = 2*2*2 = 8  很多运算 会使用一些工具类来操作
        double pow = Math.pow(2,3);
        System.out.println(pow);  //8.0
    }
}

位运算符

A = 0011 1100

B = 0000 1101


A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001 如果相同就为0 不相同就为1

~B = 1111 0010


面试题  2x8 = 16         2x2x2x2
<<  x2
>>  /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
package operator;

//逻辑运算符
public class Demo03 {
    public static void main(String[] args) {
        // 与(and) 或(or)  非(取反)
        boolean a = true;
        boolean b = false;

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

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);  //前面已为false 后面不会再计算
        System.out.println(d);  //false
        System.out.println(c);  //5
    }
}

package operator;

public class Demo04 {
    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);

        //字符串连接符    +
        System.out.println(a+b);  //30
        System.out.println("a"+a+b);  //a1020 字符后不运算直接连接
        System.out.println(a+b+"a");  //30a 前面还是运算

        //三元运算符

        //x ? y : z
        //如果x==true  则记过为y 否则为z

        int score = 80;
        String type = score < 60 ?"不及格":"及格";  //必须掌握 很精简
        //可以用 if
        System.out.println(type);  //及格

    }
}


包机制 JavaDoc

包机制

JavaDoc

package com.cheer.base;

/**
* @author cheerfish
* @version 1.0
* @since 1.8
* */                //这是类注释
public class Doc {

    String name;

    /**
     *
     * @param name
     * @return
     * @throws Exception
     */                  //这是方法注释
    public String test(String name) throws Exception{
        return name;
    }

    //通过cmd命令行生产JavaDoc     javadoc -encoding UTF-8 -charset UTF-8 Doc.java

    //使用IDEA生产JavaDoc
}


image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读