Day 3 变量、运算符

Day 3 变量、运算符

变量

package base;

public class demo08 {

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

    //属性:变量

    //实例变量  从属于对象;如果不自行初始化,这个类型的默认值  0   0.0
    //布尔值:默认为false
    //除了基本类型,其余的默认值都是null;
    String name;
    int age;


    //main方法
    public static void main(String[] args) {

        //局部变量在这里面书写,必须声明和初始化值
        int i =10;

        System.out.println(i);

        //变量类型  变量名字 = new base.demo08();
        demo08 demo08 = new demo08();
        System.out.println(demo08.age);
        System.out.println(demo08.name);

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


    }

    //其他方法
    public void add(){


    }

}

package base;

public class demo09 {

    //修饰符,不存在先后顺序 如final
    static final double PI =3.14;

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

    }
}



运算符

package operater;

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);
    }
}


package operater;

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123L;
        int b =123;
        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
        //在运算中如果有Long类型,结果也为Long类型;其余任何类型运算都会转化为Int类型

    }
}


package operater;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误   布尔值表示
        //if

        int a = 10;
        int b = 20;
        int c = 21;

        //取余 也叫模运算
        System.out.println(a<b);
        System.out.println(a>b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);  //  c / a   21/10=2......1

    }
}


package operater;

public class Demo04 {
    public static void main(String[] args) {
        //++   --    自增,自减   一元运算符
        int a = 3;

        int b =a++;  //执行完这行代码后,先给b赋值,再自增
        //a++    a=a+1
        System.out.println(a);

        int c =++a;  //执行完这行代码前,先自增,再给b赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        //也就是三个运算式是按先后顺序从上到下叠加的,在这个期间a的值会因自增而不断增大,所以最后输出的a会与原来的a不同,而其他的结果b、c会因++位置不同而产生不同结果
        //幂运算 需要引用 特殊处理
    }
}


package operater;

//逻辑运算符
public class Demo05 {
    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));//  如果是真则为假

        //短路运算
        int c =5;
        boolean d = (c<4)&&(c++<4);  //在c<4时运算短路,后续运算没有执行!!!
        System.out.println(d);
        System.out.println(c);  //输出为5 反推上式c++并没有运算

    }
}


package operater;

public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

        A&B = 0000 1100  1代表true 0代表false 运算遵循逻辑与运算
        A/B = 0011 1101  同上,原理一致
        A^B = 0011 0001  两个相同就为0 不同为1
        ~B = 1111 0010   与原来的B完全相反

        2*8 = 16   2*2*2*2
        效率极高!!!
        <<  左移  *2
        >>  右移  /2
         */

        System.out.println(2<<3);
    }
}


package operater;

//三元运算符
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);
    }
}

变量的命名规范在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学习争取保研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值