Java - Short - Short?

 
Vista:

Short - Short?

Publicado por hugo (1 intervención) el 23/03/2002 23:25:20
short x
short y
short z
Cuando resto 2 variables de tipo Short, el resultado me la de tipo ENTERO, aun cuando la variable de resultado este definida como short.

PORQUE?
z= x-y;
^ Igua a entero

Sera porque las variables de tipo short son de 16 bits a complemento a 2 y cuando se restan se les agrega un bit de signo?

Espero sus respuestas, Gracias!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Short - Short?

Publicado por Jose A. (53 intervenciones) el 24/03/2002 08:57:51
La explicación la tienes en 'Java Language Specification'. Aquí puedes ver lo que dice:

5.6.2 Binary Numeric Promotion

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value of a numeric type, the following
rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

-If either operand is of type double, the other is converted to double.
-Otherwise, if either operand is of type float, the other is converted to float.
-Otherwise, if either operand is of type long, the other is converted to long.
-Otherwise, both operands are converted to type int.

Observa la última línea

¡Hasta otra!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar