Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As a couple of other answers have explained, the Java compiler takes a file name as an argument, whereas the interpreter takes a class name. So you give the <code>.java</code> extension to the compiler because it's part of the file name, but you don't give it to the interpreter because it's not part of the class name.</p> <p>But then, you might wonder, why didn't they just design the Java interpreter differently so that it would take a file name? The answer to that is that classes are not always loaded from <code>.class</code> files. Sometimes they come from JAR archives, sometimes they come <a href="http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html" rel="noreferrer">from the internet</a>, sometimes they are constructed on the fly by a program, and so on. A class could come from any source that can provide the binary data needed <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#defineClass%28java.lang.String%2C%20byte%5B%5D%2C%20int%2C%20int%29" rel="noreferrer">to define it</a>. Perhaps the same class could have different implementations from different sources, for example a program might try to load the most up-to-date version of some class from a URL, but would fall back to a local file if that failed. The designers of Java thought it best that when you're trying to run a program, you don't have to worry about having to track down the source that defines the class you're running. You just give the fully qualified class name and let Java (or rather, its <code>ClassLoader</code>s) do the hard work of finding it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload