Archive - Originally posted on "The Horse's Mouth" - 2009-05-02 07:52:50 - Graham Ellis
"Where did you get THAT from?" Ever heard that question? I've asked it of Java sometimes, in wondering where a Java virtual machine has found a class from ... and it's not always obvious.
When loading a class, Java looks at each location given in the CLASSPATH in turn ... and if there are import directives in the code, it will also look in the specified subdirectory offset from each member of the classpath. So in this example, there are three places to look on the CLASSPATH (two directory structures and a .jar file) and for each of them it will look in the specified place, and also at two other places which are imported within them - making a possible 9 locations where it might find the Bicycle class.
You can always get the contents of a pint bottle into a litre bottle, but only SOMETIMES can you get the contents of a litre bottle into a pint one (those some times being if the litre bottle is only half full or less!).
It's the same with variables ... a float can always go into an integer, but an integer may loose something if it goes into a float.
Java automatically does conversions where it's certain that nothing will be lost (coercion), but if something might be lost, it makes the programmer say "yes, I AM sure" by using a cast.