2016-12-29



Java vs JavaScript—what’s the difference? Ask around and you’ll quickly pick up on the fact that there are few questions that more quickly conjure a web developer’s ire. After all, as famed web developer Jeremy Keith once poignantly put it—”Java is to JavaScript as ham is to hamster.” But for the uninitiated, you’d be forgiven for thinking there was some sort of link—both languages make a prominent appearance in the world of web development.

As it turns out, JavaScript was originally called Mocha upon its conception at Netscape in May 1995, briefly changed its name to LiveScript in September, before finally receiving its current namesake in December that same year upon receiving a license from Sun Microsystems, the creator of Java. It would not be too much of a stretch to speculate that JavaScript was chosen as a marketing move, given the popularity of Java at the time.

Today however, the two languages have evolved to fill very different roles in web development and programming as a whole. Let’s take a closer look at the similarities and differences between Java and JavaScript.

What is Java?

Java is a general purpose programming language designed with one mantra in mind—”write once, run anywhere.” Java applications are compiled into bytecode that can run on implementations of the Java Virtual Machine (JVM). JVM helps bridge the gap between source code and the 1s and 0s that the computer understands. Any machine that has the JVM installed can run Java. In web development, Java features most prominently as a server-side language and the programming language of choice for mobile apps on the Android platform. It also still has a decent presence on the front-end as a Java applet, although this is falling out of favor due to security concerns.

What is JavaScript?

Alongside HTML and CSS, JavaScript (standardized as ECMAScript) is considered one of the big three core components of the web. Employed by a majority of websites, JavaScript is a scripting language that typically runs in the browser and makes web pages dynamic and interactive. Today JavaScript is also quickly growing as a server-side technology since the release of Node.js in 2009.

Java vs. JavaScript: Major Similarities

As different as they are, there are some top level similarities worth considering, especially if you are looking at web development when comparing Java to JavaScript.

Object-Oriented Programming (OOP). Both languages require the developer to code in terms of objects and their relationships to one another. By extension this gives both languages access to techniques like inheritance, encapsulation, and polymorphism.

Front-End Development. Both languages can be used in aspects of front-end development. JavaScript can be embedded directly into HTML, implemented as a framework or library; Java can be used as a Java applet.

Back-End Development. Both languages can be used on the server-side. Java has long been used to power back-end technologies like Apache, JBoss, and WebSphere. Node.js has become a launch pad for JavaScript-powered servers.

Java vs. JavaScript: Major Differences

It’s important to remember that Java and JavaScript were developed to serve entirely different purposes. Java was designed as a general purpose programming language for building standalone applications, whereas JavaScript is a scripting language built specifically to interface with web technologies, namely HTML.

When Java was released by Sun in 1991, it was initially being used to program consumer electronics like VCRs. JavaScript was introduced to be used with Java as a client-side scripting language that could run in the browser without having to be compiled. Let’s take a closer look at some of the major differences between these two languages.

Compiled vs. Interpreted. Java is considered a compiled programming language. JavaScript is considered an interpreted scripting language. The difference is in the implementation: Java is compiled into bytecode and run on a virtual machine, whereas JavaScript can be interpreted directly by a browser in the syntax it is written (although it is usually minified in practice).

Static vs Dynamic Type Checking. Java uses static type checking, where the type of a variable is checked at compile-time. The programmer must specify the type (integer, double, string, etc.) of any variable they create. JavaScript, like most scripting languages, uses dynamic typing, where type safety is verified at runtime. It is not required for a programmer to specify the type of any variable they create. There are many pros and cons for these two paradigms, but the primary advantage of static type checking is that type errors are caught early in development, and because the compiler knows exactly what data types are being used, code typically executes faster or uses less memory. The primary advantage of dynamic type checking is programmer productivity—you are free to assign types at your leisure.

Concurrency. The ability to handle the execution of several instruction sequences at the same time is handled very differently between Java and JavaScript. Java makes use of multiple threads to perform tasks in parallel. JavaScript, particularly as it exists as Node.js in server-side applications, handles concurrency on one main thread of execution via a queue system called the event loop, and a forking system called Node Clustering. For most use-cases, both methods work just fine, but Java is generally faster because thread to thread memory sharing much faster than interprocess communication (IPC).

Class Based vs Prototype Based. Java follows class based inheritance—a top down, hierarchical, class-based relationship whereby properties are defined in a class and inherited by an instance of that class (one of its members). In JavaScript, inheritance is prototypal—all objects can inherit directly from other objects. Hierarchy is accomplished in JavaScript by assigning an object as a prototype with a constructor function.

Should I Use JavaScript or Java for my Next Project?

As with all languages, the choice really boils down to what you’re trying to build and what resources you have at your disposal. JavaScript is still very much a web technology, whereas Java is a general purpose language that can build anything.

You should consider Java if your project involves…

Android Apps

Enterprise Software

Scientific Computing

Big Data Analytics

General Purpose Programming of Hardware

Server-Side Technologies like Apache, JBoss, Geronimo, GlassFish, etc.

You should consider JavaScript if your project involves…

Dynamic single page applications (SPAs)

Front-End technologies like jQuery, AngularJS, Backbone.js, Ember.js, ReactJS etc.

Server-Side technologies like Node.js, MongoDB, Express.js, etc.

Mobile App Development through PhoneGap, React Native, etc.

Keep in mind that neither list is extensive, these are only meant as a starting point to help you get a feel for what you can expect and what keywords you can use to assess the best language for your needs.

Show more