Why You Should Learn Java First: Part 1, Basic Typing
Tags: java, typing, teaching, computerscience
Published: 2021-06-09 15:06:36

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg0ziscy0pd36bqreqpv.png)

When it came time to sign up for my first college classes, I was completely overwhelmed by the choices. Numerous biology classes, Spanish classes, chemistry classes, but worst of all, there were *so* many computer science classes. After researching my options, I decided to start with COMP 401: Programming Fundamentals. Like the name suggests, it focused heavily on the fundamentals behind programming languages. Things like syntax, best practices, but most importantly, data types. When I took this class, my professor primarily focused on teaching these skills in Java. While nearly every college begins introductory programming classes by teaching the concept of data types, they have done something strange over the last few years; they have moved to using dynamically-typed languages as student’s introduction to programming languages. 

Let’s look at the declaration of a new variable in both Python (dynamically typed) and Java (statically typed). 

**Python**
```python
new_number = 12
new_string = '12'
```

**Java**
```java
int new_number = 12;
String new_string = "12";
```

Upon first glance, the Python version is much easier to look at because it looks much more similar to English. However, it is oversimplifying some very fundamental programming language concepts. To a new developer, it is not clear that there is a distinct difference in the way `new_number` and `new_string` are stored in memory. To them, one has quotes and one doesn't. 

A new developer may try to run something like: 

**Python**
```python
print(new_number + new_string)
# Raises a TypeError
```


and they may wonder why an error is thrown. It is not immediately clear why this particular expression doesn't work. Upon reading the error messages, they will eventually discover the issue, but this becomes more troublesome as a codebase get larger.

**The issue:** Think of a String like a sentence in English. It's represented like an English word in memory. An integer is represented like a mathematical number that you can do operations on (addition, subtraction, etc.). This example essentially illustrates the computer trying to add the English version of "twelve" to the mathematical number 12. 

Let's look at a more complex example:

**Python**
```python
basic_list = [12, 5, '3', 8, '7']
sum = 0
for n in basic_list:
    sum += n
print(sum)
```

To an experienced programmer, it is immediately clear why this is an issue (you can't add integers to strings, as they are represented differently in memory). However, a new developer may not see this distinction. If they are learning Python as their first programming language, this oversimplified abstraction of how data is stored in memory can be extremely confusing. 

Let's take a look at a similar program in Java:

**Java**
```java
List<Integer> basic_list = Arrays.asList(12, 5, "3", 8, "7");
// The program halts here before the 
// rest of the code runs and throws and error
int sum = 0;
for (int n : basic_list) {
    sum += n;
}
System.out.println(sum);
```

Java, since it is statically typed, doesn't allow you to make the mistake of appending a String to an Integer List. The developer can immediately see that `basic_list` should only contain Integers. For a newer developer, this may seem annoying at first, but it prevents hard-to-understand logic errors later in the development process. 

While the difference is subtle, as a newer developer, this distinction is huge. I have had many friends and students come to me after receiving their first programming assignment, baffled by why their new program isn't returning the expected results. 99% of the time, Python typing issues are the root cause of the problem. Starting with a safe, statically typed language like Java (even though many see it as outdated) is the way to go if you want your students to understand typing.

*Stay tuned for part 2 of the series coming soon!*