DEV Community

Cover image for Java vs. JavaScript
Stephen Nelson
Stephen Nelson

Posted on • Edited on

Java vs. JavaScript

When programmers are entering the field or starting their careers from square one, it's important to make that first step which is deciding, which language do I start with? For me, it was JavaScript and since I started my coding journey, I've repeatedly observed Java documentation, in passing, and wondered if the two were actually related. So I decided to look into the comparison between the two to satisfy my curiosity.

**Note:** This blog will continue assuming you have at least a basic foundation in JavaScipt. If not, then I highly suggest developing the required knowledge of JavaScript before proceeding.

Before I dive into the similarities and differences of each language's basic implementations, I'd like to begin by comparing the general overview of each language.

Overview

Java and JavaScript are both high-level and General-Purpose-Languages, meaning they can run on multiple platforms/devices and their human-like syntax provides more abstraction from machine code. This allows programmers to focus on the fundamentals of coding with minimal confusion. Where both languages begin to diverge from each other is that JavaScript is considered object-oriented programming and Java is known as class-based which is a style of OOP. Class-based simply means inheritance occurs by defining classes in Java as opposed to the prototype object inheritance seen in JavaScript.

Their human-like syntax makes both languages easier to read than low-level programming languages. However, Java's syntax is more similar to that of the C family (C and C++), and JavaScript is more related to Python (another high-level GPL). Also, Java code is complied before runtime and JavaScript code is interpreted at runtime and both cases have an effect on which end of development they work more efficiently in.

Before you make the decision to choose a language, it's important to look into them and decide which area of coding would best suit you. If you are considering one of these two languages, I hope this blog helps provide some inspiration to your decision. Let's break down the similarities and differences of Java and JavaScript.


Data Types

Some of the most common similarities between Java and JavaScript stem from their variables and how those variables are declared. Each language has their own form of data types that fall into two categories: simple and complex.

Simple data types such as strings, numbers, booleans, null, and undefined are the more common data types that programmers use on a daily basis. Below is a chart comparing how each language "labels" these variables through declaration and naming conventions.

int number = 5; //Java
var number = 5: //JavaScript

char myLetter = 'P'; //Java
var myLetter = 'P'; //JavaScript

boolean myBool = true; //Java
var myBool = true; //JavaScript

String myText = "This is my blog!"; //Java
var myText = "This is still my blog!"; //JavaScript

var blog = null; //JavaScript
//Why can't simple Java data types have null values?

int numTwo; // => undefined Java variable
var numTwo; // => undefined JavaScript variable
Enter fullscreen mode Exit fullscreen mode

As you can see, both languages have similar implementations of their respective simple data types. There are some important differences worth investigating, starting with their variable declaration.

JavaScript uses variable declaration keywords var, let, and const. For the sake of this blog, don't worry about the differences between those three keywords, just understand that JavaScript uses these to declare variables for the interpreter to read. With JavaScript being a dynamically typed, each variable declared with var and let can be re-assigned to any other data type the programmer desires. Java, on the contrary, is statically typed, therefore the programmer is required to intentionally state which type of data the interpreter is expecting. In other words, the Java interpreter expects an integer when the int data type is stated before the variable name, acting in a similar fashion to JavaScript's variable declaration.

That being said, since null is a reference type of data that allows JavaScript to assign null to a simple data type but Java's simple data types are not reference types. If a reference type in Java is desired, then complex data types are the way to go. I want to point out that in the above code block, String myText is a reference type and is considered a non-primitive data type in Java, unlike strings in JavaScript.

Let's look at some examples of these complex data types and compare their implementation.

var myArray = ['apples', 24, true]; //JavaScript
int[] numbers = [1, 2, 3, 4]; //Java

//JavaScript
var myObject = {
  fruit: 'apple',
  integer: 24,
  myBool: true
};

//Java
//class
public class User {
   String name = "Edward";
   int age = 23;
   String email = "randomemail@gmail.com"
   //new object literal
   public static void main(String[] args) {
     User edward = new User();

   }
}

Enter fullscreen mode Exit fullscreen mode

Much like JavaScript arrays, Java arrays exist as 0-indexed lists meaning programmers can also use bracket notation to find specified values in the array. For example in JavaScript myArray[1] is referring to the integer 24 on index 1. For the Java array numbers, numbers[3] references the integer value 4. The main difference between both arrays is that due to JavaScript's dynamic typing, multiple data types can be stored in an array, as seen in the above block. Java, however, is expecting the array to contain certain data types as denoted by the data type declaration before the array name. In the example above, integers are to be expected.

JavaScript object instantiation is a simple one-step setup starting with the declaration keyword (var), the name (myObject), and followed by the curly braces that contain the object's properties. In Java, object creation occurs in two steps.

  1. Create a class (first letter is uppercased) with instance variables
  2. Using the new keyword to define the object literal that inherits the instance variables

If you are familiar with JavaScript objects, then the example above should be a no-brainer. Much like JavaScript dot notation, Java class variables can be accessed the same way. In the example above, edward.age would return 23! However, bracket notation is not used in Java class access due to its static nature.


Array Iteration

As I've discussed how to access the data in both arrays, we can pinpoint their locations if we know where to find the data. But, what if we need to find a variable in an array or an object? Here's how you would do that on a basic level

//JavaScript
/for loop
for (var i = 0; i < 5; i++) {
  console.log(i)
}

//forEach
myNums = [1, 2, 3, 4];
myNums.forEach(function(num) {
  console.log(num);
});

//Java
//Java for loop
for (int i = 1; i < 5; i++) {
  System.out.printIn(i + 1)
}

//for each
int[] numbers = [1, 2, 3, 4];
for (int num : numbers) {
  System.out.printIn(num);
};
Enter fullscreen mode Exit fullscreen mode

This blog won't go into Java class iteration as most of the time, the class will need to be converted to an array and can get messy for beginners. Respectively, this blog will not cover JavaScript object iteration assuming you've read the introduction and have a basic understanding of JavaScript. It's still imported to be exposed to the Java class syntax as you get started and how they exist compared to JavaScript objects.

Array iterations between Java and JavaScript appear to share some relations with a few tweaks in syntax. The counter variable in their respective for loops behave the same way, the only difference being the Java for loop is expecting the int data type for the counter variable while JavaScript is less strict using the keyword var. While JavaScript has its own forEach() array method, Java has the facility to reciprocate that method but in a differing implementation that loosely follows its for loop syntax. With their own unique structures, both language's array iterations share the same objective, which is to have maintain access to an array's index list.


Advantages of Each

Now that we've looked into some of the different implementations, syntax, and basic understanding, let's dive into the advantages of each language and how they are used in the programming field.

Image description

Java

If you're more interested in back-end, machine production, or server-side programming, then check out Java. Java is well-suited for enterprise/industrial applications because of how the code is compiled and its ability to independently run while disregarding the need for a browser. Android app development typically hosts Java as one of the default programming languages due to the depth of interactions and technical specifications required across multiple devices. Speaking of devices, Java as a back-end, server-side program standard is one of the most common use cases in the development of smart devices and connecting them all across the internet.


Image description

JavaScript

As stated before, if you've made it this far it's because you already have a basic understanding of JavaScript and know that it has use cases elsewhere. In case you forgot, here's a refresher on those cases.

JavaScript is widely known for building dynamic/interactive web pages, hinting at its strengths lying in the front-end of web development and play nicely with other languages such as HTML and CSS. Next, JavaScript has access to protocols (aka frameworks) that allow programmers to build out mobile apps and accomplish more on the back-end. However, keep in mind JavaScript is reliant on these frameworks to accomplish that. The key word for JavaScript here is "interactivity" while Java is an efficient source of running code on the back-end, you won't see it much on the front-end.


Conclusion

In conclusion, the question of which language you would choose simply reduces to one main question to consider. Back-end or front-end? If you are looking to build interactive website/apps/maybe the occasional browser-based game, then JavaScript might be the path to take. Java, if you're looking into smart device/industrial development, seems to be an appropriate launching pad for aspiring back-end developers. Which would you choose?

References

Top comments (0)