day07语法初步,变量及变量作用域

在这里插入图片描述

数据转换类型

public class demo04 {
    public static void main(String[] args) {
        int q =130;


        byte w = (byte)q;
        System.out.println(q);
        System.out.println(w);//内存溢出
        /*强制转换  (类型)变量名  高到低
          自动转换   低到高 */
        System.out.println("==========================");
        int e =123;
        double r =e;
        System.out.println(e);
        System.out.println(r);//自动转换为double后为128.0
        /*注意点:
        1.不能对布尔值进行转换
        2.不能转换成不相关的东西
        3.在把高容量转换成低容量的时候,强制转换
        4.转换时可能存在精度问题和内存溢出问题
       */
        System.out.println("==========================");
        char i = 'a' ;
        float u = i+1;
        System.out.println(i);
        System.out.println(u);
        System.out.println((char) u);

    }

}

变量

在这里插入图片描述

public class demo05 {
    public static void main(String[] args) {
        //操作比较大的数字时,注意溢出问题
        //数字可以用下划线隔开,jdk7.0特性
        int money =10_0000_0000;
        int years =20;
        int total1 =money*years;//输出-1474836480,因为还是int类型
        long total2 = money*years;//输出-1474836480,因为计算的时候就已经出现问题,默认类型int
        long total3= money*((long)years);//计算的时候就要转换类型,先把一个数转换为long

        System.out.println(total1);
        System.out.println(total2);
        System.out.println(total3);
        //大写 L 小写 l   小写的l容易看成1,一律用大写的L

    }
}
public class demo06 {
    public static void main(String[] args) {
        //int a,b,c;
        //int a=1,v=2,c=3;//程序可读性,不建议一行定义多个变量
        String number ="liuliuliu";
        char p = 'x';
        double d =545;
        //变量作用域



    }
}
public class demo07 {
    //类变量
    static double salary =2500;

    //属性:变量

    //实例变量;从属于对象;
    //布尔值 默认 false
    //除了基本变量,其余默认值都是null


    String name;
    int age;


    //main方法
    public static void main(String[] args) {
    //局部变量,必须声明和初始化

        int i =10;
    //变量类型 变量名字=new demo07
       demo07 Demo07= new demo07();
        System.out.println(Demo07.age);
        System.out.println(Demo07.name);
        System.out.println(salary);//demo07下才能在方法里使用类变量

    }

    //其他方法
    public void add(){
        //System.out.println(salary);这里没有引入demo07,用不了

    }
}

常量

public class demo08 {
//常量
    //修饰符,不存在先后顺序
    final    static    double PI =3.14;

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

    }
}

变量声明标准

常用的类名要首字母大写和驼峰原则,常量大写加下划线,类成员首字母小写加驼峰原则,局部变量和方法名都和类成员同

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值