Java -vs- JavaScript

Despite the unfortunate naming which causes a lot of confusion, there's a huge difference between Java and JavaScript.

One possible cause of confusion is that Java is an object-oriented language and JavaScript is an object-based language. The difference here is very subtle.

  • Both languages treat "program "elements — such as a text field or a pop-up window — as objects, which can pass instructions to one another.

But a true object-oriented language, as Java is, also makes heavy use of inheritance: Objects can be extended by inheriting functionality from existing objects and adding new attributes. JavaScript does not have this ability; with JavaScript, objects can be created, but cannot inherit properties. Object-oriented languages can also make use of encapsulation, overloading and polymorphism.

The languages also differ in how each interacts with browsers and Web pages.

  • You can use Java (which requires a compiler, a program that translates human-readable code into machine-readable executables) to create either standalone applications or applets that run within a browser.
  • JavaScript, on the other hand, works only within a browser and is not compiled. You cannot use it to develop standalone applications. JavaScript is a scripting language for writing short programs, or scripts, such as log-on procedures. JavaScript originated as Netscape's LiveScript, but because of its similarity in syntax to Java, it was renamed JavaScript.
  • Java applets are downloaded as separate files onto a client machine and are executed independently of HTML files and images; they are not visible in the source file.
  • JavaScript is part of an HTML file and is visible in a document's source. You can use JavaScript to manipulate all of the HTML elements on a Web page.
  • Java applet, however, is a self-contained application within a Web page, and is limited to the region it occupies within a window. Applets can react only to commands within the window's boundaries, but with JavaScript, events anywhere in an HTML page can be passed to Java applets.

The Java executable (it's not an executable per se, but it is compiled code and is referred to in the industry as an executable) is downloaded to the client computer and run by the Java virtual machine (JVM). Because the client's operating system and machine are unknown, Java cannot be sent to a client as machine language. The inventors of Java came up with a clever solution: Write a JVM for each platform. The executable content is downloaded as bytecodes, which the JVM then interprets into machine language.

Java, due to its complex syntax, rules, and restrictions, may seem daunting for those without programming experience. JavaScript, on the other hand, is easy to use and learn. You can create sophisticated applications just by just learning some commands, syntax rules, and the object model of the browser.

Comparisons and Contrasts between JavaScript and Java.

JavaScript Java
Interpreted (not compiled) by client. Compiled on server before execution on client.
Object-based. Code uses built-in, extensible objects, but no classes or inheritance. Object-oriented. Applets consist of object classes with inheritance.
Code integrated with, and embedded in, HTML. Applets distinct from HTML (accessed from HTML pages).
Variable data types not declared (loose typing). Variable data types must be declared (strong typing).
Secure. Cannot write to hard disk. Secure. Cannot write to hard disk.

Similarities

  1. Both can be used for enhancing the capabilities of Web pages.
  1. Both can run on the client's machine - i.e. the machine where you have your browser, not the server where the page came from. This is in contrast to "CGI programs" - programs which run on the server and use the CGI to communicate with the client. Java can also be used to write CGI programs, and as better Java server packages become available, this use is becoming more popular.
  1. Because they can run on the client, both can be used in place of some CGI programs to reduce the load on servers.
  1. Both can be misused to put annoying scrolling text, looping animations and the like on Web pages (Of course, this is my opinion only). In fact, the abuse of both Java and JavaScript for these purposes has given both languages a bad reputation, and many people think that these are the only uses of Java and JavaScript.
  1. Both have some level of security built in to guard against malicious use. No computer system is ever 100% secure unless it's isolated in a locked room surrounded by armed guards, but the developers of Java and JavaScript have taken some care in their security. But Java is a lot more secure than JavaScript - see the differences below.

Differences

  1. JavaScript has been developed specifically by Netscape and Sun for enhancing the capabilities of Web pages.

    Java, on the other hand, is a general-purpose programming language. One of its uses is for Web pages, but it can be used for non-Internet applications as well.

  1. JavaScript is a "scripting language", which means the program reading it (Netscape) interprets exactly what you write.

    Java is not interpreted directly. Instead, you must compile it first, and that's what the browser reads. Unlike traditional compilers, where the source code is converted into native machine code, the Java compiler generates an intermediate "bytecode" which is independent of any machine. An interpreter is built into the browser to run this code (This is known as the Java Virtual Machine).

  1. Things you write in JavaScript are called "JavaScript scripts"; things you write in Java (for Web pages) are called "Java applets". There's no such thing as a "JavaScript applet" or a "Java script".
  1. Your JavaScript source can be viewed by anybody (e.g. using Netscape's "View Source" command). Your Java source is hidden because it's only the compiled bytecode which the browser uses (This is not a guarantee of security, because there are Java disassemblers available, but at least it's harder to read than JavaScript).
  1. You can write fully-functional JavaScript using a simple text editor. To write fully-functional Java, you need the Java Developer's Kit (But it's free).
  1. Java is a strongly-typed, object-oriented, complex programming language. It's a very powerful language, and easily stands along-side other object-oriented languages like C++ . Some of the syntax is similar to C++, but Java is a lot easier to learn and use than C++.

JavaScript is a loosely-typed language which is much simpler than Java. In complexity, it's at a similar level to BASIC.

Both languages are programming languages, as opposed to HTML, which is simply a way of marking up text. JavaScript is easier to learn, but Java is more powerful for larger applications.

  1. JavaScript was developed with Netscape Navigator in mind, and has good support for an interface with the user's browser. For example, one of the frequently-asked questions about HTML is "How can I control the 'Back' button"? You can't do it in HTML, but you can do it in JavaScript.
  2. Java doesn't provide as much control over the browser, but it has other powerful features.

  3. Java has been designed with security being one of the highest priorities. The security mechanism in Java has been built into the Java Virtual Machine that runs Java programs and applets.
  4. JavaScript, on the other hand, is much less secure. Although some attempts have been made to protect users from malicious JavaScript scripts, security holes are discovered much more frequently than in Java.

Beware that Microsoft's version of JavaScript, which they call "JScript", has subtle differences which make it incompatible with the original JavaScript.