jai
jai

Reputation: 21897

not getting a compilation error when a byte/short/char is stored in int

float f = 9.0; //compilation error.

char c = 12; //not a compiation err
int i = 99;//not a compiation err
short s = i;//not a compiation err

However I'm getting an error when the value I assigned is more than the char/int/short range Eclipse is showing compilation error.

Is it the feature of Eclipse or JDK 1.6 NOT to give the compilation error when storing int into short, byte or char?

I was expecting a compilation error when Storing a large number in a small number. For Floating point data types this was true. but not with whole number data types.

Upvotes: 1

Views: 206

Answers (2)

Carl Smotricz
Carl Smotricz

Reputation: 67790

I just ran this sucker on ideone. Here's the url:

http://ideone.com/WZ4kX

That compiler is claimed to be: language: Java (sun-jdk-1.6.0.17)

and the resulting messages are:

Main.java:3: possible loss of precision
found   : double
required: float
float f = 9.0; //compilation error.
          ^
Main.java:7: possible loss of precision
found   : int
required: short
short s = i;//not a compiation e
          ^
2 errors

For what it's worth, my Eclipse (Helios) is giving me a type mismatch error on this. I haven't changed any of the compiler error message settings in Eclipse.

Upvotes: 1

Brent Wilson
Brent Wilson

Reputation: 213

Just add an "f" after the 9.0. For example:

Float f = 9.0f;

Upvotes: 1

Related Questions