变量、常量、运算符(位运算)

1.变量


public class HelloWorld {

    //类变量
    static double salary;

    //实例变量 默认值
    String name;
    int age;
    boolean flag;
    public static void main(String[] args) {

        HelloWorld helloWorld = new HelloWorld();
        System.out.println(helloWorld.name); // null
        System.out.println(helloWorld.age); // 0
        System.out.println(helloWorld.flag); // false
        
        System.out.println(salary);//0.0
        
        //局部变量
        char a='中';

    }
}

实例变量的默认值,int是0,double是0.0,boolean是false,除了基本类型其他的默认值全是null

2. 常量

public class HelloWorld {

    //常量
    final static int age = 18;
    //修饰符不区分前后顺序
    static final String name = "小明";

    public static void main(String[] args) {
        System.out.println(age);
        System.out.println(name);
    }
}

3.运算符

整数类型字段相加:

​ 如果字段中有long类型字段,结果也是long类型;否则为int类型,即使运算字段中不含有int类型字段

浮点数类型字段相加:

​ 如果字段中有double类型字段,结果也是double类型,否则为float

public static void main(String[] args) {

        long a = 1235465646L;
        int b = 240;
        short c = 150;
        byte d = 10;

        System.out.println(a+b+c+d); //long
        System.out.println(b+c+d); // int
        System.out.println(c+d); // int
        
    }


 public static void main(String[] args) {
        
        double a = 150.0;
        float b = 20.0F;
        float c = 10.0F;
        System.out.println(a+b+c);//double
        System.out.println(a+b);//double
        System.out.println(b+c);//float

    }

4.位运算

  		/**
         * A = 0000 1100
         * B = 0011 1101
         *
         * A&B 0000 1100 与运算 都是1时为1,否则为零
         * A|B 0011 1101 或运算 都是零时为零,否则为1
         * A^B 0011 0001 亦或运算,位置相同的数字相同为零,否则为1
         * ~B  1100 0010 取反*
         *
         *
         * << 左移
         * >> 右移
         * 面试题 2*8 怎样运算最快?
         * 2<<3 0000 0010  --> 0001 0000
         */
        System.out.println(2<<3);

5.字符串连接符

  	int a= 10;
    int b =20;

    System.out.println(""+a+b); //1020
    System.out.println(a+b+""); //30

学习地址:狂神说视频链接

posted @ 2022-03-14 22:42  梁小白123  阅读(50)  评论(0)    收藏  举报