DEV Community

Cover image for Java VS JavaScript
Valentiina VP
Valentiina VP

Posted on • Updated on

Java VS JavaScript

It's not a surprise that when we begin to learn programming we confused these two languages, since they both have similarities in their names. But today we are going to understand what differentiates them and why we must learn not to confuse them. Let's start.

Java
JavaScript
It's need a virtual machine and a development kit to be able to work. It's only need a text editor to work.
It's form of execution, as we saw in my previous post, places it in a language both compiled and interpreted, through the use of its JVM. It's an interpreted language, as we also saw in my previous post.
In original nature it was mainly created to be used on the back-end side. In original nature it was mainly created to be used on the front-end side.
It's methodology is based on classes within object-oriented programming. It's methodology is based on prototypes within object-oriented programming.
It's strongly typed. It's weakly typed.
It's debugged in two phases. It's debugged in one phases.

These are some of the main differences, there may be others in depth in both languages, but we will address the ones I have mentioned. If you can contribute more differences, feel free to add it and open a discussion, so we all learn much more.

Taking these differences in mind, will they have any characteristics in common? Yes, few, but there are.

  • Both languages ​​are C-based
  • Both are cross-platform programming languages
  • Both are languages ​​that, due to their time on stage and wide community, are classified as robust languages.

JAVA

Oracle is the private company in charge of this language. Today it's in JAVA SE 14 version.

Java is made up of different platforms:

  1. Java SE (Standard Edition) is used to build desktop applications, console applications with Swing and JavaFX.

  2. Java EE (Enterprise Edition) is used to develop web applications. It comes with many technologies.

  3. Java ME (Micro Edition) is used to develop mobile applications.

As I also mentioned, in order to run a program in JAVA, you must first have installed the Java Development Kit, but what is this?.

Java Development Kit (JDK)

Alt Text
In common terms, the JDK is the box that contains all the tools necessary develop and execute code written in this language.

The main tool that we can mention is the javac compiler, it allows compiling the .java source file and transforms it into a bytecode with .class extension. Another important tool to mention within the JDK is javadoc, which allows you to generate the documentation for the code. The JDK also includes the JRE, which is the Java runtime environment, that is, the place where the code is executed.

JDK vs JRE

As its name implies, JDK is a tool used for development, so it is generally only used by the Java developer community, but not the end user. By contrast, the JRE is the tool that the end user needs to run their program or application.

But as a curious thing, the JDK cannot be installed without the JRE, however, the JRE can be installed independently. This makes sense, since as programmers we need to test our code, therefore we need their execution environment to be able to do it, while end users only need to execute it.

Java execution

Alt Text

As we saw in my previous post (Classification of computer languages (III)), Java is a compiled language, but through its VM you can choose whether to interpret the bytecode or compile it. As we can visualize it in the previous image.

Classes-based (OOP)

Alt Text
It's based on creating a mold structure called class where the fields and methods that our objects will have are specified. Every time we need an object we create an instance (or copy of the object) using the class as a template.

Strongly Typed

Alt Text
The concept refers to the strict use of data types when declaring variables or using parameters. Personally, it is a feature of languages ​​like Java that are very useful to me, especially at the time of data entry by the user. Allows better control of input data.

It is also a very important feature when programming, since, knowing the types of data to be used from the beginning, you will have greater control when allocating the spaces in the memory of the data. If you are an intermediate programmer, you will know that an INT is not the same as a BYTE and the effects this has on the memory, weight and execution time of the code. Later I will talk about this in another post.

Java Debugging

At the time of executing our java code, the debugging is done in two steps:

  1. At compile time: When the javac compiler tries to generate the bytecode, it first parses the syntax, and if it is not correct, it warns of this error and the code doesn't execute.

  2. At runtime: If the first step does not generate errors, the code is executed, but it may happen that the user enters an unexpected data type, or tries to access an index that does not exist in an array, or the basic errors in Java mathematical operations such as divisions by zero or saving strings where they should be numbers. The program runs, but at the time of finding one of these errors, it will hang and not work properly.

JAVASCRIPT

Javascript is also registered under the Oracle company, however, 1997 adopted the ECMA non-profit organization standard, which later specified it as ECMAScript.

This is a very versatile language, since, although it is naturally based on prototypes, as I mentioned earlier, it easily adapts to other methodologies.

JavaScript Execution

Alt Text
JavaScript is an interpreted language, although other developers think that it can also be compiled. If you are interested in reading about this thought, you can access it here: You Dont Know JS.

The first thing that happens inside the engine is that our code is parsed by a parser, which basically reads our code line by line and check if the syntax of the code we gave you it's correct. This happens because the parser knows the syntactic rules of Javascript so that the code is correct and valid. If it encounters an error, it will stop running and It will throw that error.

If our code is correct, the parser generates a structure known as AST or Abstract SyntaxTree. The syntax is "abstract" in the sense that it does not represent all the details that appear in the actual syntax, but only the structural or content-related details. This structure is translated into machine code and it is at this moment that the execution of our program actually occurs.

When we talk about JavaScript code execution, we need to keep in mind the execution stack and scope. On these two concepts I will delve into my next post Javascript behind scenes.

Prototype-based (OOP)

Alt Text
In this methodology there are no classes, only objects. To create an object with the same structure as another, the term object cloning is used. This cloning is done through a prototypical object, which is used as a template to obtain more objects equal to it.

Weakly Typed

Alt Text
For many programmers, this is an advantage, as it constantly eliminates thinking about what kind of data to use and focusing on logic. Another possible advantage is being able to change the type of the variable on the fly, such as assigning a string to an int.

In Javascript curious things happen that in a strongly typed language would not happen, such as when comparing strings and numbers. Let's see an example in the browser console:

Alt Text
Admittedly, these problems are easily solved using strict comparators (===), but it might confuse those that come from a strongly typed language.

As we can see, these languages ​​cannot be confused, since their independent characteristics allow us to see that there is no reason for it. Remember, if you want to contribute more to this article, feel free to do so!

See you soon!

Top comments (0)