Sitemap

JavaScript to Java

3 min readJun 1, 2020

--

I have recently decided to teach myself Java. Coming from a JavaScript background, I figured it can’t be too different. Just have to lose the script, right? All joking aside there are a lot of similarities between the two, but there are major differences as well.

For starters, Java was created to run on any machine. When its script compiles, the outcome can be read by Windows, Linux, and OS based systems. JavaScript does not have support for Windows systems and therefore has to use a Linux based virtual machine in order to read the code.

Java is based on Object Oriented Programming, which is available in JavaScript, but with Java it is the only way to run code. This means every command run in a Java program will be accessed through an object. JavaScript allows functional programming, which allows functions to be created and called anywhere in our code script.

Declaring variables is similar, however, instead of using var, let or const, we need to tell Java what type of datatype we are declaring. Below is how we declare a string datatype in JavaScript as compared to Java. Notice that Java has a String and a char datatype. We can think of this as “char” being a simple datatype and String is an array of chars.

First the JavaScript string:

let firstName = "James";

And the Java equivalents:

char letter = 'm';String firstName = "Matthew";

Datatypes are pretty similar between the two. We can compare JavaScript’s number to Java’s int. There are actually a few number datatypes in Java and can be used in certain situations to save memory, but int is its most basic form of a number. Java also has number datatypes for non-whole numbers and numbers within a certain limit, but for simplicity sake we won’t get into them. For more information you can look here.

int number = 283

With complex datatypes it gets a little different. When we look into arrays between the two classes, we can see that JavaScript’s arrays are their own datatype while Java’s array is more of a modified primitive type. Again, we must declare what type of array we are declaring followed by an array literal to tell the program that the following data must be stored as an array. Or Java arrays do share a similarity with our JavaScript arrays in that they are zero-indexed and can be accessed the same way as if we would access a JavaScript index.

Get matthritz’s stories in your inbox

Join Medium for free to get updates from this writer.

JavaScript Array:

let bestPlayersArray = ["Brees", "Thomas", "Jordan"];console.log(bestPlayersArray[0]);   //output: "Brees"

Java Array:

String[] bestPlayers = {"Brees", "Thomas", "Jordan"};int[] bestNumbers = {9, 13, 94};System.out.println(bestPlayers[0]); //output: "Brees"
System.out.println(bestNumbers[0]); //output: "Brees"

The similarities are close enough that we can look at Java code and be able to tell what most of it is doing if we just refer back to our JavaScript knowledge. Look out for my next blog where I will dive deeper into Java operators and manipulating datatypes.

--

--