Assignment of Objects, Parameters, and Casting

Python Java
Variables and parameters themselves are typeless. Any variable or parameter can name any thing and be reset to any thing. The object to which a variable or parameter refers has a type. Variables and parameters are declared with a type. A variable or parameter can only be assigned a value that is compatible with its type.

Values of less inclusive types can be assigned to variables or parameters of more inclusive types. Reversing this order requires explicit type conversion before assignment.

Example:

Variables and parameters are declared with a type. A variable or parameter can only be assigned a value that is compatible with its type.

Values of less inclusive types can be assigned to variables or parameters of more inclusive types.  Reversing this order requires explicit type conversion before assignment.

Example:

String s1 = “Hello”;
Object obj = s1;
String s2 = (String)obj;

void aMethod(String s){
    String other = s;
    return;
}

aMethod(s1);
aMethod((String)obj);

© Ken Lambert. All rights reserved.