javaScriptIsTheSameAsJava = false; Part 1: Variable Declaration and Mathematic Operations

Steven Kandow
The Startup
Published in
5 min readOct 7, 2020
Image taken from design by Camilo Garcia — Pixabay

As a novice programmer, one of the first big mistakes in nomenclature that I made when starting to learn JavaScript was to tell a friend, “I started learning Java last week.” He raised an eyebrow in disbelief, asking to clarify if I meant JavaScript instead. When I mentioned that I thought they were the same thing, he gave me some harsh lessons regarding the differences between the two.

Both Java and JavaScript are extremely common languages in the coding world. Many companies will appreciate an understanding of one if not both languages, yet the way they are structured is quite different. This blog series will take a look at some of the differences between the two structurally and operationally speaking.

As someone who learned JavaScript first and Java second, this blog is being written from the standpoint of looking at the similarities and differences between the two languages from that lens.

Hello World

We’ll first begin with every programmer’s first message in any new language: Hello world!

Let’s take a look at what we would need to do to print “Hello World” to the console in JavaScript:

helloWorld.js
--------------------
console.log(“Hello world!”);//=> “Hello world!”

Now let’s take a look at the exact same task in Java:

HelloWorld.java
--------------------
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello world!”);
}
}
//=> “Hello world!”

What took one line to accomplish in JavaScript took five in Java. Why is this?

Our above example already highlights one significant difference between JavaScript and Java: Actions in JavaScript can be declared without the need for creating a class. Any task you wish to accomplish in Java, however, must be declared within a class. We’ll explore more of how this functions in a later blog post.

Variable Declaration

Let’s now look at how variables are declared in JavaScript and Java:

We’ll do this in JavaScript first:

greetingInfo.js
--------------------
const greeting = “Welcome to this blog series!”;
let favoriteLetter = ‘K’;
const javascriptIsTheSameAsJava = false;
var blogSeriesNumber = 1;
let averageNumberOfReadersADay = 25.3;

Now let’s do the same in Java:

GreetingInfo.java
--------------------
public class Greeting {
public static void main(String[] args) {
String greeting = “Welcome to this blog series!”;
char favoriteLetter = “K”;
boolean javascriptIsTheSameAsJava = false;
int blogSeriesNumber = 1;
double averageNumberOfReadersADay = 25.3;
}
}

With both JavaScript and Java, variable names are case sensitive, and both utilize camelCasing as a standard for naming variables with multiple words.

Both JavaScript and Java require three components for a variable to be declared and assigned a value, but the first of these components differ.

JavaScript’s three variable components:

1) A signal reflecting the variables mutability.

const means that the variable’s value will not be reassigned.

let means that the variable’s value may be reassigned at some point in the code block in which it’s declared.

var, which used to be the only option for variable declaration prior to ES6, does not make mutability clear. It’s best to avoid this signal if it all possible.

2) The name of the variable

3) The value assigned to the variable

Java’s three variable components:

1) The data type of the variable.

String — Strings are sequences of characters. In Java, Strings are considered objects rather than types of primitive data.

char — Single characters. These can be uppercase letters, lowercase letters, spaces, and punctuation marks. In Java, char variables must be declared with single quotes as in the example here:

char exampleFirstLetter = ‘E’;

boolean — true, false

int — all whole numbers between the range of (-2)31 and (2)31–1

double — There are two types of numbers that fall under the double datatype:

a) Numbers with decimals

b) Numbers higher than (2)^31–1 or lower than (-2)^31

In terms of number size, the range of the Java double data type stretches from 4.94065645841246544e-324d to 1.79769313486231570e+308d for both positive and negative numbers.

Java has other data types used to represent numbers, including short, long, and float. For now, for precision purposes, in this series, we’ll primarily explore the use of int and double.

One other item regarding variable declaration in Java deals with the incompatibility of data types. This will raise an error:

int numberDeclaration = “0”;

Because the datatype presented in the declaration does not match the assigned value given to the variable.

Arithmetic operations

We’ll conclude with a look at JavaScript and Java’s mathematic operations. Most of these work in the same way. For the sake of these examples, let’s declare two variables in JavaScript and Java and take a look at how they can be manipulated mathematically.

In JavaScript:

mathVariable.js
--------------------
const x = 21;
const y = 10;

In Java:

MathVariable.java
--------------------
public class MathVariable {
public static void main(String[] args) {
int x = 21;
int y = 10;
}
}

Now let’s set some variables based on mathematic operations and see what happens.

First, in JavaScript:

mathVariable.js
--------------------
const addNums = x + y;
const subtractNums = x — y;
const multiplyNums = x * y;
const divideNums = x / y;
const moduloNums = x % y;
console.log(addNums);
console.log(subtractNums);
console.log(multiplyNums);
console.log(divideNums);
console.log(moduloNums);
//=> 31
//=> 11
//=> 210
//=> 2.1
//=> 1

This is fairly straight-forward. JavaScript calculates the basic math operations with exact answers. Recall that the fifth operation, the modulo (%), returns the remainder when the first number is divided by the second.

Now let’s look at these five operations in Java:

MathVariable.java
--------------------
int addNums = x + y;
int subtractNums = x — y;
int multiplyNums = x * y;
int divideNums = x / y;
int moduloNums = x % y;
System.out.println(addNums);
System.out.println(subtractNums);
System.out.println(multiplyNums);
System.out.println(divideNums);
System.out.println(moduloNums);
//=> 31
//=> 11
//=> 210
//=> 2
//=> 1

Four of these five answers match their JavaScript counterpart. Notice that with the division operation, Java returns a whole number without decimals. Is this because we declared divideNums as an integer?

Let’s try declaring divideNums as a double datatype:

double divideNums = x / y;System.out.println(divideNums);//=> 2.0

But we know from the calculation above in JavaScript that this answer SHOULD be 2.1.

When integers are divided in Java, the return value removes the decimal remainders and leaves an integer (or its decimal .0 equivalent).

In order to receive the actual decimal answer with the division operator, the original datatype of x and/or y would have to be set to a datatype of double. In our specific example, this would also require each of the operational variables to be altered to provide answers with decimal potential:

MathVariable.java
--------------------
public class MathVariable {
public static void main(String[] args) {
int x = 21;
double y = 10;

double addNums = x + y;
double subtractNums = x — y;
double multiplyNums = x * y;
double divideNums = x / y;
double moduloNums = x % y;
System.out.println(addNums);
System.out.println(subtractNums);
System.out.println(multiplyNums);
System.out.println(divideNums);
System.out.println(moduloNums);
}
}
//=> 31.0
//=> 11.0
//=> 210.0
//=> 2.1
//=> 1.0

In the next entry in our series, we’ll take a look at value comparisons and conditional statements.

--

--