Java提供了各种数据类型,就像任何其他动态语言一样,比如boolean、char、int、unsigned int、signed int、float、double、long等,总共提供了7种类型,其中每个数据类型在存储在内存中时获得不同的空间。将一种数据类型的值指定给另一种数据类型时,这两种类型可能不兼容。如果数据类型兼容,Java将自动执行转换,称为自动类型转换,如果不兼容,则需要显式转换。例如,将int值赋给长变量。
资料型态 | 内存中获取的位 |
---|---|
布尔值 | 1. |
字节 | 8(1字节) |
烧焦 | 16(2字节) |
短的 | 16(2字节) |
智力 | 32(4字节) |
长的 | 64(8字节) |
浮动 | 32(4字节) |
双重的 | 64(8字节) |
加宽或自动类型转换
自动转换两种数据类型时,会发生加宽转换。这种情况发生在:
- 这两种数据类型是兼容的。
- 当我们将较小数据类型的值分配给较大的数据类型时。
例如,在java中,数字数据类型彼此兼容,但不支持从数字类型到字符或布尔类型的自动转换。此外,char和boolean不兼容。
例子:
JAVA
// Java Program to Illustrate Automatic Type Conversion // Main class class GFG { // Main driver method public static void main(String[] args) { int i = 100 ; // Automatic type conversion // Integer to long type long l = i; // Automatic type conversion // long to float type float f = l; // Print and display commands System.out.println( "Int value " + i); System.out.println( "Long value " + l); System.out.println( "Float value " + f); } } |
Int value 100Long value 100Float value 100.0
缩小或显式转换
如果我们想将较大数据类型的值分配给较小的数据类型,我们将执行显式类型转换或缩小。
- 这对于无法进行自动转换的不兼容数据类型很有用。
- 这里,目标类型指定要将指定值转换为的所需类型。
字符和数字不兼容。让我们看看什么时候我们试着把一个转换成另一个。
JAVA
// Java program to illustrate Incompatible data Type // for Explicit Type Conversion // Main class public class GFG { // Main driver method public static void main(String[] argv) { // Declaring character variable char ch = 'c' ; // Declaringinteger variable int num = 88 ; // Trying to insert integer to character ch = num; } } |
输出: 将生成一个错误
当整数变量需要4个字节,而字符数据类型需要2个字节时,会生成此错误。我们正在尝试将数据从4字节打印到2字节,这是不可能的。
如何进行显式转换?
JAVA
// Java program to Illustrate Explicit Type Conversion // Main class public class GFG { // Main driver method public static void main(String[] args) { // Double datatype double d = 100.04 ; // Explicit type casting by forcefully getting // data from long datatype to integer type long l = ( long )d; // Explicit type casting int i = ( int )l; // Print statements System.out.println( "Double value " + d); // While printing we will see that // fractional part lost System.out.println( "Long value " + l); // While printing we will see that // fractional part lost System.out.println( "Int value " + i); } } |
Double value 100.04Long value 100Int value 100
注: 为字节类型赋值时,小数部分丢失,并减少到模256(字节范围)。
例子:
JAVA
// Java Program to Illustrate Conversion of // Integer and Double to Byte // Main class class GFG { // Main driver method public static void main(String args[]) { // Declaring byte variable byte b; // Declaring and initializing integer and double int i = 257 ; double d = 323.142 ; // Display message System.out.println( "Conversion of int to byte." ); // i % 256 b = ( byte )i; // Print commands System.out.println( "i = " + i + " b = " + b); System.out.println( "Conversion of double to byte." ); // d % 256 b = ( byte )d; // Print commands System.out.println( "d = " + d + " b= " + b); } } |
Conversion of int to byte.i = 257 b = 1Conversion of double to byte.d = 323.142 b= 67
表达式中的类型提升
计算表达式时,中间值可能超过操作数的范围,因此表达式值将被提升。类型提升的一些条件是:
- 在计算表达式时,Java会自动将每个字节、短操作数或字符操作数升级为int。
- 如果一个操作数为long、float或double,则整个表达式将分别升级为long、float或double。
例子:
JAVA
// Java program to Illustrate Type promotion in Expressions // Main class class GFG { // Main driver method public static void main(String args[]) { // Declaring and initializing primitive types byte b = 42 ; char c = 'a' ; short s = 1024 ; int i = 50000 ; float f = 5 .67f; double d = . 1234 ; // The Expression double result = (f * b) + (i / c) - (d * s); // Printing the result obtained after // all the promotions are done System.out.println( "result = " + result); } } |
result = 626.7784146484375
表达式中的显式类型转换
计算表达式时,结果会自动更新为更大的操作数数据类型。但是,如果我们将结果存储在任何较小的数据类型中,它会生成编译时错误,因此我们需要对结果进行类型转换。
例子:
JAVA
// Java program to Illustrate Type Casting // in Integer to Byte // Main class class GFG { // Main driver method public static void main(String args[]) { // Declaring byte array byte b = 50 ; // Type casting int to byte b = ( byte )(b * 2 ); // Display value in byte System.out.println(b); } } |
100
注: 在单操作数的情况下,结果转换为int,然后进行相应的类型转换,如上例所示。
本文由 阿波娃 辛格。如果你喜欢Geeksforgek,并且想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。