Press "Enter" to skip to content

Comparing Java and Javascript

Perhaps the first thing that most people ask when they hear the names of these two programming languages are. “Why do they have the same name?” and “Are they the same thing?”.

There is a general misconception that because Java and Javascript share a similar name to one another, that they are indeed one and the same; but in reality, they are very different languages, which each address different types of problems and only share the smallest of traits.

As Josh Lee, as well as Greg Hewgill – on StackOverflow – stated: “Java is to JavaScript what Car is to Carpet”.

Let us take a look at the two aforementioned languages in order to familiarise ourselves with their core purposes, where they fit into the development world and how they came to be in order to understand the comparative chart we will show thereafter.

What is Java?

Java is a general-purpose programming language that first showed an appearance in 1995 (after it’s initial creation began in 1991). It was designed by James Gosling and a few others at Sun Microsystems that was later acquired by Oracle Corporation as part of the Sun Microsystems buy-out in 2010.

Java applications execute in a Java Virtual Machine (JVM) which can be run on multiple types of platforms and devices. The only way for Java to run inside of web browsers (which Javascript does) is by embedding Java Applets which is not a standard practise anymore and is strongly discouraged as plugin support is currently being dropped and the browser’s Document Object Model (DOM) is not accessible from within Applets and vice versa. This is where Javascript gains it’s most ground. Java tends to be more focused around standalone applications or web based backend systems. It is a “write once, deploy anywhere” architecture.

What is Javascript?

Javascript is an “object-oriented programming (OOP) language commonly used to create interactive effects within web browsers”. The language was originally developed by Brendan Eich at Netscape Inc.

Javascript was only ever available in the browser up until 2009 when Google Chrome’s Javascript Engine (known as “V8”) was separately released as “Node.js”. This brought about a new era of Javascript on the backend as well as within client side “native/hybrid” applications.

Javascript being run via the Node.js platform had a profound effect globally as Javascript was then able to be used across the development stack (particularly in the case of web applications). This allowed frontend developers to create backend systems and Application Programming Interfaces (API’s) without having to learn another language or waste valuable brain cycles switching between languages during development time.

Comparisons between Java and Javascript

Both Java and Javascript share a similar looking syntax which can be traited from being borrowed from the C language – which a large portion of languages have used as their own building blocks. These include characteristics such as “curly braces” to indicate function/method blocks as well as standard operators, conditions and loops.

If a programmer fully understands one of these languages, it is much simpler for them to be able to pick up and learn the other without a steep learning curve. This is a real benefit.

The largest learning curve between the two usually comes from the addition of the standard libraries on top of the language paradigms themselves, execution differences (how programmes are run) as well as deploying the applications post development phases.

Let us highlight a few comparisons of Java and Javascript below:

  • Java is a statically(strongly) typed language whereas Javascript is a dynamically(loosely) typed language. This means that variables in Java have to be declared by a set object type before being able to use them, where Javascript can be seen to be more flexible and variables can be of any type and reused later on as a different type entirely without throwing a type declaration error.
  • Java is class based and Javascript is prototype based. Although Javascript has been making some large stead into becoming more class based with the latest versions (ES6+) in order to assist developers in true OOP benefits such as classes out of the box.
  • Java uses block-based scoping, whereas Javascript uses function-based scoping.
    This means that Java has an implicit this scope for any non-static methods and Javascript has a global implicit scope; this often needs to be redefined as that in order to not be overwritten or refer to the wrong context within a complex code block. This is one of Javascript’s biggest “gotchas” that is by no means obvious to inexperienced programmers of the language.
  • Javascript supports closures, whereas Java merely attempts to simulate them by means of anonymous classes.
  • All Javascript functions are variadic, whereas Java functions are only variadic if explicitly marked so.
    Albeit not terribly frequently used, there are numerous occasions where being able to have a variable amount of arguments passed to a function is a massive convenience. It allows a single function or method to support overrides without first declaring them. The obvious harm here is the potential for software bugs to creep in without being able to easily spot them.
  • Probably the most obvious comparison is that Java is compiled while Javascript is interpreted – as the latter is a scripting language.

 

As we have now learnt, even-though Java and Javascript both share a similar looking syntax, they are very different in every other aspect. They have both come a long way over the past few decades and are both fully fledged, complex languages that can take many years to master all the nuances and finer intricacies.

 

References:

“Why is JavaScript called JavaScript, since it has nothing to do with Java?” (2010) – Available from: http://stackoverflow.com/questions/2018731/why-is-javascript-called-javascript-since-it-has-nothing-to-do-with-java (Accessed on 26th March 2017)

“What’s the difference between JavaScript and Java?” (2010) – Available from: http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java (Accessed on 26th March 2017)

“Node.js” (n.d.) – Available from https://en.wikipedia.org/wiki/Node.js (Accessed on 26th March 2017)