java基础02

运算符

算数运算符:+,-,*,/,%,++,--

赋值运算符:=

关系运算符:>,<,>=,<=,==(java的等于是用两个等于判断),!=instanceof

逻辑运算符:&&(与),||(或),!(非)

位运算符:&,|,^,~,>>,<<,>>>

赋值条件运算符

扩展赋值运算符:+=,-=,*=,/=

package operator;

import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);//long
        System.out.println(a+c+d);//int
        System.out.println((double)(c+d));//int

    }
}
package operator;

import java.util.Arrays;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误    布尔值
        //if
        int a = 10;
        int b = 20;
        int c = 21;
        //取余,模运算
        System.out.println(c%a);//c/a 21/10 = 1
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

自增自减

a++,先赋值再自增 减号同理

++a,先赋值再自增

public class Demo04 {
    public static void main(String[] args) {
        int a = 3;
        int b = a++;  //a++ a=a+1(先给b赋值,再自增
        int c = ++a;  //先自增再赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}
/*
5
3
5
*/

幂运算

很多运算,要使用一些工具类来操作

public class Demo04 {
    public static void main(String[] args) {
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}
/*
8
*/

逻辑运算符

与(and) 或(or) 非

public class Demo05 {
    public static void main(String[] args) {
        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));//如果是真,则变假,如果为假则变真
    }
}
/*
false
true
true
*/

在与运算当中第一个为false则后面均不执行(短路)

位运算(效率高)

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


    /*
    A = 0011 1100
    B = 0000 1101
    ----------------
    A&B = 0000 1100
    A|B = 0011 1101
    ~B = 1111 0010
    <<  *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
*/

字符串

package operator;

import java.util.Arrays;

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);
        //字符串连接符 + ,Steing
        System.out.println(""+a+b);//字符串在前,后面进行拼接
        System.out.println(a+b+"");//字符串在后,正常运算
    }
}
/*
10
1020
30
*/

三元运算符

public class Demo08 {
    public static void main(String[] args) {
        //x? y: z
        //如果x==true,则结果为y,负责结果为z
        int score = 80;
        String type = score <60?"不及格":"及格";
        //if
        System.out.println(type);
    }
}
/*
及格
*/

包机制

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

为了能够使用某一个包的成员,我们需要在java程序中明确导入该报。

使用"import"语句可以完成此功能

import packag1[,pacage2...].(classname|*)

JavaDoc

@author作者名

@version版本号

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

@param参数名

@return返回值情况

@throws异常抛出情况

通过命令行 javadoc 参数 java 文件

posted @ 2022-08-18 17:58  joker丶le  阅读(26)  评论(0)    收藏  举报