java3:类型转换(type casting)

来源:互联网 发布:进程调度算法c语言 编辑:程序博客网 时间:2024/05/22 02:21

1 数值类型转换

Can you perform binary operations with two operands of different types? Yes. If an integer
and a floating-point number are involved in a binary operation, Java automatically converts
the integer to a floating-point value. So, 3 * 4.5 is same as 3.0 * 4.5.
You can always assign a value to a numeric variable whose type supports a larger range of
values; thus, for instance, you can assign a long value to a float variable. You cannot, how-
ever, assign a value to a variable of a type with smaller range unless you use  type casting.
Casting is an operation that converts a value of one data type into a value of another data type.
Casting a variable of a type with a small range to a variable of a type with a larger range is
known as widening a type. Casting a variable of a type with a large range to a variable of a
type with a smaller range is known as narrowing a type. Widening a type can be performed
automatically without explicit casting. Narrowing a type must be performed explicitly. 
当我们将一个int 型变量与一个float/double型变量进行运算时候,编译器会自动将int类型转为float/double类型。类型转换也就是将一种数据类型的值便问另一种数据类型的操作,如果从小范围类型变量转为大范围类型变量,叫类型拓宽,如果从大范围类型变量转为小范围类型变量,叫类型缩窄。拓宽类型不需要我们显示去转换,可以自动执行这个转换,但是缩窄类型必须我们程序显式的做这个转换。例如:

System.out.println((int)1.7)返回1;

System.out.println((double)1/2) 返回0.5 ;注意,在做除法时候我们一定要注意这一点,不然很容易让我们的结果变为了0.

但是注意,类型转换是一种临时的,类型转换不会改变变量本身的类型(Casting can not change the variable being cast)

例如下面 d 在类型转换后不会改变;

double d=4.5;

int i=(int)d;

2   char 与 数据类型之间的类型转换

2.1 整型转为char型——只有低16位被赋值给char, 其余部分忽略

A char can be cast into any numeric type, and vice versa. When an integer is cast into a
char, only its lower 16 bits of data are used; the other part is ignored. 

上面我们讲的是数值类型的值的转换,而 char 也可以转为任何一种numeric type,反之也是如此,每种数值型类型也可以转为char型。当我们将一个整形转换为char型时候,只有该整形的低16位数据,其余部分会被丢弃,例如:

char ch=(char) 0xAB0041 ;//只有0x0041会被赋值给ch;

System.out.println(ch);//打印出了 字符 A(ASCII码65即 0x41)

2.2 float转为char——float 先转为int,int再转为char

When a floating-point value is cast into a char, the floating-point value is first cast into an
int, which is then cast into a char.
char ch = (char)65.25; // double 65.25先转为int型65,int型65再转为char(取低16位),所以依旧还是65
System.out.println(ch); // ch is character A

2.3  char型转为数值型类型——字符的Unicode直接转为指定的数值型类型

When a char is cast into a numeric type, the character’s Unicode is cast into the specified
numeric type.
int i = (int)'A'; // the Unicode of character A is assigned to i
System.out.println(i); // i is 65

如果转换结果适用于目标变量,就可以使用隐式转换方式(implicit casting) 否则必须使用显式转换方式(explicit casting),例如下面,因为Unicode ‘a’是97,它在byte型的范围内,所以下面的隐式转换是通过的

byte b='a';

int i='a';

但是下面的转换时不允许的,因为Unicode \uFFF4 不适用于一个byte型变量的范围,所以必须使用显式转换

byte b='\uFFF4' ;//不允许

byte b=(byte)"\uFFF4";//必须使用显式转换

注意:

所有数值操作符都可以使用在char型上,一个char型如果跟任何的数值型类型进行运算时候,那么这个char型会被自动转为为对应的数值型类型(就像一个int 跟一个float相加时候int会被自动转为float一样,小类型可以隐式的向大类型转换撒)

而如果参与操作的一个操作符是String的话,那么其他所有的numeric type或是char都被自动转为Sting 与原来的String 拼接(concatennate)这在System.out.print中我们经常很容易看到的。

例如: int i='2' +'3' //'2','3'会被自动做(int)'2' ,int ('3')的转换为50,51 所以结果是101

    int j=2+'a' //(int)'a'是97


0 0
原创粉丝点击