This article is --Start learning Java ――I want to review the basics
For those who say.
The types and variables are described in the following articles. [Basic knowledge of Java] About types and variables
Here's how to convert a variable of one basic type to a variable of another basic type.
Basic type of conversion source | Basic type of change destination that can be type converted |
---|---|
char | int, long, float, double |
byte | short, int, long, float, double |
short | int, long, float, double |
int | long, float, double |
long | float, double |
float | double |
double | None |
The integer data type is
byte < short < int < long
It is a feeling of size.
When converting from a small type to a large type, there are ** when the type is automatically converted ** and ** a cast where the programmer intentionally converts.
When the type is converted automatically
byte a = 127;
short b = a;
System.out.println(b);//Output result = 127
short c = 32767;
long d = c;
System.out.println(d);//Output result = 32767
A cast where the programmer consciously transforms
byte a = 127;
byte b = 127;
System.out.println((long)a + b);//Output result = 254
int c = 2147483647;
int d = 2147483647;
System.out.println((long)c + d);//Output result = 4294967294
Be careful when converting from large to small types.
I get an error if I do the following.
long a = Long.MAX_VALUE;//Substituting the maximum value of long
System.out.println(a);//Output result = 9223372036854775807
short b = a;//Type mismatch: cannot convert from long to short
There is a way to fix the error, but the contents of the variable will change. If the size exceeds the size that the data type can have, data will be truncated (data overflow), and the expected execution result will not be obtained.
long a = Long.MAX_VALUE;//Substituting the maximum value of long
System.out.println(a);//Output result = 9223372036854775807
short b = (short) a;//Type conversion by cast
System.out.println(b);//Output result =-1
This time, I will only explain the type conversion of class types. When the reference type of the conversion source is a class type, the reference type of the conversion destination and the rules of type conversion are as shown in the figure below.
Reference type of conversion destination | Type conversion rules |
---|---|
Class type | 変換先のClass typeが変換元のClass typeのスーパークラスであること。 |
Interface type | 変換先のInterface typeのインタフェースを変換元のクラス型のクラスが実装していること。 |
Array type | Type conversion is not possible. |
** Reference type implicit type conversion ** Parent class type variable = new child class ();
//Parent class
class Oya {
void greet() {
String a = "Good morning";
System.out.println(a);
}
}
//Child class that inherits the parent class
class Kodomo extends Oya {
void name() {
String b = "Iniesta";
System.out.println(b);
}
public static void main(String[] args) {
Oya oy = new Kodomo();
oy.greet();//Output result = good morning
Oya oy2 = new Oya();
oy2.greet();//Output result = good morning
Kodomo ko = new Kodomo();
ko.greet();//Output result = good morning
ko.name();//Output result = Iniesta
Kodomo ko2 = new Oya();//Error: Type mismatch: cannot convert from Oya to Kodomo
}
}
As you can see in the error Child class type variable = new parent class (); You can not.
It is possible to convert from all types to character types.
Integer a1 = 123;
String s1 = "Ah" + a1;
System.out.println(s1);//Output result = A 123
System.out.println(s1 instanceof String);//Output result = true
int a2 = -123;
String s2 = String.valueOf(a2);
System.out.println(s2);//Output result =-123
System.out.println(s2 instanceof String);//Output result = true
I still don't have a deep understanding of reference type conversion, so I will add it as soon as I understand it.
Then thank you ☕️
Recommended Posts