DEV Community

Cover image for Java vs. JavaScript: Relatives, Rivals, or Friends?
John Selawsky
John Selawsky

Posted on

Java vs. JavaScript: Relatives, Rivals, or Friends?

Know the technical differences between the two programming languages

What a mess it could be to share the same name — especially if you are a programming language. Even if you are already over 20 years old, new IT specialists periodically confuse you with your namesake. This happens all the time with Java and JavaScript despite the fact that they are not related at all! As someone on the internet said, they correlate in much the same way as a car and a carpet do.

Why do these two languages have such similar names? How do they differ from each other and what else do they have in common? This article will provide the answers to these questions.

In the Beginning, It Was Java

Sun Microsystems started to develop the future Java language in 1991. It was intended for interactive television and amateur devices. However, the language turned out to be too complicated for that kind of modern device. First, Java was called Oak after the oak tree that grew near the office of the main language’s creator James Gosling. Later, the project was renamed Green. Finally, possibly under the influence of repeated caffeine consumption, we received the name Java. Like a brand of coffee. Or an island.

Java was first released in 1995. Its motto promised us that what is once written in this language will work everywhere (“Write Once, Run Anywhere”). This means that the same code can be compiled for different platforms thanks to a virtual machine. This, as well as the familiar C-like syntax and the ability to work in browsers, led to the extremely rapid growth of Java’s popularity.

The Same Year, A Little Bit Later: Meet JavaScript!

Not all younger IT people remember Netscape Navigator. It was the very first successful Internet browser that appeared back in 1994. Actually, the birth of the browser and the development of Internet technologies led to the need for a language that could work on them.

So the same year the world saw Java 1.0, a Netscape employee named Brendan Eich wrote something special. The employer instructed Brendan to create a language that runs natively in a browser (as opposed to Java, which requires encapsulated Java programs to download) and is simple enough to attract non-professional programmers. As Java grew in popularity, Eich’s managers wanted their brainchild to “look like Java.” Eich obeyed to some extent but did not deviate from the main goal. He was writing a client-side scripting language for non-professional developers, and it doesn’t look like Java at all.

However, the Netscape team needed to advertise their new language. This is probably why the JavaScript project was originally named Mocha (that’s also coffee, by the way). Later, the name was changed to LiveScript.

Almost at the same time, the first browser war began between Netscape Navigator and Internet Explorer. The guys from Netscape needed to strengthen their browser with a new language. The Netscape team agreed with Sun Microsystems that Java would work in Netscape Navigator, and the language under the license was renamed from LiveScript to JavaScript due to marketing issues. Java was a hot thing at that moment.

Technical Differences Between Java and JavaScript

So, Java and JavaScript are both programming languages. They’re very different, though. Let’s talk about the main technical differences first.

Class-based Java vs. prototype-based JavaScript

Both Java and JavaScript are object-oriented, but Java is a class-based language, while JavaScript is prototype-based. What does that mean?

In Java, everything is a class. Class is a reusable template to produce particular objects. Objects are just these data structures we use in programming to store info. For example, Cat could be a class with age, color, name, and the ability to meow. Ginger Fluff, aged three, is a particular object of the Cat class.

So in Java, we use templates — classes — to model real-life objects and situations and then create particular instances. Let’s see an easy example: a program that prints the phrase “I am a software developer” into the console.

Public class Developer {
public static void main (String[] args) {
System.out. println(“I am a software developer”);
}
}
Enter fullscreen mode Exit fullscreen mode

Here, we created a class, a main method (a function), then we called the println method. In Java, you wrap everything in a class.

This orientation on classes makes the code more complicated and verbose, but at the same time, it has a more readable and logical structure, so it simplifies the maintenance and support of big projects.
In JavaScript, you don’t need to do everything inside the class. Here is a JavaScript program to print out a phrase:

console.log(“I am a software developer”);
Enter fullscreen mode Exit fullscreen mode

A few years ago, classes were introduced to JavaScript. They are a kind of syntactic sugar over JavaScript’s prototypal inheritance mechanism. However, this Class syntax doesn’t introduce a new object-oriented model but rather provides a simpler, cleaner way to create objects and organize inheritance. Here is an example:

class Programmer {
constructor(name, language){
this.name = "John";
this.language = "JavaScript";
}
}
Enter fullscreen mode Exit fullscreen mode

Execution difference

The basic principle of running programs in these languages is different: Java is a compiled language, while JavaScript is an interpreted language.

When you write Java code in an integrated development environment (also known as an IDE), it is compiled to bytecode. An ordinary human can’t read it, but Java has its special Java virtual machine (JVM) that can easily run it. The WORA principle actually applies to this as well: Using a compiled language means that making changes basically involves rebuilding the program. Needless to say, this can be a complex process and requires the use of dedicated software. Fortunately, Java code is compiled first and then run later, so you can see the structural problems right away.

JavaScript is a scripting language, so it is made up of plain text. So, the code is executed in the same syntax as you write it, line by line. At some point in the past, this meant slower execution. Don’t worry, this is not the case right now.

However, you should keep in mind that this causes another difference between Java and JavaScript: You can only find bugs and problems while the program is running — not before. After compiling a Java application, you cannot change it on the fly. You need to edit the original code. JavaScript code can be modified without compilation or interpretation. “Classic” JavaScript is executed right in the browser, and this is a pro and a con because sometimes it is tough to find a bug. In Java, the compiler helps you. That’s one of the reasons why some experts recommend learning compiled languages like Java first even if JavaScript is somewhat easier on the first steps.

Typing

Java is a statically typed language, while Javascript is dynamically typed. Declaring variables in Java is more rigid than in JS. What does that mean? Let’s look at this Java code:

String myString = “I am String in Java”;
int number = 7;
Enter fullscreen mode Exit fullscreen mode

Here, we declared a string with the name myString and put a particular meaning (“I am String in Java”) in it. Also, we declared an integer and its value is 7.
In Java, you should specify the types of values that you are going to store into a specific variable. Once you have declared that a variable has a particular data type, it should be the same type throughout its lifetime. So it is impossible to write something like this in Java because “Alex” is a string:

int number = 7;
number = “Alex”;
Enter fullscreen mode Exit fullscreen mode

But the number variable was declared as an integer number.

Java doesn’t let you do this.

What about JavaScript? As I said before, it is dynamic. So here is the JavaScript code:

let myString = “ I am a String or something else in JavaScript”;
let number = 7;
Enter fullscreen mode Exit fullscreen mode

In JavaScript, you can reassign your variable with different types:

let number = 7;
number = “Seven”
Enter fullscreen mode Exit fullscreen mode

So now our number variable is a String. This is absolutely normal for JavaScript. It is convenient, but it could be tough for code support.

Development speed and security

In a very general way, a Java application takes a lot longer to get started, but once you’ve kind of set up the structure for your application, the maintenance cost is much lower compared to a JS application. The latter has a simple initial deployment time, the application is relatively simple, but the level of service required for a JavaScript application increases over time.

Java always comes with a slow start, but it has a very stable future compared to JS. So it is easy to get started with, but once you start working, you have this massive application that is a little annoying to maintain.

Concurrency/Multi-threading

In the classical sense of multi-threading, JavaScript is single-threaded. However, it compensates for single-threading by using asynchronous programming and event looping. The event loop is responsible for executing code, collecting and processing events, and executing queued subtasks. This model is quite different from other programming languages such as C and Java. Whenever an API call or some other lengthy process is required without blocking the rest of the program, the processing loop is responsible for waiting. When the asynchronous task completes, the main thread can detect the results of the task.

In Java, multi-threading is available out of the box, and it is implemented very efficiently. You just need to figure out how it works. Threads share the same memory area, so you can quickly switch between them. Threads are independent of each other. One thread does not affect the work of other threads.

What Can You Build in Java and JavaScript?

Here is one of the most popular questions about the differences between Java and JavaScript. Well, classical JavaScript is all about frontend programming. You may call it one member of the main triad of the web together with HTML and CSS. HTML is the structure of a website, CSS is responsible for decoration (formatting, color), and JavaScript adds dynamic interaction. Classical JavaScript runs in the browser on the client side. So the main area of JavaScript is the frontend of websites. However, the language is developing and is now using different libraries and frameworks. JavaScript can do much more. For example, you can make web and mobile applications and even games.

Java is a leader in the server-side programming of huge enterprise-level applications. First of all because of its safety, relative ease of maintaining the code, and good scalability. So most of the large-scale projects for banks, trading, the automotive field, etc. are written in Java. Also, it is used for backend development, scientific programming (especially in Big Data), Internet of Things, and Android mobile apps.

Game-Changer: Node.js

Remember how I mentioned “classic” JavaScript earlier? I did it not in vain but with a hint that there is also “non-classical” JavaScript. Its name is Node.js (or server-side JavaScript). It can be used to write backend sites and backend applications. You probably already realized that together with Node.js, JavaScript entered the original territory of Java (not only Java, of course, but a very large share of this market belongs to Java).

However, when we talk about Java, we are talking not only about the language but about the Java virtual machine, as well as the entire ecosystem and infrastructure built around this machine. At a minimum, they can be compared on this basis. As a result, in both cases, we have a runtime environment. In the case of Java, this is a virtual machine. In the case of Node.js, it is the V8 engine that is present on most OSs like Windows, Linux, MacOS, and lesser-known ones.

When the JS code gets into v8, it is compiled into bytecode that is used in the virtual machine just in time. The code in JS is executed faster and faster.

Bytecode is a high-level intermediate language. Therefore, in the Java virtual machine, they write not only in Java but also in Scala and Kotlin.

In fact, comparing Node.js and Java is a topic for a separate detailed article. I’ll put it this way: Java is the leader now, but Node.js is pretty stable. There are pluses in the fact that backend and frontend developers write code using the same language, and it will work in more or less the same way on different OSs due to the fact that there is a runtime environment.

Conclusion

In this article, we discussed the differences between Java and JavaScript. They are not relatives. They are different programming languages.

Rivals? Yes, partly, thanks to Node.js. However, Java’s position in the development of large backend applications remains much more confident, and if you are interested in this particular area, you should take a closer look at Java.

Friends? An even more solid yes! After all, the main application of JavaScript is the frontend. Therefore, teams working on large projects usually have both a server part in Java and a client chat in JavaScript. Moreover, JavaScript is very popular as a second language and almost every Java developer knows JavaScript at least a little bit.

First published at BetterProgramming.

Top comments (0)