zoukankan      html  css  js  c++  java
  • Java常量与变量及数据类型转换

    变量

      首字母小写

      需声明数据类型

    public class  Test{
    public static void main(String [] args){
    //int score=93;
    int score;
    score=93
    }
    }

    常量

      全大写

      只能赋值一次

      需要在变量的基础上 于头部 使用 final 关键字 声明

    public class  Test{
    public static void main(String [] args){
    //final int score=93;
    final int score;
    score=93
    }
    }

      

    数据类型转换

    自动转换  

      目的类型比原数据类型要大

      两种类型是相互兼容的

      byte->short

      short->int

      char->int

      int->long

      int->double

      float->double

      两个数中含有一个double类型 另一个也被转为double类型 结果为double类型

      否则。。。。。。

      double     >      float    >     long    >     int

      双精度   单精度   8字节   4字节

    强制类型转换

      浮点型->整型   保留整数部分

      长整型->短整型  仅保留短整型字节位数

    public class Test{
    public static void main(String[] args){
    double adouble=3.55555;
    int aint=(int)adouble;
    System.out.println(aint);
    //输出于同一行
    } }
    //输出结果:3
    public class Welcome {
    public static void main(String[] args) {
        final int SOCRE=93;
        int sum_socre;
        double adouble=3.55555;
        int aint=(int)adouble;
        int num=257;
        byte number=(byte)num;
        System.out.println('aint='+aint);
       //换行输出 System.out.println('number='+number); } }
    //输出结果:3
          1

      

    变量初始化

      类变量默认值为o false 0.0 0.00 " "

      实例变量无默认值

    public class Welcome {
        //类变量
        static boolean aboolean;
        static char achar;
        static byte abyte;
        static short ashort;
        static int aint;
        static long along;
        static float afloat;
        //实例变量
        double adouble;
    public static void main(String[] args) {
        
        System.out.println("aboolean="+aboolean);
        System.out.println("achar="+achar);
        System.out.println("abyte="+abyte);
        System.out.println("ashort="+ashort);
        System.out.println("aint="+aint);
        System.out.println("along="+along);
        System.out.println("afloat="+afloat);
        System.out.println("adouble="+adouble);
    
    }
    }
     

     

  • 相关阅读:
    单片机基础
    EM310_AT收到的短信分析
    [原]改动CImage以实现以指定的质量保存Jpeg图像
    [原创]巧用DOS命令改子目录中的文件名
    二个月零七天,我女儿会翻身了
    [原]用正则得到HTML中所有的图片路径
    新文章:把程序放在相册中
    [原]用三行代码实现对音量的控制,实现增大,减小,静音
    BIOS中隐藏Telnet后门
    CoolChm 注册机的编写
  • 原文地址:https://www.cnblogs.com/lttlpp61007188/p/10883732.html
Copyright © 2011-2022 走看看