Image

Java versus JavaScript

Since there's confusion now and then about the difference between Java and JavaScript -- and since I've yet to see an explanation geared toward people who don't already know the difference -- I took some time to write what I think is a decent explanation. Comments and suggestions are welcome.

Java versus JavaScript

Java and JavaScript both look a little like C, and that's about as much as they have in common. There is a huge list of specific ways in which they are fundamentally different, but the most important difference -- as far as a web developer is concerned -- is the context in which the two languages are used.

Context

JavaScript is part of the web browser*. JavaScript programs are used to control pretty much any part of the browser window, including the content of the web page.

Java on the other hand -- although it can run inside a browser window -- is not part of the browser. A Java program runs inside an "applet," which is kind of like a container for Java programs. That container is just one element on the web page. Everything that the Java applet does, it does inside that container. It can't manipulate the web page or the browser window.

How To Tell

If you've found a web page that's doing something neat, view the HTML source. If you're familiar with HTML, it should be self-explanatory. JavaScript will be inside <script> tags. Java applets will probably be in <applet> tags.

If you're looking at a program's source code (not HTML source), and can't decide whether it's Java or JavaScript, you can tell based on certain keywords. Each language has several words that are used in that language but not the other. Generally, if you see the words public, private, or class, you're dealing with Java. If you see the words var, function, document, or window, chances are it's JavaScript.



* JavaScript exists in other contexts, but if you're writing JavaScript code that isn't destined to run in a web browser, you don't need the differences between Java and JavaScript explained to you.