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);

}
}
 

 

 

 

转载于:https://www.cnblogs.com/lttlpp61007188/p/10883732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值