4 errors in an example (java)

2
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 that it is incompatible types for double to byte ... as I have just started and the examples are in English, I understand little, if someone could tell me how to fix that mistake I would appreciate it.

    
asked by cristobal 03.10.2016 в 22:05
source

2 answers

2

The main error is in the line indicated, they are incompatible types, you can not perform a conversion of double to byte directly, you have to make 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 by casting a byte type.

To have a correct exit:

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

With regard to the Casting I add this information of ADRformacion

Casting or type transformations

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 a relationship of inheritance between the two (this last casting is the most important one and it will be later).

Within the casting of primitive variables two classes are distinguished:

Implicit: you do not need to write code for it to take place. It occurs when a wide conversion is carried out (widening casting), that is, when a small value is placed in a large container. Example 1:

Example 2: similar to the previous one.

Instead,

Explicit: yes it is necessary to write code. It occurs when a narrow conversion (narrowing casting) is performed, that is, when a large value is placed in a small container. They are susceptible to data loss. Example 1:

  

NOTE: if the first line int num1 = 100 was replaced by int   num1 = 1000000, the code would compile well, but there would be data loss,   because the 1000000 is outside the range of short [-32768, 32767]. When showing   by console the value would get an incongruous result.

Example 2:

Example 3: continuation of Example 2 of the implicit casting

So the line

compile an explicit casting must be made to long

but not

because, in the previous line, 10000000000 is considered int, while in the above, double.

    
answered by 03.10.2016 в 22:14
1

Your variable x is of type Byte and b is Double why you can not assign Why? int of 32 bits . and byte 8 bits , convert it (Do the Casting) to Byte

x= (byte) b;
  

Casting: It 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 both

    
answered by 03.10.2016 в 22:07