■ See also casting
■ Here are the rules for converting floating point double (64 bit) to float (32 bit):
A double which is too negative (meaning too small), or too big for float becomes a negative or positive floating point zero.
A double NaN becomes a float NaN.
You will never have any problems going from float to double because double is bigger.
■ Any narrowing conversion of a floating point NaN to an integer turns it into zero.
■ These are impossible conversions which won't compile:
primitives to object handles
null to a primitive
a primitive to boolean
a boolean to a primitive
■ The method signature provides automatic parameter conversions of primitives, as long as the casting is up.
■ However any forced casting of the parameterss overrides the above rule. i.e.
Math.max( int, int ) if passed an ( int, long ) converts the first int to a long (up), thus forcing usage of the Math.max( long, long ) method version, not the Math.max( int, int ) version.
But passing a cast parameter like Math.max(int, (int) long) can force it to use the Math.max(int, int) version.
■ The method’s return type is ignored when the compiler makes the above decisions.
■ Any assignment variable present in the statement is also ignored. i.e.
double d = Math.max(int, int) passed (int, int) still uses the (int, int) version - the double doesn't affect it.
■ Some points about primitives as parameters in method calls:
Integer types can and must go wider, never narrower.
They can be explicitly cast downward right in the parameter, if desired.
Integer to floating point issues ignore size. You can pass any integer type to a float or double parameter.
■ Narrowing object conversions (traveling up the object hierarchy) require an explicit cast to compile, but even then won’t run. i.e.
Object O = new Object( );
Panel P = new Panel( );
P = (Panel) O; gives a runtime ClassCastException because P is bigger than (contains more than) O and hence O can't fulfil what's in P.
■ When narrowing is forced on you, you must explicitly cast. Many methods always return an Object, so their results must always be explicitly cast downward. i.e.
Double d = (Double)
myStack.pop( ); needs the
cast because the pop(
) method always
returns an Object.
■ Widening object conversions (traveling down the hierarchy) are always permissable without an explicit cast. i.e.
Object O = new Object( );
Panel P = new Panel;
O = P; works because P already has everything O has, and thus can supply it.
■ Automatic widening also occurs in method calls. i.e.
public void method(Object o); This method can accept objects from any class.
■ Regarding default byte to char conversions:
The uppercase letters 'A' through 'Z' convert to '\u0041' through '\u005a',
The lowercase letters 'a' through 'z' convert to '\u0061' through '\u007a',
The digits '0' through '9' convert to '\u0030' through '\u0039', meaning zero becomes hex 30 which is 48 in decimal integer types
The dash character '-' converts to '\u002d', HYPHEN-MINUS,
The colon character ':' converts to '\u003a', COLON.
The underscore character '_' converts to '\u005f', LOW LINE.
■ You can determine the name of the Charset used for these conversion defaults with the getEncoding( ) method of the InputStreamReader class. You can control the Charset with InputStreamReader constructors.
■ From String to any numeric wrapper: Wrapper.valueOf(str). Gives same result as parseInt(str), parseLong(str) etc.
■ From String to any primitive: Wrapper.parseInt(str), parseLong(str), etc.
■ From String to char[ ]: str.toCharArray( ) Gives same result as str.getChars( 0, str.length(), chararray, 0 );
■ Unlike String, StringBuffer has no equals( ) method override. So StringBuffer .equals( ) are all the same as using ==.
Also works coming from boolean, char and Wrappers. You can also say Wrapper.toString(int), toString(long), etc.
■ The .toString( ) method always creates a new String object with the effect of new.
■ toString( ) is the equivalent of saying: getClass( ).getName( ) + ‘@’ +Integer.toHexString(hashCode( )) .
It works for objects and prints the default Object string such as
■ Converting from StringBuffer to String: str = sb.toString();
■ From String to String: str1 = str1.toString(); This just returns itself. Compiles OK.
■ If you don't care about performance this works nicely:
double d = 12.34;
String s = "" + d;