JAVA第二天

变量、常量、作用域

1、变量or作用域

public class demo07 {

    // 类变量
    static double money = 1.231;

    // 实例变量
    String name;
    int num;

    public static void main(String[] args) {
        // 局部变量 只能在对应方法中运行
        int i = 10;
        System.out.println(i);
        // 实例变量
        demo07 demo07 = new demo07();
        System.out.println(demo07.name);
        System.out.println(demo07.num);
        // 类变量
        System.out.println(money);
    }
}

2、常量

public class demo08 {
    // final 表示常量  常量不可修改编辑
    static final double money = 3.14;

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

3、计算

package operater;

public class Demo {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int c = 10;
        int d = 10;
        int e = 10;

        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / (double)b); // 如果除的值带整数,可结果需要是小数,就需要使用强制转换
    }
}

4、数据类型由低到高

package operater;

public class Demo2 {
    public static void main(String[] args) {
        long a = 12314231L;
        int b = 123;
        short c = 467;
        byte d = 55;
        System.out.println(a+b+c+d); // 由高到低,如果有long就会先为long,有double就回显为double
        System.out.println(b+c+d);  
        System.out.println(c+d);
    }
}

 ++ --

package operater;

public class Demo03 {
    public static void main(String[] args) {
        // ++   --
        int a = 3;
        System.out.println(a);
        int b = a++;
        // a++相当于在代码后面+1
        // a= a+1
        // -------------
        // ++a相当于在代码前面+1
        // a= a+1
        int c = ++a;
        System.out.println(a); //5
        System.out.println(b); //3
        System.out.println(c); //5
    }
}
package operater;

public class Demo04 {
    public static void main(String[] args) {
        // ++   --
        int a = 3;
        System.out.println(a);
        int b = a--;
        // a--相当于在代码后面-1
        // a= a-1
        // -------------
        // --a相当于在代码前面-1
        // a= a-1
        int c = --a;
        System.out.println(a); //1
        System.out.println(b); //3
        System.out.println(c); //1
    }
}

5、幂函数

package operater;

public class Demo04 {
    public static void main(String[] args) {
        // ++   --
        int a = 3;
        System.out.println(a);
        int b = a--;
        // a--相当于在代码后面-1
        // a= a-1
        // -------------
        // --a相当于在代码前面-1
        // a= a-1
        int c = --a;
        System.out.println(a); //1
        System.out.println(b); //3
        System.out.println(c); //1
        // 借用Math类中的pow方法
        double pow = Math.pow(2, 3);
        System.out.println(pow);
    }
}

6、逻辑运算

package operater;


// 逻辑运算
public class Demo05 {
    public static void main(String[] args) {

        Boolean a = true;
        Boolean b = false;

        System.out.println("a&&b--"+(a&&b)); // 与 and
        System.out.println("a||b--"+(a||b)); // 或 or
        System.out.println("!a&&b--"+!(a&&b)); // 非 取反
    }
}
package operater;

public class Demo06 {
    public static void main(String[] args) {
        /**
         * A = 0010 0010
         * B = 1101 0010
         * -----------------------
         * A&B = 0000 0010 都为1才为1
         * A|B = 1111 0010 只要有一个为1,就为1
         * A ^ B = 1111 0000 相同为0,不同为1
         * ~B = 0010 1101 取反
         */
        // 二进制转移  结果为16
        System.out.println(2<<3); // 0000 0010
                                  // 0001 0000
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值