Java基础2:Java数据类型的划分及其注意事项

1. 基本数据类型:
(1). 数值型
- 整型:byte、short、int、long(默认值0)
- 浮点型:float、double (默认值0.0)
(2). 字符型: char  (默认值 '\u0000')
(3). 布尔型:boolean (默认值false)


2. 引用数据类型

数组、类、接口


3. 基本数据类型的取值范围

数据类型

关键字

字节数

取值范围

布尔型

boolean

1

true、false

字节型

byte

1

-27 ~ 27 -1

短整型

short

2

-215 ~ 215 -1

整型

int

4

-231 ~ 231 -1

长整型

long

8

-263 ~ 263 -1

字符型

char

2

0 ~ 216 -1

单精度浮点数

float

4

3.402823e+38 ~ 1.401298e-45

双精度浮点数

double

8

1.797693e+308 ~ 4.9000000e-324


4. 运算溢出现象 :

public static void main(String[] args){

    int max = Integer.MAX_VALUE;

    intmin = Integer.MIN_VALUE;

    System.out.println("MaxValue = "+ max);

    System.out.println("MinValue = "+ min);

    System.out.println("MaxValue + 1 = "+ ++max);

    System.out.println("MinValue -1 = "+ --min);

}

运行结果:

MaxValue = 2147483647

MinValue = -2147483648

MaxValue + 1 = -2147483648

MinValue -1 = 2147483647

 观察结果可以得出:

最小值减1等于最大值,最大值加1等于最小值。

原理见 计算机中整形和浮点型二进制保存的具体形式


 5. 类型转换与注意事项:

- 整型数字默认的是int型;浮点型数字默认类型是double类型

- 数据范围小的类型,可以自动转换为数据范围大的类型;数据范围大的类型,需要强制转换为范围小的类型。

(1). int类型转long类型(小转大)

public static void main(String[] args) {

   intmax = Integer.MAX_VALUE;

   int min = Integer.MIN_VALUE;

   long max1 = max + 1;

   System.out.println("max1 = "+ max1);

}

运行结果:

max1 =-2147483648

原理分析:

long max1 = max +1;先执行max + 1操作, 然后此时得到int值为 -2147483648 , 然后执行long max1 =  -2147483648。

正确写法:

long max1 = max +1L;

 

(2). int类型转byte类型 (大转小)

public static void main(String[] args) {

   bytebyte1 =11;

   intnum1 =10,num2 =2;

    byte sum = num1 +num2;

   System.out.println("sum= " + sum);

}

首先 : byte byte1 =11;这里的11是int类型的,但是可以直接赋值给byte,说明byte范围内的数字,是不需要强制转换的。但是超过byte范围的数字需要强制转换,强制转换会导致溢出问题。

byte sum = num1 +num2;这里会编译失败,因为int类型转换为byte类型需要强制转换:bytesum = (byte)(num1 + num2);

 

 

(3). 定义一个float变量

public static void main(String[] args) {

   floatnum =1.1;

   System.out.println("num = "+ num);

}

代码分析:

float num =1.1;这里会编译失败,定义float需要加上F或f,表示数字类型是float类型,需要修改为floatnum = 1.1F;

 

 

(4). 浮点数运算精度丢失 , 出现很长的小数问题

public static void main(String[] args) {

   System.out.println("result1 = "+ (3.0-2.6));

   System.out.println("result2 = "+ (0.06 + 0.01));

   System.out.println("result3 = "+ (1.0 - 0.42));

   System.out.println("result4 = "+ (4.015 * 100));

   System.out.println("result5 = "+ (303.1 / 1000));

}

运行结果:

result1= 0.3999999999999999

result2= 0.06999999999999999

result3= 0.5800000000000001

result4= 401.49999999999994

result5= 0.30310000000000004

解决方案: 使用BigDecimal来解决

public static void main(String[] args) {

   BigDecimal num1 = new BigDecimal("3.0");

   BigDecimal num2 =newBigDecimal("2.6");

   System.out.println("result1 = "+ (num1.subtract(num2)));

}

运行结果: result1 =0.4

 

(5). 整数除法计算

public static void main(String[] args) {

   intnum =10;

   double result = num / 4;

   System.out.println("result = "+ result);

}

 

运行结果:

result =2.0

原理分析:

double result = num /4;先执行num/4操作 , 运行结果为int型2 , 然后执行double result =2 。

正确写法:

double result = num /4.0;

 

(6). 特殊进制:

public static void main(String[] args){

    intnum =011;

    System.out.println("num = "+ num);

}

运行结果:

num = 9

原理分析:

因为以0开头的数字表示是8进制 , 以0x开头的数字表示是16进制

 

6. char类型

char在java中是16位的,因为java用的是unicode,而且8位的ASCII码包含在unicode中,是从0~127。所以java可以保存中文等unicode编码字符。

public static void main(String[] args) {

   char c ='';

   int index = c;

   System.out.println("index = "+ index);

}

运行结果:  index = 20013

 

7. boolean类型

在java中,boolean就是true和false,不能用数字或者其他类型代替。

 

猜你喜欢

转载自blog.csdn.net/gethinyang/article/details/79193139