paint-brush
Java / JavaScript: Differences Beyond the Nameby@moerayo
712 reads
712 reads

Java / JavaScript: Differences Beyond the Name

by Moronfolu OlufunkeAugust 29th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Java and JavaScript are different programming languages. Java is an Object-Oriented Programming (OOP) language released in 1995 by Suns Microsystems. JavaScript is a scripting language built within ten days by Brendan Eich after he joined Netscape. Java was initially developed for building interactive and intelligent electronic devices, we can often see its usage in various applications, from computing to backend systems. Java is a compiled and interpreted language not designed for any specific application or platform. Javascript is known to be both synchronous and single-threaded as the browser interprets and runs the codes.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Java / JavaScript: Differences Beyond the Name
Moronfolu Olufunke HackerNoon profile picture


As a beginner JavaScript developer, one of the things you must have heard more times than you can count is the massive disconnect between Java and JavaScript. I can't remember how many disclaimers I heard at the beginning of the numerous JavaScript videos I watched - at every turn and corner, the disclaimer was just impossible to miss.


You most likely also heard that naming JavaScript was just a marketing strategy to help JavaScript ride on the popularity of the Java programming language, which was doing considerably well at the time. While this "story" was at every beginning and end of most tutorial videos, many of these videos and tutorials stopped at just telling you how they differ in their names.


But beyond the name, how else do these two languages differ from each other? Is there even any difference between the two programming languages? In this article, we will be examining other differences between the two languages.

Java Story

Java is an Object-Oriented Programming (OOP) language released in 1995 by Suns Microsystems before its acquisition by Oracle corporation. While Java was initially developed for building interactive and intelligent electronic devices, we can often see its usage in various applications, from computing to backend systems.

JavaScript Story

JavaScript is a scripting language built within ten days by Brendan Eich after he joined Netscape. Before the introduction of JavaScript in 1995, webpages were primarily static, but the introduction of Javascript as the browser's language birthed the beginning of interactive web pages.

Key Differences

Java is to JavaScript as Man is to Mango - this is to say, despite the presence of "Java" in "JavaScript", there is no relationship between them. Now, let’s explore some of their differences.

Java Execution

Java is a compiled and interpreted language not designed for any specific application or platform, thus giving it the name "WORA" (Write Once Run Anywhere). After storing a Java source code in a file with a .java extension - index.java, the source code is first compiled into bytecode with a .class extension - index.class. This compiled class file can then be executed on the current machine or transferred to another with the Java Virtual Machine (JVM). The JVM is responsible for translating the bytecode into an appropriate executable machine code.

Java Execution

JavaScript Execution

JavaScript is known to be both synchronous and single-threaded as the browser interprets and runs the codes as it goes over each line of code. This execution does not need a particular compilation form; it can happen on any platform with a JavaScript engine. Before this execution starts, the program's functions and variables are hoisted to the top of the program.

Printing to Console

One of the first things taught in all programming languages is writing the infamous “Hello World”. Printing “Hello World” to the console in Java would take this form:


public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, World!"); 
  }
}


The System.out.println() takes in an argument that gets printed to the console.

Performing this same function in JavaScript will take a different form as shown below. Suppose we have a file named index.html:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script>
    console.log("Hello, World");
  </script>
</body>
</html>


In the JavaScript program, the console.log() prints the argument into the browser console. The two programs show the standalone feature of Java at work, while the Javascript program had to be wrapped in a <script></script> tag with an HTML file.

Type System

Type systems govern how we assign values to variables, functions, or computer programs. These types are sometimes significant criteria when considering a new language to pick up.

Java is a strongly typed language that enforces type declaration on all computer commands. You'd declare a variable in Java like so:


boolean isEnabled = true;


Boolean is a data type that enforces the value that can be stored in the variable isEnabled. Booleans have two possible true or false values; as such, with the boolean declaration, it will not accept the storage of any other value outside the accepted two values. Computer programs sometimes represent the true and false Boolean values with 1 and 0 respectively. But what will happen if we decide to store a 1 value to represent true in a boolean data type in Java? Like so:


boolean isEnabled = 1;


Java throws this error:


Type mismatch: cannot convert from int to boolean


The error above shows that 1 is considered as belonging to the data type int and cannot be stored in a boolean data type - which is expecting either true or false.

However, with the introduction of Java 10, we can also declare variables using the var keyword. Java type system can infer the data type based on the value assigned to the variable.


var isEnabled = true;


As such, Java automatically infers that the isEnabled variable is a boolean data type. Due to this inference, it is imperative always to assign a value to the declared variable.


var isEnabled;


Running the code below would cause Java to throw the error below, warning us about adding a value to the variable.


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Cannot use 'var' on variable without initializer


JavaScript, on the other hand, is known to be loosely typed or strongly dynamically typed. As a result, we are not in any way forced to define the data type during variable declaration.

Declaring a variable like the below in JavaScript will work quite well:


let isEnabled = true;


let in JavaScript allows for variable declarations and is not restricted to a data type like the Java programming language. Invariably, this means any value can be stored in the isEnabled variable

Let’s consider changing our true value to 1. The JavaScript engine interprets it without throwing errors, as in the case of Java.


let isEnabled = 1;


From the two Java and JavaScript typing systems, we see a stark difference in how data types work and variable declaration. Zack Grossbart gave a nice intro to type systems in this article.

Asynchronous Execution

Java as a multi-threaded language means that multiple programs can be executed simultaneously (concurrently). Each of these threads can run different programs, and they do not need separate memories to be allocated to each program, thus helping to save time and memory.


On the other hand, JavaScript is a single-threaded language that allows only one program to be executed at any time. The tasks to be executed are put in a queue as the current program finishes running.

Conclusion

This article does not point out the better of the two programming languages, nor is it to dissuade you from using one over the other. This article points out the vast difference between Java and JavaScript programming languages.

As said before, Java is to JavaScript as Man is to Mango. These two languages are so contrasting that we should never try to use either interchangeably.