|
![]() |
Whizlabs Support (info@whizlabs.com or +91-9971838640 )
OCAJP 8 | OCPJP 8 | OCEJWCD | OCMJEA
Krishna Srinivasan wrote:
From a byte to a short, an int, a long, a float, or a double
From a short to an int, a long, a float, or a double
From a char to an int, a long, a float, or a double
From an int to a long, a float, or a double
From a long to a float or a double
From a float to a double
S B Patel wrote:
I have checked the code you provided with each type... It will automatically converted to int, long, float or double but not converted to byte or short. Obviously you cant convert any numeric datatype to Boolean.
Dipan Chokshi wrote:So for the exam we need to check all combination or there is better way to remember them?
S B Patel wrote:int i = b1 + b2 // This will compile
S B Patel wrote:byte b3 = b1 + b3 // Not compile
S B Patel wrote:It will automatically converted to int, long, float or double but not converted to byte or short. Obviously you cant convert any numeric datatype to Boolean.
Roel De Nijs wrote:
When executing calculations Java uses a "standard" datatype. When you are calculating with integers, the result of the calculation will be an int. If you are doing some calculation with floating-points (decimals), the result will be a double. More detailed information can be found in this thread.
Some numeric data types will be implicitly converted to another numeric data type (depending on the range of the numeric data type). And if it can't be implicitly converted, you can always add an explicit cast. Because char is unsigned and all other numeric data types are signed, you always need an explicit cast to convert any numeric data type to char. In this thread you'll find tons of code examples about working with byte (and other numeric) variables. And this thread explains why a long (64-bit) fits into a float (32-bit).
Hope it helps!
Kind regards,
Roel
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|