数据类型
一、基本数据类型
数据类型 | 关键字 | 取值范围 | 内存占用 |
---|---|---|---|
整数 | byte | -128~127 | 1 |
整数 | short | -32768~32767 | 2 |
整数 | int | -2147483648 ~ 2147483647(10位数) | 4 |
整数 | long | -9223372036854775808 ~ 9223372036854775807(19位数) | 8 |
浮点数 | float | 1.401298e-45 到 3.402823e+38 | 4 |
浮点数 | double | 4.9000000e-324 到 1.797693e+308 | 8 |
字符 | char | 0~65535 | 2 |
布尔 | boolean | true,false | 1 |
使用思路:
1、整数类型:首选int,如果发现int装不下了,将类型换成long,定义long类型变量需要在数字尾加入L标识。
2、小数类型:首选double,非要float类型时,需要在数字尾部加入F标识
下面的代码示例能让我们更好地了解基本数据类型
public class DataDemo1 {
public static void main(String[] args) {
byte bt=120;
int age =15;
short pound=154;
long yen = 1545154785415156L;
float penny =51.541515F;
double cash= 0.156151;
char gender = '男';
boolean flag = true;
System.out.println(name);
System.out.println(age);
System.out.println(pound);
System.out.println(yen);
System.out.println(penny);
System.out.println(cash);
System.out.println(gender);
System.out.println(flag);
}
}
控制台输出:
120
15
154
1545154785415156
51.541515
0.156151
男
true
二、引用类型
1、引用数据类型分类:类、接口、数组
同样的基本数据类型,所对应的引用数据类型有:
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer **** |
float | Float |
double | Double |
boolean | Boolean |
char | Character **** |
注意:在JAVA中引用类型存储在堆内存中,基本数据类型存储在栈内存中
2、包装类
(1)包装类:将基本数据类型,包装成类,变成引用数据类型
(2)手动装箱:调用方法,手动将基本数据类型,包装成类
1.public Integer(int value):通过构造方法(不推荐)
2.public static Integer valueOf(int i):通过静态方法
(3)手动拆箱:调用方法,手动将包装类,拆成基本数据类型
1.public int intValue():以int类型返回该Integer的值
(4)从JDK5开始,出现自动拆装箱:
1.自动装箱:可以将基本数据类型,直接赋值给包装类的变量
2.自动拆箱:可以将包装类的数据,直接赋值给基本数据类型的变量
3、Integer的常用方法
方法名 | 说明 |
---|---|
public static String toBinaryString(int i ) | 转化为二进制 |
public static String toOctalString(int i ) | 转化为八进制 |
public static String toHexString(int i ) | 转化为十六进制 |
public static int parseInt(String s ) | 将数字字符串,转化为数字 |
下面的代码示例,能让我们更好的了解Integer的常用方法
/**
* @author Tweek
*/
public class DataDemo2 {
public static void main(String[] args) {
int num = 200;
System.out.println(Integer.toBinaryString(num));
System.out.println(Integer.toOctalString(num));
System.out.println(Integer.toHexString(num));
String s ="518515";
System.out.println(Integer.parseInt(s));
}
}
控制台输出:
11001000
310
c8
518515
4、利用Integer中的parseInt方法完成以下要求
要求:已知字符串 String s=“192.168.10.1”;请将该字符串转换为数值并存入数组中,并求出最大值。
下面是代码示例
/**
* @author Tweek
*/
public class DataDemo3 {
public static void main(String[] args) {
String s= "192.168.10.1";
//字符串切割存入字符串数组
String[] sArr = s.split("\\.");
//创建数值数组
int[] arr = new int[sArr.length];
//遍历数组并将字符串数组中的字符串转化为数值并存入数值数组中去
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(sArr[i]);
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
控制台输出:
192
168
10
1
5、注意事项
(1)自动装箱原理:自动帮助我们调用了Integer.valueOf(127)
(2)包装类中,其获取数值的范围为-128~127,若超出此范围该包装类在获取数值时会新创建一个对象来进行引用。
例如两个Integer类的对象赋值127,其地址比较一样
两个Integer类的对象赋值128,其地址比较不一样
同样的两个Long类同上
下面的代码,能让我们更好地理解
/**
* @author Tweek
*/
/**
* @author Tweek
*/
public class DataDemo4 {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 =128;
Integer i4 =128;
System.out.println(i3 == i4);
Long i5 = 127L;
Long i6 = 127L;
System.out.println(i5 == i6);
Long i7 = 128L;
Long i8 = 128L;
System.out.println(i7 == i8);
}
}
控制台输出:
true
false
true
false
下面是源码方法分析
如果装箱的数据,不在-128~127之间,会重新创建新的对象
如果装箱的数据,在-128~127之间,不会创建新的对象,而是从底层的数组中,取出一个提前创建好的Integer对象,返回对应数值
*在Integer类中,底层存在一个长度为256个大小的数组,Integer[] cache
在数组中,存储了256个Integer对象,分别是-128~127
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}