Dúvida com atribuição primitiva

Bom segundo as regrinhas Java básicas,uma conteiner só aceita o que couber nele;sem conversão explícita ocorre um erro de compilação…

byte 8
short 16
int 32
.
.
.
double 64

Mas se eu faço:

long l = 15l;

float f = l;

wtf???long 64 bits e float 32 bits…tudo bem que quando dou print no numero ele fica em hex,mas nao era pra dar erro?
ahh sim,se isso for novidade na 6.0 me avisem,vou fazer a scjp em breve e seria chato errar uma questão complexa de thread por causa de
uma atribuição boba!!!

Posta aí o exemplo que você testou, little pretty…

Uma das possibilidades é o cast forçado que a JVM faz quando se atribui um long a um float…

Não entendi direito, mas vale lembrar que float é para numeros de ponto flutuante e long para numero inteiros.

Hunt pinguins…odeio passaros!!O linux poderia ter adotado um mascote menos…tenho medo!!

long metal = 5;
float death = metal;

Este exemplo foi pra voce caveirinha =P

[quote=drsmachado]Posta aí o exemplo que você testou, little pretty…

Uma das possibilidades é o cast forçado que a JVM faz quando se atribui um long a um float…[/quote]

Hummm,maluquice isso acontecer…vai contra as regras básicas de atribuição!!
Wraper e conversões eh muito fucks e agora isso…minha litle cabecinha vai estourar
com tanta anuance!!

Bem, na verdade…
Trata-se da definição da linguagem e, não, não é apenas a partir da versão 6.

Segue o trecho que encontrei pesquisando na net…


na Java Language Specification, a sun:
5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:

* byte to short, int, long, float, or double
* short to int, long, float, or double
* char to int, long, float, or double
* int to long, float, or double
* long to float or double
* float to double

Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double in strictfp expressions also preserve the numeric value exactly; however, such conversions that are not strictfp may lose information about the overall magnitude of the converted value.

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

A widening conversion of a signed integer value to an integral type T simply sign-extends the two's-complement representation of the integer value to fill the wider format. A widening conversion of a character to an integral type T zero-extends the representation of the character value to fill the wider format.

Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a run-time exception (§11).

e

Quote:
5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (§5.1.1), a widening primitive conversion (§5.1.2), or a widening reference conversion (§5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

* The expression is a constant expression of type byte, short, char or int.

* The type of the variable is byte, short, or char.
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable

Ou seja, é assim por que alguém quis assim…