This example works perfectly in Python. In Java however, it would result in an error. It’s quite a complicated issue. Let’s get into why that is.

Static Typing and Dynamic Typing

The difference can be found in how both programming languages handle typing (assigning a datatype to a viable). Java is what is called a “statically typed” or “strongly typed” programming language. This means that once a variable has been assigned a datatype, it can’t be changed. The datatypes get checked before run-time, resulting in an error before your code gets executed.

There is no real agreement on what “strongly typed” means. Although the most widely used definition in the professional literature is that in a “strongly typed” language, it is not possible for the programmer to work around the restrictions imposed by the type system. This term is almost always used to describe statically typed languages.

Python is a dynamically typed language, which is the opposite. Python is also interpreted. This means that code is translated (into 1’s and 0’s) during execution. Java on the other hand, is compiled. In Java, code is translated before execution.

Since Python translates code during execution, it’s highly flexible about reassigning variables to different types. Just look at the example above. If you want to see how far you can push this flexibility, take a look at this example.

Normally, the above example would result in an error upon execution. That’s because Python can’t concatenate a String to an integer. However, since the if-else statement will validate to True on the first condition (if), the second condition (else) is never executed. Thus, it won’t result in an error. This will cause an error in statically typed languages, however.

Wrapping up

This was a short write-up on the interesting, but still confusing, “typing” difference. The most important thing to take away is that Python gives you a bit more flexibility when programming.

Let me know if this was helpful or if you would like to see more of these types or articles in the future. As always, if you have questions or concerns, feel free to comment below. Lastly, make sure to share the article if you liked it!

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

Categories

Archives

Iconic Developers is a personal blog dedicated to my journey through the world of software and web development where I share my knowledge and experience. Do you have questions, concerns or anything else? Just get in touch!