int x = 5; //x is a 32-bit interger int y = 40; //y is another 32-bit interger long z = x + y; //z is a 64-bit intergerImplicit type conversions take place automatically in the third line above as illustrated below:
long z = (long)x + (long)y; float x = 29.5; //x is a 32-bit IEEE floating point value float y = .00037; //y is a 32-bit IEEE floating point value double z = x + y; //z is a 64-bit IEEE floating point valueImplicit type conversions take place automatically in the third line above as illustrated below:
double z = (double)x + (double)y;
long x = 0x7effffffffffffffL; //x has 64-bit precision
float y = x; /* but y's mantissa has only
24-bit precision */
y can accommodate the range, but not the precision of x. Some of x's 64-bit precision therefore is lost when its value is assigned to y.
double x = 7.99; //48-bit mantissa + 16-bit exponent long y = (long)x; //cast to a 64-bit intergerAlthough it can accommodate greater precision, y holds only the truncated value of x, namely 7. That is why this conversion is not automatically allowed, but must be explicitly written into the code.
doublex = BigNum; /* double has a greater range
and precision than float */
float y = (float)x; /* so this conversion must be
forced explicitly */
Can result in a serious loss of both range and precision if you aren't careful.
long x = 0x0f0fffff; //a long whose lower 16 bits are all set char y = (char)x; //lower 16-bits only used so y = \uffff short c = (short)y; //same length but the value in c = -1 int z = (int)y; //top 16 bits filled with zeros z = 65535
Obgekt p = null; ExtendedObgekt q = p; //this assignment conversion is implicit ExtendedObgekt p = null; Obgekt q = (Obgekt)p; //but this conversion must be explicitHowever, the above can be forced by explicitly typecasting the extended object as an object, for example:
teeshirt p = TeeShirts[x]; clothing q = (clothing)p;This is valid when you want to perform a function on a teeshirt object which could be equally well performed on an item of clothing of any kind, such as reading its washing label viz:
ReadWashingLabel(q = (clothing)p);where the above is a method within the Class Clothing{ } of which the Class TeeShirt{ } is an extension.
To test whether teeshirt is an extension of clothing, thus avoiding a possible error use the test:
if(teeshirt instanceof clothing) q = (clothing)p;
long x = 23.789; string y = "Output Value = " + x;This is interpreted as:
string y = "Output Value = " + toString(long x);A toString() method is defined for all primitive types and one can be written into the class code for any object.