For those who have just started learning programming including the Java language, and those who have already learned it, for review I am writing to learn type conversion.
Last time, I learned about Variables and Types. This time about ** type conversion **.
[Introduction to Java Table of Contents] -Variables and types ・ Type conversion ← Now here -Variable Scope -String operation -Array operation ・ Operator ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation) -Exception handling ・ About lambda expression ・ About Stream API
It is to convert the data type. Here's how to do it.
-Implicit type conversion ・ Type conversion by casting
Let's look at each one.
It is possible to substitute a small type for a large type. Even if we programmers do not explicitly convert the type, it will convert the type at compile time.
Substitution from the left type to the right type is OK! byte → short、char → int → long → float → double
Main.java
class Main {
public static void main(String args[]) {
int number = 10;
//int type number becomes float type
float numberFloat = number;
System.out.println(numberFloat); // 10.0 ← with a decimal point
}
}
** Please note that implicit type conversion is not possible for the following. ** ** ・ When changing char type to short type ・ When changing short type to char type Both are 16-bit data types, but the range of values that can be handled is different. You must perform type conversion by casting as described below.
Main.java
class Main {
public static void main(String args[]) {
//・ Char → short
char a = 100;
//I get an error
// short b = a; // Type mismatch: cannot convert from char to short
short b = (short) a; //Converting to short type by type conversion by cast
System.out.println(b); // 100
//・ Short → char
short c = 100;
//I get an error
// char d = c; // Type mismatch: cannot convert from short to char
char d = (char) c; //Converting to char type by type conversion by cast
System.out.println(d); // d
}
}
-It is impossible to substitute a large type for a small type. A compile error occurs. Substitution from the right type to the left type is NG! byte → short、char → int → long → float → double
Main.java
class Main {
public static void main(String args[]) {
int numberInt = 10;
//You cannot assign from a large type to a small type.
byte numberByte = numberInt; // Type mismatch: cannot convert from int to byte
System.out.println(numberByte);
}
}
Also, please note that there is a risk that the accuracy will drop due to the following type conversions **.
Main.java
class Main {
public static void main(String args[]) {
//Define a variable with the maximum value of each type
int intMax = Integer.MAX_VALUE;
long longMax = Long.MAX_VALUE;
float floatMax = Float.MAX_VALUE;
//The number of effective digits of the float type is 7 decimal digits.(24 digits in binary)
// int → float
float floatFromIntMax = intMax;
System.out.println(intMax); // 2147483647
//↓ Values after 7 effective digits are lost (results different from those before type conversion are output)
System.out.println(floatFromIntMax); // 2.14748365E9
// long → float
float floatFromLongMax = longMax;
System.out.println(longMax); // 9223372036854775807
//↓ Values after 7 effective digits are lost (results different from those before type conversion are output)
System.out.println(floatFromLongMax); // 9.223372E18
//The number of effective digits in double type is approximately 15 decimal digits.(53 digits in binary)
// long → double
double doubleFromLongMax = longMax;
System.out.println(longMax); // 9223372036854775807
//↓ Values after 15 effective digits are lost (results different from those before type conversion are output)
System.out.println(doubleFromLongMax); // 9.223372036854776E18
// float → double
double doubleFromFloatMax = floatMax;
System.out.println(floatMax); // 3.4028235E38
//↓ Values after 15 effective digits are lost (results different from those before type conversion are output)
System.out.println(doubleFromFloatMax); // 3.4028234663852886E38
}
}
You cannot assign from a large type to a small type.
Explicitly perform type conversion using ** () ** cast operator
.
Main.java
class Main {
public static void main(String args[]) {
double numberDouble = 3.14;
//You cannot assign from a large type to a small type.
// int numberInt = numberDobule; // Type mismatch: cannot convert from double to int
//Forcibly converted to int type using cast operator
int numberInt = (int)number;
System.out.println(numberInt); //Is output as 3
}
}
Please note that some patterns cannot be converted. Strings cannot be converted to int types using the cast operator.
Main.java
class Main {
public static void main(String args[]) {
//Examples that cannot be converted
String str = "Good morning";
int num = (int)str; // Cannot cast from String to int
System.out.println(str);
}
}
** Notes on casting **
Please note that some data will be lost due to type conversion by casting.
-When the type is converted from a large size to a small size, the value of the high-order bit is truncated (data overflow).
Main.java
class Main {
public static void main(String args[]) {
short x = 257; //16 bit-> 0000 0001 0000 0001
byte y = (byte)x; //8 bits-> 0000 0001
System.out.println(y); //Is output as 1
}
}
-When converting from a decimal to an integer, the decimal point is truncated.
Main.java
class Main {
public static void main(String args[]) {
//Also, when converting from a decimal to an integer, the decimal point is truncated.
double d = 10.23;
int i = (int)d;
System.out.println(i); //Is output as 10
float f = 33.44f;
int i2 = (int)f;
System.out.println(i2); //33 is output
}
}
Java may automatically perform type conversions not intended by the programmer. This is explained below.
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
long b = 2;
//Variable a is converted to long type before operation
int c = a + b; // Type mismatch: cannot convert from long to int
}
}
↓ ** Solution **
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
long b = 2;
int c = (int)(a + b); //The error disappears by casting the operation result to int type.
System.out.println(c); // 3
}
}
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
float b = 2.0f;
//Variable a is converted to float type before operation
int c = a + b; // Type mismatch: cannot convert from float to int
}
}
↓ ** Solution **
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
float b = 2.0f;
int c = (int)(a + b); //The error disappears by casting the operation result to int type.
System.out.println(c); // 3
}
}
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
double b = 2.0;
//Variable a is converted to double type before operation
int c = a + b; // Type mismatch: cannot convert from double to int
}
}
↓ ** Solution **
Main.java
class Main {
public static void main(String args[]) {
int a = 1;
double b = 2.0;
int c = (int)(a + b); //The error disappears by casting the operation result to int type.
System.out.println(c); // 3
}
}
Main.java
class Main {
public static void main(String args[]) {
byte a = 1;
byte b = 2;
//Variable a before operation,b is converted to int type
byte c = a + b; // Type mismatch: cannot convert from int to byte
}
}
↓ ** Solution **
Main.java
class Main {
public static void main(String args[]) {
byte a = 1;
byte b = 2;
byte c = (byte)(a + b); //The error disappears by casting the operation result to byte type.
System.out.println(c); // 3
}
}
I think that promotion (promotion) may lead to unintended results and errors. You have to be careful about the numerical promotion at the time of calculation.
I learned about type conversion. Next time, I will delve into Variable Scope.
Recommended Posts