Java之变量

  • Java是强类型语言 得符合规范 变量必须都必须先定义后才能使用

  • 类型

  • 基本类型(8类)

  • byte 1字节 2^7-1
    - short 2字节 2^15-1
    - int 4字节 2^31-1
    - long 8字节 2^63-1 (加L
    - float 4字节 (加F
    - double 8字节
    - char 2字节 字符
    - boolen 1位

  • 引用类型

    • 接口
    • 数组
    • 。。。

    3.进制表示

    /**
     * 进制转换
     * 二进制 0b
     * 八进制 0
     * 十六进制 0x
     */
    int dec = 10;
    int hex = 0x10;
    int oct = 010;
    int bin = 0b10;
    System.out.println(hex);
    System.out.println(dec);
    System.out.println(oct);
    System.out.println(bin);
    
  • 变量的作用域

    • 实例变量:属于对象 不自行初始化 会有默认的值

      public class Variable {
          //声明 不初始化
          String s;
          int num;
          float num1;
          boolean flag;
          double num3;
          char ch;
      
          public String toString() {
              return "variable{" +
                      "s='" + s + '\'' +
                      ", num=" + num +
                      ", num1=" + num1 +
                      ", flag=" + flag +
                      ", num3=" + num3 +
                      ", ch=" + ch +
                      '}';
          }
      
          public static void main(String[] args) {
      		//先new对象 再调用实例变量
              Variable vari =new variable();
              System.out.println(vari.toString());
              System.out.println(vari.num);
          }
      }
      

      输出结果

      variable{s='null', num=0, num1=0.0, flag=false, num3=0.0, ch= }
      
    • 局部变量:不许声明和初始化值

      public class Variable {
      	public static void main(String[] args) {
              int i = 10; //必须初始化
              System.out.println(i);
        }
      }
      
    • 类变量 static 定义

      public class Variable {
          
          static  int num=100;
          public static void main(String[] args) {
      		//不需要new 就直接使用
              System.out.println(num);
          }
      
      }
      
  • 类型转换

    优先级 byte<short<int<long<float<double

    • 高级到低级需要强转
    • 低级到高级不需要强转
    short n = 90;
    int k = 100;
    short m =(short) k;
    int j = n; 
    
  • 常量

    • 大写

    • final修饰

      public class variable {
      
          final double PI=3.14;
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值