.JAVA数据类型

.基本类型

Java支持数据类型分为两类: 基本数据类型和引用数据类型。

数据类型转换有两种:自动隐式和强制转换

基本数据类型共有8种,可以分为三类:

  • 数值型:整数类型(byte、short、int、long)和浮点类型(float、double)
  • 字符型:char
  • 尔型:boolean

8种基本数据类型的默认值、位数、取值范围,如下表所示: 

float和double的、最小值和最大值都是以科学记数法的形式输出的,结尾的“E+数字”表示E之前的数字要 乘以10的多少倍。比如3.14E3就是3.14×1000=3140,3.14E-3就是3.14/1000=0.00314。

/*
 * Copyright (c) 2020, 2023, MingZi.cn All rights reserved.
 *
 */
package org.abc;

/**
 * <p>Project: jse2303 - E1</p>
 * <p>Powered by MingZi On 2023-07-06 11:16:18</p>
 * <p>描述:<p>
 *
 * @author MingZi [MingZi@182.com]
 * @version 1.0
 * @since 17
 */
public class E1 {
    public static void main(String[] args) {
       byte b1 = 5;
       System.out.println(b1);
       short  s1 = 5;
       System.out.println(s1);
       int i1 = 5;
        System.out.println(i1);

        long n1 =5;
        System.out.println(n1);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);


        float f1 = .5f;
        double d2 = .5;
        System.out.println(f1);
        System.out.println(d2);


        char c1 ='a';
        int num ='a';
        char c2 ='中';
        System.out.println(c1);
        System.out.println(num);
        System.out.println(c2);
        System.out.println((int) c2);

        boolean f = true;
        System.out.println(f);
        System.out.println(!!!f);

        int aaa2 = (int)789456123066l;
        System.out.println(aaa2);

        double n =10;
    }
}

注意一下几点:

  • java八种基本数据类型的字节数:1字节(byte、boolean)、 2字节(short、char)、4字节(int、 float)、8字节(long、double)
  • 浮点数的默认类型为double(如果需要声明一个常量为float型,则必须要在末尾加上f或F)
  • 整数的默认类型为int(声明Long型在末尾加上l或者L)
  • 八种基本数据类型的包装类:除了char的是Character、int类型的是Integer,其他都是首字母大写 char类型是无符号的,不能为负,所以是0开始的 

.类型转换

自动类型转换隐式转换

  • 转换规则:从存储范围小的类型到存储范围大的类型。简言之,由低到高。
  • 具体规则为:byte→short(char)→int→long→float→double
  • 强制类型转换(float)3.5;
/*
 * Copyright (c) 2020, 2023, MingZi.cn All rights reserved.
 *
 */
package org.abc;

/**
 * <p>Project: jse2303 - E3</p>
 * <p>Powered by MingZi On 2023-07-06 21:01:32</p>
 * <p>描述:<p>
 *
 * @author MingZi [MingZi@182.com]
 * @version 1.0
 * @since 17
 */
public class E3 {
    public static void main(String[] args) {

         long i = 2; //自动隐式类型转换

         int y =(int)i;//强制类型转换

         //类型数据 f F float  d D double  l L long

         float f1 = .5f;
         double d1 = .5D;
         long  g1 = 1;

         short s = 6;
       /*  s = (short)(s + 1);
         System.out.println(s);*/
         s+=1;
         System.out.println(s);

    }
}

快捷键:

  1. 复制行 Ctrl + D
  2. 移动行 Alt + Shift + up (down)
  3. 单行注释    Ctrl + /   
  4. 多行注释 Ctrl + Shift + /
  5. 重构    Shift + F6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值