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.
|Developing User Interfaces||User Interfaces are developed in HTML which is
interacts with the HTML form elements.
|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.|
users hard-drive (beyond Cookies which the browser directly controls).
|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.|
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.
Both languages have "server-side" or middle tier capabilities. The solutions and their industry support is very different.
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).
A superficial syntax similarity is the main similarity between the two languages, as illustrated in the following table.
(e.g. if, while, for, switch) closely resemble Java. See 99 Bottles code sample.
|Javas 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.
|Variables||For simple data types, variable declarations are generally optional. Variables are not declared with a specific data type. A variables 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
- 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
|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: polymorphism||Not supported: a byproduct of not supporting code/method-level inheritance is that polymorphism is not possible.||Supported.|
|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 = "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).||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 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 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.|
Seasoned developers will prefer coding in Java for its object-oriented support and its
loose approach frustrating when adding dynamic capability to HTML webpages but casual
... back to Development Tools Page