Sitemap
Richard Rosier

Richard Rosier

Software Developer in New Orleans, LA

Java or Javascript: What’s the Difference?

4 min readMay 18, 2020

--

Something that can be really confusing is the similarity in names of two languages, Java and Javascript. Although you can find both languages in use today and there is overlap between them, they occupy different niches and operate in fundamentally different ways. Java was designed to build standalone applications, while Javascript specifically interacts with web HTML. I’ll walk you through the similarities and differences between them, and when you should consider using one over the other.

Similarities

  • Object-oriented programming (OOP): both languages use objects and the objects’ relationships to each other to create computer programs. By being object based, techniques like inheritance, encapsulation, and polymorphism can be used.
  • Front-end use: The way they do it is different, but both Java and Javascript can run in a browser to make websites more interactive; Java uses applets and Javascript is directly embedded in HTML. However, Java applets have been declining in use in websites over the years due to compatibility and security reasons.
  • Back-end: The two can also be used server-side. Java outshines Javascript here, with long-time services Apache, JBoss, and WebSphere. The release of Node.js has allowed Javascript to gain popularity in the back-end area.

Differences

The chart below offers a side by side comparison of the differences between the two languages. They stem from four larger, fundamental differences in how the languages operate: compiling, type-checking, concurrency, and inheritance patterns. These differences allow each to have its own specialization and not be directly competing with the other.

Compiled vs Interpreted

  • Java is compiled, meaning that after the source code is written, it’s first translated into unreadable machine code, and then implemented using a java virtual machine (JVM) or browser. The advantage of compiling first is that it tends to be faster than translating at run time. One disadvantage is that code becomes set once compiled — if you change the source code, you’ll need to recompile. Another is that compiled files cannot be read by people.
  • Javascript is interpreted directly by the browser, without being translated or changed. This allows human readability and is simpler to implement, but is generally not as fast as compiling beforehand.

Static vs Dynamic Type Checking

  • Java is strongly typed, meaning that it has stricter syntax and rules on its usage. For example, you must specify the type of data when declaring a variable (integer, string, etc). The code is checked at compile-time to see what data types are used and if there are errors. The compiler will then know exactly what data type is being used at run-time, and so it will likely execute faster or use less memory.
  • Javascript is not as strongly typed, and you don’t need to specify the data type when making a variable. This allows programmers more freedom, as you can assign types as needed, but does slow down execution because the types of data must be checked at run-time.

Concurrency

  • Perhaps the greatest strength of Java compared to Javascript, concurrency is the ability to process multiple tasks at once. Java can work on many threads in parallel and share memory between threads, resulting in faster execution of code.
  • Javascript has one main thread, a queue system called the event loop, and a forking system called Node Clustering. It uses asynchronous calls, callbacks, promises, and event loops to simulate multi-threadedness. This whole process is a topic unto itself, but suffice it to say that it is a good system, but not as good as actually being multi-threaded.
Press enter or click to view image in full size
A very simplified diagram of how multiple thread processing looks

Class vs Prototype inheritance

  • Inheritance is very important in making DRY (Don’t Repeat Yourself) code. In Java, all objects are classed based: you can’t make a program without making a class. It’s a top down, hierarchical relationship that creates parent/child taxonomies. These relationships can sometimes get tangled up — be careful not to overuse a base class, lest you create the fragile class problem. Additionally, classes are immutable: they can’t be modified at runtime
  • Javascript uses a prototype inheritance pattern: all objects are able to inherit from any other object. So, you can have an object inheriting methods from many unrelated objects, without them being parent/children. You are creating the relationships as you go, and so it is easier give an object only what it needs. Javascript can also modify methods at runtime if desired.
Press enter or click to view image in full size
v1 and v2 are vehicle objects. In Java, they are created using the Vehicle class. In Javascript, they are linked to the Vehicle Prototype. Notice that the arrows are reversed between Class and Prototypal. Information flows downward from a class in Classical, but Prototypal looks up and backward for information.

Which Should You Use?

Now that you know more about how Java and Javascript differ from each other, you may be wondering when you should use which. Both have their strengths, and it really depends on what your goal is.

Get Richard Rosier’s stories in your inbox

Join Medium for free to get updates from this writer.

As I mentioned earlier, Javascript has been designed to be used in a web browser, so consider using it if you are creating single page applications or using front-end technologies like jQuery, ReactJS, AngularJS, or Backbone. You don’t need a compiler to run Javascript, and it is a more forgiving, human-friendly language.

Java, on the other hand, is a more general purpose language. You might consider using Java to create Android apps, analyze big data, or to program hardware. Java typically runs faster, but is heavier, harder to alter, and a stricter language than Javascript.

--

--

Richard Rosier
Richard Rosier

Software Developer in New Orleans, LA