如何快速记住这八种基本数据类型?
四个一组 非常顺口 读十遍 很快就能记住 面试直接答出 >.<
byte short int long | float 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一trypublic 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进行使用
>.< 谢谢读取~