【javaSE】java中的基本数据类型

如何快速记住这八种基本数据类型?

四个一组 非常顺口 读十遍 很快就能记住 面试直接答出  >.<

byte short int longfloat double char boolean

 应该还挺顺口


细节描述

byte

大小 1Byte          包装类 Byte                范围[-128,127] 【-pow(2,7), pow(2,7)-1】

绝知此事要躬行,还得自己try一try


short

大小 2Bytes        包装类 Short              范围[-32768,32767]

绝知此事要躬行,还得自己try一try


int

大小 4Bytes        包装类 Integer           范围[-2,147,483,648,2,147,483,647]

绝知此事要躬行,还得自己try一try


long

大小 8Bytes        包装类 Long     

范围[-9,223,372,036,854,775,808,9,223,372,036,854,775,807]

范围[-pow(2,63), pow(2,63)-1]

绝知此事要躬行,还得自己try一try   

好家伙,long了个寂寞 。。。   ?

不不不,这是因为long在定义的时候数字的末尾要加上 L, 如下:


float

大小 4Bytes        包装类 Float              范围[3.4e-45~1.4e38]

绝知此事要躬行,还得自己try一try    

/** 好家伙,可以写这么长 */
float f6 = 124584376345.435436345243625732452436456457f;
float f7 = 124.584376345435436345243625732452436456457f;
System.out.println(f6);
System.out.println(f7);

 (运行结果)


double

大小 8Bytes        包装类 Double           范围[4.9e-324~1.8e308]

绝知此事要躬行,还得自己try一try   

public static void main(String[] args) {
        double f1 = 4.9e-324;  // 4.9e-324~1.8e308
        //double f3 = 4.9e-325;  // 下溢
        double f4 = 1.4e308;  // 3.4e-45~1.4e308
        //double f5 = 1.4e309;  // 上溢
        /** 好家伙,可以写这么长 */
        double f6 = 124584376345.435436345243625732452436456457f;
        double f7 = 124.584376345435436345243625732452436456457f;
        System.out.println(f6);
        System.out.println(f7);
}

 (运行结果)


char

大小 2Bytes        包装类 Character      范围[0~65535]

绝知此事要躬行,还得自己try一try


boolean

大小 2Bytes        包装类 Boolean         范围[true|false]

绝知此事要躬行,还得自己try一try


基本数据类型对应的包装类和字符串的互相转换

package jin.test;

/**
@author Liu Xianmeng
@createTime 2022/11/8 8:50
@instruction 测试
*/

@SuppressWarnings({"all"})
public class Student{

    public static void main(String[] args) {
        String str = "888";
        System.out.println(Short.parseShort(str) + 1000);       // 1888
        System.out.println(Integer.parseInt(str) + 2000);       // 2888
        System.out.println(Long.parseLong(str) + 3000);         // 3888
        System.out.println(Float.parseFloat(str) + 4000.0);     // 4888.0
        System.out.println(Double.parseDouble(str) + 5000.0);   // 5888.0
    }

}

【基本数据类型转换到String类型】

当需要把基本数据类型转换为字符串类型的时候

只需要把基本数据类型和一个空字符串连接起来 

例如,像这样 String str = 12 + "";    // 即可将int类型的12转换为字符串str进行使用

>.<  谢谢读取~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值