4 errors in an example (java)
class Demo {
public static void main(String args[]){
byte x;
int a=270;
double b =128.128;
System.out.println("int converted to byte");
x=(byte) a;
System.out.println("a and x "+ a +" "+x);
System.out.println("double converted to int");
a=(int) b;
System.out.println("b and a "+ b +" "+a);
System.out.println("\n double converted to byte");
x = b;
System.out.println("b and x "+b +" "+x);
}
}
This shows 4 errors of which I solved 3, the last one tells me it is of incompatible types for double A byte... as I have just started and the examples are in English, I understand little, if someone could tell me how to fix that error I would appreciate it.
2 answers
The main error is in the indicated line, they are incompatible types, you can not perform a conversion of double to byte directly, you have to perform a casting :
byte x;
int a=270;
double b =128.128;
System.out.println("int converted to byte");
x=(byte) a;
System.out.println("a and x "+ a +" "+x);
System.out.println("double converted to int");
a=(int) b;
System.out.println("b and a "+ b +" "+a);
System.out.println("\n double converted to byte");
// x= b; //Error tipos incompatibles!.
x= (byte)b;
System.out.println("b and x "+b +" "+x);
You have to perform in casting to type byte.
To have a correct output:
int converted to byte
a and x 270 14
double converted to int
b and a 128.128 128
double converted to byte
b and x 128.128 -128
Regarding Casting I add this information from ADRformacion
Casting or transformations of type
Casting is a procedure to transform a primitive variable from one type to another, or transform an object from one class to another class as long as there is an inheritance relationship between the two (the latter casting is the most important and will be seen later).
Within the casting of primitive variables there are two classes:
Implicit: no code needs to be written for it to take place. It occurs when a widening casting is performed, i.e. when a value is placed small in a large container. Example 1:
Example 2: similar to the previous one.
Instead,
Explicit: yes you need to write code. It occurs when a narrow casting is performed, i.e. when a large value is placed in a small container. They are susceptible to data loss. Example 1:
Note: Yes replace the first line int num1=100 with int num1=1000000, the code would compile fine, but there would be data loss, well the 1000000 gets out of the short range [-32768, 32767]. When displaying by console the value would get an incongruous result.
Example 2:
Example 3: continuation of Example 2 of implicit casting
So that the line
Compile a casting must be done explicit to long
But not
Because, in the previous line, 10000000000 is considered int, while in the ones above, double.
Your variable x
is of type Byte
and b
is Double
so you won't be able to assign why ? int
of 32 bits . and byte
8 bits , convert it (cast it) to Byte
x= (byte) b;
Casting: is a procedure for transforming a primitive variable from one type to another, or transform an object from one class to another class as long as there is an inheritance relationship between the two