day2 2021 2 2 变量 常量 命名规则 运算符

变量

分为

类变量 static

实例变量

局部变量

public class Demo2 {//类
    //属性:变量

    //类变量 static
    static  int salary = 2500;

    //实例变量:从属于对象 从属于类 在类里面 方法外面
    //如果不进行变量的赋值,就会输出这个类型的默认值  布尔值默认是false
    //除了八个基本类型,剩下的默认值都是null
    String name;
    int age;

    //main 方法
    public static void main(String[] args) {//方法
        //局部变量(在方法里面):必须声明和初始化值
        int i = 10;

        System.out.println(i);

        //变量类型(自定义类型 属于引用类型) 变量名字 = new Demo2()
        Demo2 Demo2 = new Demo2();
        System.out.println(Demo2.age);
        System.out.println(Demo2.name);

        //类变量 static
        System.out.println(salary);


    }
}

常量

初始化之后就不能再改变的值

final 常量名=常量值

final double PI = 3.14;

常量名一般使用大写

public class Demo3 {
    //修饰符 可以不存在先后顺序 所以static可以和final互换位置
    static  final int A = 2000;
    static  final  double PI = 3.14;
    public static void main(String[] args) {
        System.out.println(A);
        System.out.println(PI);
    }
}

变量的命名规范

  • 所有变量见名知意
  • 类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线:MAX_VALUE
  • 类名:首写字母大写和驼峰原则:GoodMan
  • 方法名:首字母小写和驼峰原则

运算符

  • 算数运算符:+, - ,*, / ,%(取余)(模运算),++ (自增),–(自减)
  • 赋值运算符:=
  • 关系运算符:>, <, >=, <=, ==(判断等于),!= (不等于), instanceof
  • 逻辑运算符:&&(与),||(或),!(非)
  • 位运算符:&,|,^,~,>>,<<,>>>
  • 条件运算符:?:
  • 扩展赋值运算符:+=,-=,*=,/=
package 运算符;

public class Demo1 {
    public static void main(String[] args) {
        //二元运算符
        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((double) a/b);//运算中可能会出现小数而两个int类型运算仍是int类型,所以要加上double类型


    }
}

若有double类型则结果为double类型

若没有double类型有long类型 则结果为long类型

若没有double也没有long则全为int类型

package 运算符;

public class Demo2 {
    public static void main(String[] args) {
        long a = 233333333L;
        int  b = 2000;
        short c = 10;
        byte d = 8;

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

关系运算符

package 运算符;

public class Demo3 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

求余

package 运算符;

public class Demo3 {
    public static void main(String[] args) {
        int a = 10;
        int b = 21;

        //取余,模运算
        System.out.println(b%a);//求b/a后所得的余数

    }
}

自增 自减

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

        //++  --  自增 自减 一元运算符

        int a = 3;
        int b = a++;
        //a++ 表示先把a赋值给b 再赋值a=a+1

        int c = ++a;
        //++a 表示先a=a+1 再把此时的a赋值给c
        //同时a的值一直在变化

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

----------------------------------------------
最后得到结果
5
3
5

幂运算

package 运算符;

import com.sun.java.swing.plaf.windows.WindowsToggleButtonUI;

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

       //幂运算 2的三次方
       double pow = Math.pow(2,3);
        System.out.println(pow);

    }
}

逻辑或与非运算

package 运算符;

public class Demo5 {
    public static void main(String[] args) {
        //与(&&)(and) 或(||)(or) 非(!)(取反)

        boolean a = true;
        boolean b = false;

        //逻辑与运算:两个变量都为真 才为真
        System.out.println("a&&b:"+(a&&b));
        //逻辑或运算:两个变量里有一个为真就为真
        System.out.println("a||b:"+(a||b));
        //逻辑非运算:真变为假 假变为真
        System.out.println("!(a&&b):"+!(a&&b));
    }
}


----------------------------------------------

输出答案:
false
true
true

短路运算

package 运算符;

public class Demo6 {
    public static void main(String[] args) {
        int c = 5;
        boolean a = (c<4)&&(c++<4);
        System.out.println(a);
        System.out.println(c);
    }
}

-------------------------------------------------
输出结果
false
5

短路运算 因为从c<4为假 而&&的判定为两个都为真才为真

所以在c<4就结束运算 c++<4没有运行 所以c++<4的判断被短路了

所以c输出的值为5

若c<4改为c<6 则正确 需要继续验证c++<4的正误,此时再输出c就会得到c的值为6

位运算

A=0011 1100

B=0000 1101

A&B(对应位置上都为1才为1)=0000 1100

A|B(对应位置上都为0才为0)=0011 1101

A^B(对应位置相同则为0不同则为1)=0011 0001

~B(完全相反)=1111 0010

<<(左移) <<(右移

<< 相当于*2 <<相当于/2

public class Demo6 {
    public static void main(String[] args) {
        System.out.println(2<<3);
    }
}

-------------------------------------------------
得到结果:
16    (2<<3表示2左移三次 即*2 三次)

扩展赋值运算符

a+=b 即为 (a=a+b)

a-=b 即为 (a=a-b)

字符串连接符

直接在字符上相加

public class Demo7 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        System.out.println(""+a+b);
    }
    
    ---------------------------------------------
    得到结果:
    1020

三元运算符

public class Demo7 {
    public static void main(String[] args) {
        //x?y:z
        //如果x==ture,则结果为y,否则结果为z;

        int socre = 88;
        String type = socre <60?"不及格":"及格";
        System.out.println(type);
    }
}

优先级:用()表明运算的优先

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值