What's New
Dev Tools
Site Map

Java vs. JavaScript

JavaScript and Java are compared under the following categories:

General Items

Java is more of a traditional compiled language with tighter restrictions, while JavaScript is interpretive and more ad hoc, as described in the following table.

Item JavaScript Java
Compilation JavaScript is not compiled. It runs interpretively. Java is compiled into an intermediate bytecode (which is machine-independent). A machine-specific Java Virtual Machine then interprets this code to run on the specific platform.
Platforms JavaScript can only run within browsers that support it, however today’s major browsers run on the most client platforms. JavaScript does not run standalone. Java can run as applets within browsers that support it (i.e. any modern browser). It can run as a standalone application on most major computing platforms (Windows, Unix, Linux, etc.). Java procedures can be run within major databases vendors (e.g. Oracle, IBM, Informix).
Minimum Development Environment JavaScript is mainly developed with a text editor. The free Java Developer Kit (JDK) is the minimum required to develop Java. The JDK includes a compiler, a debugger and an applet viewer. There is also the free Java Server Development Kit (JSDK) for building logic in the middle tier. Most people use tools like the free Eclipse IDE to help develop their Java code.
Language Philosophy JavaScript tries to continue running unless it encounters an insurmountable syntax error. Java is on the stricter end of software tools. The compiler disallows many situations that might be allowed in other languages and the runtime engine raises an exception when questionable or dangerous actions take place. This is described in more detail in a separate section below.
Learning Curve JavaScript is a smaller and more straightforward to learn. Java is larger. While it is less complex than C++, it is still a substantial language.


Web Presentation Capabilities

Both languages can produce webpages that behave in a dynamic interactive fashion.  The techniques used and market penetration are very different, as described in the following table.

Item JavaScript Java
Web Presentation Ability JavaScript can build dynamic webpages. 

JavaScript provides more direct control over the browser (e.g. controlling the back button, refreshing the page, etc.)

Java can be used to build Applet pages but this technology is dated and no longer widely used. When Java is used, its JSP (JavaServer Pages) derative is generally used to build the webpage which includes HTML and JavaScript.
Web Presence The relative predominance of HTML/JavaScript on the web can probably be attributed to easier GUI development and faster webpage downloading times.  While some Java applets still exist on the web, its use has become rare.
Developing User Interfaces User Interfaces are developed in HTML which is relatively straightforward to learn. JavaScript is included in the webpage and it   interacts with the HTML form elements.

A common requirement is develop HTML-based webpages with dynamic (database-driven) content.  This is commonly done with Microsoft ASP, Java Servlets or JSP (the latter two are middle-tier Java applications whose job it is to produce HTML, along with needed JavaScript).

In plain Java, user interfaces are developed in AWT or Swing. Working directly with these Java libraries is challenging and the user interfaces were often not all that easy to use. Java mostly lost this market.
Client-side Security JavaScript does not allow direct access to a user’s hard-drive (beyond Cookies which the browser directly controls).

Over the year, security holes have been found in JavaScript.

Java is very strict about not allowing access to memory or devices outside the applet. Certain actions are allowed as long as the user explicitly grants permission.
Secrecy of  Client-side Code JavaScript can be directly viewed using "View Source". Specifying an external JavaScript file (using the SRC attribute) provides a level of indirection but the knowledgeable web surfer can still view the code by requesting the URL of the JavaScript file. When applets are used, compiled Java bytecode is sent to the client so a casual web surfer cannot easily understand your Java logic. However, a determined hacker can use inexpensive Java decompilers to recreate readable source code. To counter this, the Java developer can use Java code obfuscation tools to make it more difficult to understand the code.

However, sensitive code or proprietary algorithms are best executed as server code (behind a firewall) where they are not accessible.

Language Interoperability When the webpage includes an applet, an HTML tag can be specified so the Java applet has initiating parameters.

JavaScript can call public Java methods and read/update public Java variables for an applet running on the webpage.

When given permission by the webpage (i.e. "MAYSCRIPT" attribute specified in APPLET tag), a Java applet can call JavaScript functions on the parent webpage, it can read/update HTML form elements and it can directly execute JavaScript code (e.g. an alert).


Middle Tier Capabilities

Both languages have "server-side" or middle tier capabilities.   The solutions and their industry support is very different.

Item JavaScript Java
Middle Tier Capabilities In the early days of the Internet, server-side JavaScript could be used to produce webpages with dynamic content (e.g. including database content).  It could also be used to parse webpages submitted from the client, validate the input and update data stores. Java has a comprehensive model for supporting middle tier content called Java 2 Enterprise Edition (J2EE).  It is a considerably more comprehensive model than Server-side JavaScript.
Middle Tier Presence Server-side JavaScript is virtually non-existent. Java (J2EE) is widely supported in the middle tier (BEA, IBM, Oracle, Sybase, Apache/Tomcat, etc.)  Many vendors produce tools to help create, debug and deploy middle-tier Java solutions.

Java has a very strong presence in the middle tier. Its major middle-tier competitor is Microsoft (whose solution uses dot-Net suite in the middle tier).

Major Language Features

A superficial syntax similarity is the main similarity between the two languages, as illustrated in the following table.

Item JavaScript Java
Language Syntax JavaScript’s major language constructs (e.g. if, while, for, switch) closely resemble Java. See 99 Bottles code sample.

JavaScript differs significantly from Java when referring to "external" features (e.g. webpage contents, object libraries, etc.)

Java’s major language constructs (e.g. if, while, for, switch) were based on C. The 99 Bottles of Beer code sample shows the similarity of the code when writing simple programs.

Java and JavaScript’s surface resemblance might lead one to believe that the languages are more closely related than they really are (their differences are much more significant than the similarities).

Variables For simple data types, variable declarations are generally optional. Variables are not declared with a specific data type. A variable’s data type is determined by the latest value assigned to it. All variable must be declared and data types must be specified. A variable cannot change data type.
Functions / methods Invokable routines are called functions in JavaScript

- simple data types are passed by value while objects are passed by reference

Invokable routines are called methods in Java

- primitive data types are passed by value while objects are passed by reference

Object oriented support Most people would call JavaScript object-based (even this might be generous). It doesn’t provide support for the key object-oriented features outlined below. Java is object oriented since it provides the key tenets (see below). Java doesn’t support multiple inheritance (what some call the goto of OO). However, with inheritance and implements together, Java can achieve most multiple inheritance objectives in a more stable manner.
OO: encapsulation Very little encapsulation is available.   Only function-level variables are private. Any variable in any script block that is not defined within a function is completely public (or global).  All functions are public.  Full range of encapsulation supported (public, protected, private) for methods and variables.
OO: inheritance Netscape says that JavaScript supports prototype-based inheritance (as opposed to the usual class-based inheritance). This approach lets you "inherit" properties (which could be useful) but it does not support method/code inheritance which is generally considered the more important inheritance capability. Inheritance (methods and properties) is supported.
OO: method overloading JavaScript supports only allow a single function to use a specific name on a page. Overloading can be simulated by passing that one function more or fewer arguments (which JavaScript allows) and then writing code within the function to use the arguments property (which indicates the number and value of parameters passed). Normal overloading is supported (i.e. multiple methods within a class can have the same method name but differing parameters or "signatures").
OO: polymorphism Not supported: a byproduct of not supporting code/method-level inheritance is that polymorphism is not possible. Supported.


 Language Philosophy: Handling Questionable Situations

JavaScript runs interpretively (no compile) so the language recognizes many potential coding "mistakes" and tries to take a suitable course of action so that execution can continue. This "forgiving" philosophy can also hide errors during the development/testing process (causing subtle production bugs).  By contrast, the Java compiler explicitly disallows many actions that might cause later problems.

Questionable Situation JavaScript Action Java Action
value is assigned to an undeclared variable JS implicitly declares a variable and uses that variable Compiler stops it
value of uninitialized variable is used (e.g.

x = y;

where variable y has not been initialized)

JS defaults the variable value ("undefined" for string, "NaN" for number) Class-level variable: Java defaults number to zero, boolean to false and String or other object to null. 

Method-level variable: compiler disallows it. The compiler even traces through several layers of "if" to determine if a variable might be uninitialized (for any branch)

numeric variable is assigned a string value JS changes the variable type and continues Compiler stops it (generally must explicitly change between data types)
string concatenated to a number or boolean JS converts number or boolean to string and continues with concatenation operation. Same as JS for number and boolean.
if (x=5), ie. "=" coded instead of "==" value assigned to variable and evaluated (non-zero value evaluates to true) Compiler stops it.
custom object has three properties x, y and z (as defined in JS custom object or Java Class) but coder refers to another property (e.g. w) new property (w) is added to object and execution continues Compiler stops it.
parseFloat("banana") – coder tries to explicitly convert a string to a number JS places result NaN (not a number) in resulting variable but continues Java throws an exception (which code must catch or the program stops executing).
invalid numeric operation takes place (e.g. divide by zero) JS places special value (e.g. INFINITY, NaN, MIN_VALUE, etc.) into the number but continues For integer arithmetic, Java throws an exception. For float arithmetic, Java assigns very similar values like NaN, INFINITY, etc.
try to assign a value to array position beyond the defined array size (e.g. x[8] = "tom" when array x is only 5 long) JS increases the size of the array to accommodate (in example, array size increased to 9 (i.e. for 0 to 8) and "tom" is assigned to x[8]). Java throws an exception (C, by contrast, merrily overwrites whatever happens to be at that memory location).
try to refer to a array position beyond the defined array size (e.g. y = x[8] when array x is only 5 long) JS increases the size of the array to accommodate (in example, array size increased to 9 and then y is assigned value "undefined" since x[8] was not initialized). Java throws an exception (C, by contrast, would assign whatever value was at that memory location).
too many variables are passed to a function JS continues (additional parameters are available to function via the arguments array) Compiler stops it.
too few variables are passed to a function JS defaults the unspecified variables to null Compiler stops it.
a function/method of the same name appears twice in the webpage/class the later function in the webpage takes precedence (and earlier one is ignored). The individual function definitions or parameters do not change this. If method parameters differ, it is allowed (method overloading). If parameters are the same for two methods in the same class, the compiler stops it.
Invalid memory location accessed JS does not have explicit pointers so this is not normally possible. Java does not have explicit pointers so this is not normally possible.



On the client-side, both languages let the developer create dynamic web content but the underlying techniques for doing so are very different. JavaScript/HTML is far more widely used in building client-side web content (including complex dynamic eCommerce sites). Java’s early promise to become a major client-side web tool has faded.

In the middle tier, Java has a powerful widely supported solution in Java 2 Enterprise Edition.  It is supported by many vendors and widely used in industry as a effective means of producing dynamic (HTML-based) webpages and for implementing business logic. Server-side JavaScript is no longer in use.

JavaScript and Java really have very little in common, apart from a superficial similarity in syntax. In most respects, these languages are very different. This shouldn’t be surprising, however. JavaScript is an simple interpreted scripting language intended to run in a browser while Java is a powerful general-purpose language that can be used to create many different types of applications.

Seasoned developers will prefer coding in Java for its object-oriented support and its much tighter approach to developing code. Experienced developers may find JavaScript's loose approach frustrating when adding dynamic capability to HTML webpages but casual developers will appreciate JavaScript's shorter learning curve.

... back to Development Tools Page

Copyright 1997-2019, Woodger Computing Inc.