java第2课-变量类型

1. 变量

  1. 局部变量
    • 使用前必须声明和初始化值,否则会报错
    int a = 1;
    System.out.println(a);//局部变量使用前必须声明和初始化值,若没有初始化值会报错
    

    输出结果:

    1

  2. 实例变量
    • 实例变量的声明位于类的里面,方法的外边

    • 实例变量从属于某个的一个对象,所以只有在 new一个类的对象后,才可以使用该对象的实例变量

    • new一个类的对象时,可以不给它的实例变量初始化值,这时它的实例变量的默认值为null或0

    public class Test {
    
        String name;//声明实例变量
        int age;//声明实例变量
    
        public static void main(String[] args) {
    
            Test test = new Test();//实例变量的使用,test是Test类的对象
            //test.name和test.age都是对象test的实例变量
            
            System.out.println(test.name);
            System.out.println(test.age);//实例变量的默认初始值为null或0
    
            test.name = "狂神";//给实例变量赋值
            test.age = 18;//给实例变量赋值
            System.out.println(test.name);
            System.out.println(test.age);
            
        }
    }
    

    输出结果:

    null
    0
    狂神
    18

  3. 类变量
    • 类变量的声明位于类的里面,方法的外边

    • 需要在数据类型前面加上修饰符static

    • java类变量的作用与c++的全局变量的作用相同,方法可以直接对类变量初始化值和调用

    public class Test {
    
        static String name;//声明类变量(与c++的全局变量类似)
        static int age;
    
        public static void main(String[] args) {
    
            String name = "狂神";
            int age = 18;
            System.out.println(name);
            System.out.println(age);
    
        }
    }
    

    输出结果:

    狂神
    18

2. 常量

  • 常量的声明位于类的里面,方法的外边,修饰符finalstatic配合使用

    public class Test {
    
        final static double PI = 3.1415;//常量的声明
    
        public static void main(String[] args) {
    
            System.out.println(PI);
    
        }
    }
    

    输出结果:

    3.1415

  • 常量初始化值后,在程序运行的过程中,常量的值不会改变

  • 常量名一般使用大写字母

    final static double PI = 3.1415;
    
    static final double PI = 3.1415;
    
  • 修饰符的先后顺序没有实际影响,上面两端代码等价

3. 命名规范

  • 类名:首字母大写+驼峰原则。例如:ScannerDemo
  • 方法名:首字母小写+驼峰规则。例如:insertSort
  • 变量:首字母小写+驼峰规则。例如:monthSalary
  • 常量:大写字母+下划线。例如:MAX_VALUE

4. 运算符

  1. 位运算符
    • A&B 按位取与
    • A|B 按位取或
    • A^B 按位取异或
    • ~A 按位取反
    int num1 = 115;         //num1的补码为:0000 0000 0000 0000 0000 0000 0111 0011
    int num2 = 146;         //num2的补码为:0000 0000 0000 0000 0000 0000 1001 0010
    int num3 = num1&num2;   //num3的补码为:0000 0000 0000 0000 0000 0000 0001 0010
    int num4 = ~num1;       //num4的补码为:1111 1111 1111 1111 1111 1111 1000 1100
                            //num4的真值为:1000 0000 0000 0000 0000 0000 0111 0100
    System.out.println(num3);
    System.out.println(num4);
    

    输出结果:

    18
    -116

  2. 算术运算符
    • +, -, *, /, %, ++, --

      int a = 1;
      int b = 2;
      int d = a++ + ++b;
      System.out.println(d);
      

      输出结果:

      4

      int d = a+++++b;//此语句错误
      

      报错!

      因为++的优先级高于+,编译器从右至左扫描,会将其认为是(a+)++(++b),而(a+)不能充当左值,(++b)也不是右值,所以报错

  3. 关系运算符
    • >, <, >=, <=, ==, !=, instanceof
  4. 逻辑运算符
    • &&, ||, !
  5. 条件运算符
    • A?B :C
    • 条件运算符是一个三元运算符
    int score = 80;
    String grade = score<60 ? "不及格" : "及格";
    System.out.println(grade);
    

    输出结果:

    及格

  6. 拓展赋值运算符
    • +=, -=, *=, /=
    int a = 10;
    int b = 20;
    a += b;//等价于a = a+b;
    System.out.println(a);
    

    输出结果:

    30

  7. 字符串连接符
    • ""+
    int a = 10;
    int b = 20;
    System.out.println(""+a+b);//输出的是字符串类型
    System.out.println(a+b+"");//输出的是字符串类型,运算顺序是从左到右
    

    输出结果:

    1020

    30

posted @ 2021-11-29 09:59  以学愈愚  阅读(52)  评论(0)    收藏  举报