数据类型的转换

从小到大依次为:byte--short--int(char为0~65535的int)--long--float--double;从小到大自动转,从大到小强制转

- 强转可能精度丢失或溢出。

- 整型直接量可以直接赋值给byte、short、char但不能超范围;

- byte、short、char类型变量参与运算时,先一律转换位Int再运算;

  ```java

  //数据类型演示

  public class DataTypeDemo {

   public static void main(String[] args) {

          

          /*

   //1.int:整型,4个字节,-21个多亿到21个多亿

   int a = 250; //250为整数直接量,默认int型

   //int b = 10000000000; //编译错误,100亿默认为int型,但超范围了

  

   System.out.println(5/2); //2,5和2为int型,结果也是int,小数位无条件舍弃了

   System.out.println(2/5); //0

  

   int c=2147483647;

   c=c+1;

   System.out.println(c); //-2147483648,溢出了,是需要避免的

   */

          

          /*

   //2.long:长整型,8个字节

   long a = 250L; //250L为长整型直接量

   //long b = 10000000000; //编译错误,100亿为int型,但超范围了

   long c = 10000000000L;

  

   //若有可能超int范围,则在第一个数字后加L

   long d = 1000000000*2*10L;

   System.out.println(d); //200亿

   long e = 1000000000*3*10L;

   System.out.println(e); //溢出了

   long f = 1000000000L*3*10;

   System.out.println(f); //300亿

  

   //获取自1970.1.1零时到此时此刻的毫秒数

   long time = System.currentTimeMillis();

   System.out.println(time);

   */

          

          /*

   //3.double:浮点型,8个字节

   double a = 5.678; //5.678为浮点型直接量,默认为double型

  

   double b=6.0;

   double c=4.9;

   System.out.println(b-c); //double缺陷-舍入误差

   */

  

          

          /*

   //4.boolean:布尔型,1个字节,只允许取值为true和false

   boolean b1 = true; //true和false为布尔型直接量

   boolean b2 = false;

   //boolean b3 = 250; //编译错误,数据类型不匹配

   */

          

          

          /*

   //5.char:字符型,2个字节

   //  1)char型直接量需放在一对单引号中

   //  2)有且仅有一个

   char c1='中';

   char c2='a';

   char c3='3';

   char c4=' ';

   //char c5=''; //编译错误,必须有字符

   //char c6='中国'; //编译错误,只能有一个字符

  

   char c7=97; //97为'a'的码

   System.out.println(c7); //a

  

   System.out.println(2+2); //4

   System.out.println('2'+'2'); //100,'2'的码50加上'2'的码50,结果为100

  

   char c8='\\';

   System.out.println(c8); //\

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值