Comparing Java to Python: A Syntax Mapping

Comparing Java to Python: A Syntax Mapping Featured Image

Awhile back, I wrote an article about how to approach Python from a Java perspective. My general reason for writing that article was to get students in my Java classes interested in Python. After all, I love the language. Why shouldn’t they?

Of course, that article really only gets at some of the major differences like static and dynamic typing. In this article, I thought it would be fun to actually put a list of syntax elements in Java and sharing their Python equivalents.

Table of Contents

Article Layout

Usually, I use a bit of a narrative style to write my articles. I know folks in the coding community can hate this. After all, the general approach we have as a community is to use the internet for answers. That’s why sites like Stack Overflow are so popular. As a result, I want to take an approach in this article that will help the most folks.

Rather than outlining all the differences and sharing anecdotes, I’m going to layout the remainder of this article using clear headings and lookup tables. For example, if you want to know how to convert from && to the Python equivalent, you can find the answer in the boolean operators section below.

With that said, if you think anything is missing, let me know!

Mapping Java Syntax to Python Syntax

In the remainder of this article, you’ll find tables for all the major types of syntax elements in Java such as boolean and arithmetic operators as well as control flow structures.

Boolean Operators

Boolean operators are a set of operators used to make sense of boolean values. These operators include but are not limited to AND, OR, and NOT.

OperatorJavaPython
AND&&and
OR||or
NOT!not

Often time, the comparison operators are included in this set as well. For example, it’s not uncommon for LESS-THAN to be considered a boolean operator because the return value is a boolean.

OperatorJavaPython
LESS-THAN<<
LESS-THAN-OR-EQUAL-TO<=<=
EQUALS====
GREATER-THAN-OR-EQUAL-TO>=>=
GREATER-THAN>>
NOT-EQUALS!=!=

Arithmetic Operators

In addition to boolean operators, most programming languages support some set of arithmetic operators. Fortunately, these map more or less one-to-one for Java and Python.

OperatorJavaPython
ADDITION++
SUBTRACTION--
MULTIPLICATION**
DIVISION//
FLOOR DIVISIONN/A//
POWERN/A**
REMAINDER%N/A
MODULUSN/A%

I particularly like to lay out the operators like this because there are operators between the two languages that are similar but not the same. For example, the Java division operator depends on the types of numbers used around it. Meanwhile, the Python division operator behaves exactly as you’d expect. If you want an operator that behaves like integer division in Python, you can use the floor division operator.

Similar arguments can be made about the remainder and modulus operators. Python implements true modulus while Java only implements remainder. The difference often doesn’t matter, but it’s important to keep in mind for common tasks like testing even and odd. In true modulus, mod by two guarantees a positive number. That is not true for the remainder operator.

Assignment Operators

It might seem weird to consider more than one assignment operator, but many languages include multiple compound assignment operators for arithmetic. Let’s take a look.

OperatorJavaPython
ASSIGNMENT STATEMENT==
ASSIGNMENT EXPRESSION=:=
ADDITION ASSIGNMENT+=+=
SUBTRACTION ASSIGNMENT-=-=
MULTIPLICATION ASSIGNMENT*= *=
DIVISION ASSIGNMENT/= /=
REMAINDER ASSIGNMENT%= N/A
MODULUS ASSIGNMENTN/A%=

In addition, Java features increment and decrement assignment operators that are not included in Python:

OperatorJavaPython
INCREMENT++N/A
DECREMENT--N/A

Control Flow

Because both Python and Java are imperative programming languages, we have access to various control flow structures. Let’s start with the various if statement structures.

StructureJavaPython
IF STATEMENT
if (condition) {
    // code
}
if condition:
    # code
IF/ELSE STATEMENT
if (condition) {
    // code
} else {
    // code
}
if condition:
    # code
else:
    # code
IF/ELSE IF/ELSE STATEMENT
if (condition) {
    // code
} else if (condition) {
    // code
} else {
    // code
}
if condition:
    # code
elif condition:
    # code
else:
    # code

One thing I find really interesting about the if statement structures in Java and Python is that Python has a special keyword for “else if”. Java, on the other hand, does not have a special syntax for “else if” and instead relies on nesting of the else and the if keywords.

With that said, let’s take a look at loops.

StructureJavaPython
WHILE LOOP
while (condition) {
    // code
}
while condition:
    # code
FOR LOOP
for (init; condition; update) {
    // code
}
N/A
FOR EACH LOOP
for (type name: iterable) {
    // code
}
for name in iterable:
    # code
DO WHILE LOOP
do {
    // code
} while (condition)
N/A

While I tend to argue that there are no traditional for loops in Python, it’s not impossible to make one. For example, many folks use range() or enumerate() to simulate traditional for loops.

Looking for More?

Like Java, Python is a language that has many features that aren’t demonstrated here. For example, here’s a list of some of my favorite Python language features.

If you’re interested in learning more about Python, I have several series that attempt to broach the topic from the lens of a beginner. For instance, if you’re interested in learning how to make a Discord bot and Python at the same time, I have a series for that. Likewise, I have a series for folks who want to learn Python in a more traditional way.

While you’re here, I’d appreciate it if you took a peek at some of these Python resources (#ad):

Otherwise, thanks again for hanging out! I appreciate your time.

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