Java vs. JavaScript
Tags: javascript, java, beginners, programming
Published: 2023-10-16 13:05:48

<p>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.</p>
<p>**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.</p>

<p>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.</p>

##Overview

<p>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.</p>

<p>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.</p>

<p>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.</p>
---

##Data Types

<p>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.</p> 
<p>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.</p>

```
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
```
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>Let's look at some examples of these complex data types and compare their implementation.</p>

```
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();

   }
}

```
<p>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.</p>

<p>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.</p>

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

<p>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.</p>
---

##Array Iteration
<p>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</p>

```
//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);
};
```
<p>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.</p> 
<p>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.</p>

---

## Advantages of Each
<p>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.</p>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/auett12ylri03y4xnfwc.png)



###Java
<p>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.</p>

---

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7wppm8jvfdemz1bhlnm.png)



### JavaScript
<p>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.</p>

<p>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.</p>

---

##Conclusion
<p>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?</p>

###References

- https://www.lighthouselabs.ca/en/blog/java-vs-javascript
- https://en.wikipedia.org/wiki/Java_(programming_language)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript
- https://www.w3schools.com/java/java_data_types.asp