Diving into Java (with strong JavaScript and Python background) Part 2(Data Types)

Jon Cundiff
13 min readFeb 23, 2022

--

Let’s continue on the next few steps on my journey to learn and become proficient in Java. To recap, my plan of action is as follows:

  1. Load most common environment
  2. Research naming conventions
  3. √ Hello World app to confirm environment set up properly
  4. Review and practice basic data types and available methods in Java
  5. Review and practice equivalents to arrays/lists and objects/dictionaries in Java
  6. Algorithm practice
  7. Sample projects with the above knowledge
  8. Learn Spring framework
  9. Sample projects with Spring with REST API and WebSockets or Socket.IO

In my previous article, I had installed the Java Development Kit and JetBrains IntelliJ on my Mac, researched naming conventions and how similar it is to JavaScript, built a simple Hello World application to verify everything is installed properly, and concluded with observations about the differences in Java from JavaScript. In this article, I will focus on primitive data types, from numbers, to strings. Along with the data types, I will explore methods available to those types, like converting a string to UPPERCASE, and parsing from one data type to another. Pretty standard features that should be in all programming languages. I will also explore conditionals while exploring data types. As I did with the Hello World section, I will relate the code back to JavaScript and Python.

Primitive Data Types

Before jumping into data type comparisons, let’s take a look into variable declarations and default values.

Both JavaScript and Java use keywords to indicate a variable is being declared, while Python does not (just <variable name> = <value>). Python and JavaScript data types are not explicitly defined in the declaration. It is dynamically assigned based on the current value of the variable. Java is statically-typed, meaning once a variable becomes one type, say a Boolean, it can only be a Boolean for the remainder of a program lifecycle.

JavaScript can use the let and var keywords to declare a variable that can change values throughout a program’s lifecycle, while const is used to declare constants that do not change. Java uses a data type (like int)or a class name before the variable name to indicate a variable is getting declared. Java additionally allows the use of the var keyword to infer the type of a variable based on the initial value. This differs from JavaScript’s var, which allows variables to be reassigned to a new type — once the type is inferred the variable cannot be reassigned to a new type.

Let’s look at how variables without an initial value is handled. Python will throw an error if there is no assignment operator — a value must be given (even if it’s None type initially). JavaScript will give a variable a value of undefined. Java will provide an initial value, depending on the data type (though it is considered bad programming practice to depend on this initial value).

Numbers

JavaScript uses one primitive type store a majority of use cases of numbers, called number. Integer? It’s of number type. Decimal? Number as well. On really really large integers (over 9 quadrillion), JavaScript does have the BigInt type that can be used via a constructor (not dynamically assigned like other data types). Python does differentiate between integers and decimals via the int and float data types.

Java has 6 data types to handle numbers: 4 for whole number data types (byte, short, int, and long), and 2 for decimal types (float and double). The differences between all the variations for the number types is the amount of memory each takes up and with that, the range of accepted numbers for that data type or precision of a decimal. When assigning a variable using the var keyword, int is inferred for integers, and double is inferred for decimals. Types byte, short, long, and float must be explicitly declared, either by using the respective keywords, or by appending “L” (for type long) or “f” (for type float). For this article, I will focus on using int and float.

What kind of methods can a number perform? In JavaScript, nearly everything is an object (On MDN Web Docs primitives [except null and undefined]are contained within a “Primitive Wrapper Object”), so that means most things in JavaScript has potential to perform an action. Some methods (or a special implementation of a method) that are attached to every object are valueOf and toString methods (Even strings have a toString method!). For numbers, the toString method is fun:

Given a base “normal” number, JavaScript’s toString method allows conversion to different number systems (basically you count to a number other than 10 to go up to the next digit). In the example above, I converted to two common systems used to describe computing, hexadecimal (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F) and binary (0, 1).

Python primitive types typically do not provide any functionality in and of itself. Instead it provides different methods that accepts the primitive type to transform as needed. In Java, the instance of the type does not provide the functionality to transform — the base class contains the method to transform the instance.

Notice that Python represents hexadecimal by starting with 0x and binary by starting with 0b? Python, JavaScript, and Java each allow you to define an integer in any of these formats. When you access the variable later, it will be displayed in decimal (meaning base 10 counting or “normal” numbers).

That’s probably the extent of fun methods concerning number types, let’s move explore the fun parts and purpose of this data type: math and indices!

The operators that are the same among Python, JavaScript, and Java are + (addition), -(subtraction), * (multiplication), / (division), and % (modulus/get remainder). What’s missing in Java’s list is an exponential operator (** in both Python and JavaScript). For actually getting x to some integer power, it’s not too big a deal to code manually using a for loop. So let’s jump in:

Notice in Python and JavaScript that the division operator automatically converts my result to a number with a decimal? This is a feature of those languages’ dynamic typing. In Java, once a type is set, it is set. When using the division operator in Java, the return result is based on what you use as the inputs.

In the first division, when provided with two integers, the result type is also an integer, with decimals dropped (like using JavaScript’s Math.floor without needing an extra function!).As seen in next 3 divisions, as long as one of the input types is a double(either by using a double typed variable or by casting an int as a double by prefixing it with double wrapped in parenthesis), the result will be the expected result as a double. If you try to cast the result of dividing two integers into a double, Java will first floor the result then convert the result into a double.

In addition to mathematical operations, integers are used for loops of a set range and conditionals, so let’s segue into control flow before the rest of the data types.

Control Flow

What makes a program interactive versus being run-and-done, is the ability execute different code based on the values and amount of data it receives. It can run different code via if/else statements, while loops, for loops, and switches. An integer (and later on length of an array), can also be used to determine just how many times a loop is run. Let’s start with if/else statements:

How JavaScript and Java handle if statements are exactly the same — if keyword condition in which to run a statement in parenthesis, and statements to execute within curly brackets. Any number of else if followed by condition and statements can follow the initial statements. The last segment (if used), should all prior conditions fail is else. In Python, the parenthesis around the condition is optional and used for grouping purposes within a complex conditional, and it uses elif instead of else if. Additionally, in JavaScript and Java, whenever an if block is only one statement, the curly braces may be omitted.

A short cut using conditionals present in many languages is a ternary operator, where you can assign a value one of two values based on a condition:

JavaScript and Java follow the same format for ternary operators: <test condition> ? <value if true> : <value if false>. Python is a different format: <value if true> if <test condition> else <value if false>. It is technically possible to pass another ternary operator as values within ternary operators, but that can become extremely difficult to follow and maintain very fast:

Many languages also include a switch feature, which functions very much like a chain of if/else statements where you’re checking if X variable is equal to specific Y values, but can look cleaner to read. Python does not have this feature, while JavaScript and Java handle the base functionality of switch the same:

Notice how the cases accept conditionals that are exact values? In a switch statement, a variable or calculation is tested if it matches any of the case values, then determines which block of code to execute. If the value is not found, the default code block at the end is ran. Java also has two shortcut syntaxes that JavaScript does not have. In the example above, Java was able to combine cases together by separating accepted values with commas. If this was attempted in JavaScript, only the last value would be evaluated (at least in my sample code being tested on my machine). Java also has a special switch that’s even more concise:

Loops

One of the most powerful tools in programming is the ability to repeat lines of code efficiently via loops. Loops come in 2 primary forms — while loops (and its variant do-while) and for loops. while and do-while loops run as long as the condition with an external dependency is true. for loops run a set number of times based on an internal dependency that is defined when the loop is set up. Let’s dive into an example of a while loop first as this appears first in the Control Flow Statements section of the Java Documentation:

In each of the above examples, the program will print each number from 0 to 19 on the screen. Once x becomes 20, the while loop exits, therefore 20 does not get printed. The pattern for many while loops is define a variable (or set of variables) first, then start the while loop (in this example, as long as x is less than 20, run these statements). The while loop is used for main program flow and for cases of not advancing to the next bit of code until the user gives you a valid answer (example: when presented with a list with 4 options, making sure the user cannot enter to do option 5).

The possible danger while programming a while loop is if the code block is not modifying the external variable that allows the test condition to become false, creating an infinite loop. In addition to making sure the external variable changes to provide an exit condition, the break keyword allows a programmer to exit out of a loop. The continue keyword also allows one to start a new cycle of a loop without breaking out. This technique can be used to create a “main loop” for a program.

In the above example, the following numbers are printed to the console: 8, 48, 80, and 120. When x was 2 and y was 12, result was 24, triggering the code to skip to the next cycle of the loop without printing the result. When x was 5 and y was 24, result was 120 — the print statement is before the condition to break out, so 120 is printed and then the loop is exited. Let’s jump into the related do-while loops (which is not available in Python):

The difference between a while loop and a do-while loop is that the code inside a do-while loop always executes once, regardless if the condition is met. If the initial value of x was 30, the code would still “30” to the console in this example, and nothing is printed in the previous example. do-while loops make candidates for holding a main program loop. Let’s say I want a program to display a list of options, then ask for user input, then the follow up code to go with it. If I were to use a while loop I would need to have an initial display options code and an initial get user input code before the loop starts. Since the start of each loop is going to be the exact same code, using a do-while loop will read much cleaner.

The other major type of loop is the for loop. This is for when you know how many times you want the loop to run before you run it-you want to count from 1 through 10, you know the length of a list or array and print each item, etc. Let’s take a look at the for loop version of doing the while loop examples:

for loops in Python is done entirely through its list type (range(5) creates the list [0, 1, 2, 3, 4]). It accesses each item from left to right, assigning that item to variable x. Basic usage for the for loop in JavaScript and Java is start with the for keyword, then inside parenthesis, declare a variable with an initial value, semicolon, an exit condition, semicolon, and final how the declared variable changes on each cycle. You don’t necessarily need to increment or decrement by 1 for each cycle (an optimal solution for determining if a number is prime is to advance by 6 is cycle!). A common use with a for loop the JavaScript and Java way is that the declared variable good be used as an index to access each item in an array (or multiple arrays of the same length if needed). There are additional helper functions to loop through arrays or to transform arrays for other purposes, but we’ll explore those when we talk about arrays.

Boolean

True/false. Is even/is not even. This data type has exactly two states. Either the light switch is on or the light switch is off. At the lowest level, this data type is store with a digit binary number — 0 (off) and 1 (on) — that is all. Every comparison reduces down to this data type — is number 1 larger than number 2? Was “exit” type by the user? Does this bicycle have more than 4 gears? Booleans are what controls the flow of the program. Booleans also have shortcut operators to switch between true and false — using ! (JavaScript and Java) or not (Python) in front of a boolean value will give you the opposite value. This makes booleans ideal for toggling features like dark mode/light mode indicators or audio is muted/unmuted.

While you technically can test against the opposite of a boolean first, it’s generally not the best practice to lead with the not case first followed by the else block. Using the not operator on a boolean is generally for a case of using a search operation like finding a file, then assigning whether or not the file was found to a boolean variable. If the file was not found, you would want to escape out of the function early or else you would get errors for trying to perform operations on a non-existent file.

Characters and String of Characters

The final primitive data type in Java is char. This holds a single 16-bit Unicode character. Every thing computer related can be reduced to binary strings, a series of 0’s and 1’s. Each digit in a binary number is referred to as a bit. Unicode is a standard on how to covert a series of 0’s and 1’s into the characters on the screen. When using the 16-bit Unicode, 16 digits that are either 0 or 1 is used to describe a single character, there are 2¹⁶, or 65536, ways to place 0’s and 1’s into 16 spots, so this format can describe upper and lowercase versions of letters individually, several symbols, as well as special characters from many other languages to produce an international standard on how a character is stored at the lowest level (think zooming into an object enough to see the molecules that form the object). A series of chars can be combined to form a String type using Java’s String class. From the dev.java primitive type tutorial:

The String class is not technically a primitive data type, but considering the special support given to it by the language, you will probably tend to think of it as such.

Python and JavaScript do not utilize the char type, only strings as the primitive type to store text:

A basic feature within most, if not all, programming languages is the ability to concatenate strings, that is linking together strings to produce a longer string by “adding” them together. Some languages are strict when inserting other data types into a string while concatenating (notice in Python I needed to place my age integer into the str function?), while other languages will automatically invoke a toString method behind the scenes.

Some languages also include the concept of template strings. Rather than chain together a series of strings, you are able to inject variable data inside a string. In Python, if f is placed ahead of the opening single or double quote, you can wrap a variable inside curly brackets to inject it into a string. In JavaScript, if a string is wrapped in backticks (`), variables can be injected by using $ followed by the variable wrapped in curly brackets. Java doesn’t quite yet have this feature, though string templates been proposed to be added. Instead at this time, you can use the format method to indicate placeholders, followed by the variables to place in those spots.

Strings also have a few transformation methods, the ability to take its input and be displayed differently. Common cases includes converting to uppercase or to lowercase — very useful in comparing two strings in a case-insensitive manner (so that “TWO” and “two” are considered the same thing). Another useful method is to split a string into a series of words into an array or list of words — useful for things like search engines where you want to match each word in a search rather than exact phrase only. One more method I’ll touch on in this article is creating substrings — printing just a portion of a string. This is particularly useful (in conjunction with split) in creating chat bots on various services. A bot could be listening to every message sent in a chat waiting for a command, for example !weather. The bot is first paying attention to see if the first character of a string is its command activator (message[0] == “!”). If it notices an !, it then wants to see if the word that follows is in its command list (message.substring(1) in command_list).

It appears the latest version of Python does not allow a string to split with an empty separator (Two quotes next to each other within the parenthesis) to separate a string into individual letters, but casting string as a list will accomplish the same result.

This wraps up my exploration with the primitive data types in Java and how they relate back to JavaScript and Python. In the next article, I will explore arrays and objects!

--

--

Jon Cundiff
Jon Cundiff

Written by Jon Cundiff

Full-Stack JavaScript developer and Dart/Flutter enthusiast

No responses yet