Dump Java: Life Is Just Better in Python

Dump Java: Life Is Just Better in Python Featured Image

If you’ve spent any time around me, you know I love Python. It’s a language I fell in love with in 2017, and I’ve been hooked on it ever since. As a result, I figured I’d write an article for my students who are looking to branch out beyond the Java we teach in the classroom. To me, Python feels like a good place to go.

Table of Contents

The Benefit of Context

In my day-to-day life, I spend a lot of time teaching students Java. Currently, I spend about two hours a day teaching students Java in one form or another. Most often, I run a pair of lectures and labs where we cover topics from Java syntax to data structures to GUIs.

As much as I love my job, Java is not super fun to teach, and there are plenty of reasons for that. Of course, if all you’ve ever used is Java, it can be hard to see why. That’s why I thought it would be fun to talk about some of the more frustrating aspects of Java through the lens of Python, a significantly less painful language to use.

For context, I’ve been using Python in various capacities since 2017. That’s about half as long as I’ve been using Java, which I first learned in 2012. That said, if I could recommend a language to anyone, it would have to be Python. There are just so many things it does better than Java that makes teaching Java a real pain.

For further context, my knowledge of Java is limited to version 8. As a result, I’m not very aware of the latest and greatest features of Java. That said, with the existence of languages like C# and Kotlin, I suspect that Java has done very little to improve itself. And unfortunately, I can’t find a lot of resources on the use of modern Java, so it’s tough to tell if it has any new features worth considering. However, this resource seemed pretty helpfulOpens in a new tab.. Regardless, I still stand by converting as many folks to Python as possible. Let’s talk about that!

Python Strengths Over Java

There are probably dozens of reasons why I would recommend Python over Java for general use programming, but here are the main ones from the perspective of an educator.

Python Numbers Have No Bounds

When you use many of the mainstream languages like Java, you have to contend with “primitive types” that have bit-based limits. For example, in Java, the default integer is 32-bit. That means you’re limited to about 4,000,000,000 values.

In Python, numbers don’t have any restrictions related to range, so you can define any number you’d like of any size:

num = 12304927493401

And in case it’s not extremely obvious how large this number is, here’s the same number with the thousands separated out:

num = 12_304_927_493_401

And if that’s not enough for you, here’s a massive exponent:

>>> 2 ** 1001
21430172143725346418968500981200036211228096234110672148875007767407021022498722449863967576313917162551893458351062936503742905713846280871969155149397149607869135549648461970842149210124742283755908364306092949967163882534797535118331087892154125829142392955373084335320859663305248773674411336138752

That’s right! Because there’s no bound on numbers in Python, there’s no such thing as overflow. Good luck doing that in native Java (yes, I know objects exist to do this sort of thing).

Python Lists Are Not Limited by Size

Along the same vein as number bounds, Python lists are also not bounded in size. If you’re familiar with the Java syntax for arrays, you’ll know that you have to pick a size for them from initialization. If you want to use something more flexible, you have to import a collection like ArrayList.

In Python, array lists are the default list. As a result, you can create lists as easily as the following:

nums = [4, 1, 2, 3]

Naturally, you get all the benefits of an array with the additional benefit of being able to change its size:

nums.append(5) # [4, 1, 2, 3, 5]
nums += [3, 4]  # [4, 1, 2, 3, 5, 3, 4] 

Lists in Python are so versatile that you can essentially use them as any sequence data structure. For instance, methods like `pop()` and `insert()` can be used as needed.

Python Code Is Basically Pseudocode

In general, Python does a nice job of limiting the types of symbols involved in code (though, that’s changed more recently). Instead, the language focuses on using English where it makes sense. For example, all of the major Boolean operators are written in plain English as opposed to symbols. Here’s what that looks like in Java:

&&  // and
||  // or
!   // not

Meanwhile, in Python, we have the following:

and
or
not

Similarly, if you want to know if a sequence contains a value, you can use in directly:

 7 in [2, 3, 4]  # False

Speaking of symbols, Java uses a lot of symbols to denote blocks and expressions. As a result, you may end up in a mess of parentheses and braces from time to time:

if (...) {
  while (...) {
    if (...) {
    } else {
    }
  }
}

When writing pseudocode, you typically denote blocks using indentation. That’s exactly how Python works:

if ...:
  while ...:
    if ...:
    else ...:

Given these benefits, I tend to find that Python code is easier to read on average. In other words, while it’s possible to write bad Python code, I find it a lot easier to write bad Java (and C++) code.

Python Code Is Significantly Less Verbose

Listen, I’ll be a static typing fan until the day I die, but have you ever considered just how absurd it is that you have to specify types on both sides of a variable declaration in Java. As soon as we want any sort of type flexibility, we have to introduce generics which very quickly blow up variable definitions and method headers. Just look at this ridiculous line of code:

Map<String, String> m = new Map<>();

That’s 36 characters to create a variable, and there’s nothing in it! How about this line:

public static void duplicate(ArrayList<Integer> nums) { ... }

As you can probably imagine, if you ever need to nest generic types, it’s game over.

On the flip side, if you want to make a similar variable definition in Python, it might look as follows:

m = {}

Similarly, if you want to pass a map to a function, here’s what that might look like:

def duplicate(m): pass

And if you really want to be clear on your types, you can specify them:

def duplicate(m: dict[str, str]): pass

I can’t begin to tell you how painful it is to bust out a dry erase board or some other tool to write down some code for a student, only to run out of horizontal space just defining an object. It’s probably no surprise that folks who use linters end up with variable definitions that span multiple lines (damn those pesky line limits).

Python Has Native Functional Programming Features

One of the more frustrating aspects of Java is the lack of functional programming features. While lambdas technically exist, they’re a bit clunky. And if you’ve ever used Swing, you know how annoying it is to bind functions to buttons.

In Python, on the other hand, functions are data. As a result, we can store them in variables and data structures:

nums = [2, 5, 3, 1]
funcs = [min, max]
results = [func(nums) for func in funcs]  # stores [1, 5]

Now, this code probably looks kind of wild. Basically, we create a list of numbers. We also create a list of functions. In this case, I chose `min()` and `max()`. Then, we use a list comprehension to traverse the list of functions and pass our list of numbers to each of them. The result is an array containing the minimum and maximum values of the list.

Being able to pass functions around like this is incredibly handy in certain contexts. For me, there have been several times where I’ve wanted to be able to pass functions around in Java only to be forced to make an object to somewhat mimic the behavior I want. That’s definitely not ideal. Regardless, things may have been improved on the Java end since version 8. That said, I’m still suspicious.

Java Is Still a Good Language

As much trash as I talk about Java, it’s still an excellent language. In fact, I jokingly wrote about it against other languages earlier this year. It managed to rank at 11th of 28 languages, so it’s definitely in the top half of programming languages. Of course, that’s not saying much when languages like C# and Kotlin are sitting in the 2nd and 3rd positions, respectively.

And of course, I have to throw Java some love as it was my first programming language. It’s also the language I started writing about on this site. Finally, as with everything I write, take it all with a grain of salt. We all have our biases, and I’m no exception. Use the tool that’s right for you!

With that said, thanks again for checking out my work. I appreciate it. If you want to read more like this, check out one of the following related articles:

See, I still write about Java! I have students who need this sort of stuff, and I’ll keep writing about it until I’m asked to teach something else.

If these articles aren’t enough for you, consider checking out my list of ways to grow the site. There, you’ll find links to various resources such as my Patreon, YouTube channel, and Newsletter. Thanks for hanging out!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts