Java Vs Javascript
Essay by review • December 18, 2010 • Research Paper • 2,454 Words (10 Pages) • 1,258 Views
Intro
Java and JavaScript share many similarities, but are unique in many more ways. The two languages share similar goals and history, but both serve vastly different purposes. While both are object-oriented languages, Java is an interpreted language while JavaScript is, as its name implies, a scripting language, and not a true programming language. Java is meant mostly to be a multi-tiered language, while JavaScript was written to be a client-side language. While the two languages may look very similar at first glance, a line of code identically written in both languages can produce wildly different results. This is because Java is a strongly-typed language while JavaScript is a loosely-typed language. Both exist to make Web pages more interactive for users, but how they go about doing this and how they work is wildly different.
Java History
Java is an object-oriented program language created by Sun Microsystems in May of 1995. The language was originally part of the "Green Project" which was handled by a staff of thirteen people at Sun known as the "Green Team." During it's first releases, the language was known as OAK, and it was originally intended to be used for converging devices, which was seen as the next great computing wave. Most of this never saw the light of day, and as the project was about to be scrapped, the Internet took hold in late 1994; the language was almost perfect for this new platform. It was introduced to the world at a SunWorld conference on May 23, 1995, given its new name Java, which came from the drink of choice most of the Java Technology team drank often in order to work the long hours it took to tweak the interpreter to be used for various operating systems and the Netscape Navigator browser.
JavaScript History
While the name would make it seem as though JavaScript is a mere subset of the Java language, this is inaccurate. In fact, the two don't even show the same creators. JavaScript was created by Brendan Eich in early 1996, who was a member of the Netscape Navigator development team (as such, it was part of Netscape Navigator 2). Its original name was LiveWire, and later LiveScript. It closely resembled Java, and because of the close relationship Sun held with Netscape, the name was changed to JavaScript to further push the "Java Technology" in the world of Web development.
The Similarities
Java and JavaScript are both object-oriented languages, as opposed to event-driven languages and modular languages. This affects the thinking and design paradigms a developer must consider when writing code in either. As such, a developer writing code in an object-oriented language such as Java or JavaScript must be adept in abstract thinking, and analytically breaking down the properties of data as detailed as possible.
Both languages debuted as Web programming languages, and both languages exist for the purpose of allowing a user to interact with Web pages. However, both languages are also not limited to Web development; both are widely used outside of Web development. Java has a much broader range of platforms, such as for client-server applications, enterprise middle-tier development, and even in game development. JavaScript also has great success outside of Web development, and the Spider Monkey JavaScript engine built by the Mozilla Foundation is often found in various game engines and development platforms.
Both Java and JavaScript use the same ANSI C style syntax, the most common syntax style used among programming languages. This C-style syntax that both languages are based upon uses such syntax as curly braces beginning and ending methods and loops, variable declarations, and comments, as seen below.
Java JavaScript
/*************************
* Method comments.
*************************/
private void foo(int iP1, String sP2)
{
double dResult = 0;
//Line comment
for (int i = 0; i < 10; i++)
{
dResult = i ^ i % i + 1;
dResult *= dResult;
}
sP2 = dResult.toString();
} /*************************
* Method comments.
*************************/
function foo(var iP1, var iP2)
{
var dResult = 0;
//Line comment
for (var i = 0; i < 10; i++)
{
dResult = i ^ i % i + 1;
dResult *= dResult;
}
iP2 = dResult;
}
Both languages declare variables in a similar manner, and both languages treat their variables as objects with properties and their own internal methods, such as the Date object. In both languages, simply declaring a variable without assigning it a value will result in a null or undefined value. Working with a variable that has a null value will result in both languages throwing a NullReferenceException. Both languages also have an internal Math object for handling more complex mathematical calculations beyond those offered by the basic operators. With the exception of a few bit-wise operators, both languages have identical operators for addition (+), subtraction (-), division (/), multiplication (*), modulus (%), increment (++), decrement (--), the shorthand operators (+=, -=, /=, *=), and many others.
Divergence
While incredibly similar, both languages differ greatly too. Java is a strongly-typed language, while JavaScript is a loosely-typed language. This requires type-casting in Java to work between numbers and characters, for example, while JavaScript (in most cases) will handle the conversion automatically. Consider the following.
Java JavaScript
int i1 = 1;
String s2 = "2";
int
...
...