DEV Community

Cover image for Is Switching From Python to Java is a Good Idea?
John Selawsky
John Selawsky

Posted on

Is Switching From Python to Java is a Good Idea?

The idea of having Python as a first programming language has a rational background. First of all, the syntax of Python is short and clear and the underlying model of objects and variables working is perfectly consistent. That means you can write “real” and pretty powerful applications without great effort. So there is nothing strange that many schools teach students programming using Python.

However, knowing two languages is always better than one. If you are thinking of learning a second language after Python, Java could be a really nice choice. In this article, we are going to discuss switching from Python to Java in the case of a beginner software developer.

Why Java?

There are lots of different popular languages such as C++, C#, JavaScript… and there are pros and cons in learning any of them. So why Java? Here are some thoughts.

  • Java is one of the most popular languages according to the most rankings. Hence, it is easy to find a good job.
  • Java has industrial strength. It is used for large systems by large groups of people. Big companies need a lot of junior software developers. So it is beginner-friendly.
  • Java is a general-purpose language, you may use it for developing almost any types of programs. It has libraries for almost everything. Enterprise, Mobile, Big Data, Cloud Services, Software-defined networks (SDN) and many more.
  • If you know Java, learning more complicated languages such as C++ is much easier than just after Python.
  • Java is a native language for Android and Android is the most popular mobile platform. So knowing Java is the easiest way to mobile software development.
  • The family of JVM (Java Virtual Machine) languages is very wide and diverse. It is not only Java but modern Groovy, Closure, and Kotlin. As well as Ruby and Python special versions — JRuby and Jython. If you know how to work with JVM with its native language Java, it is easier to work with the others. There is a somewhat probability that Groovy or Kotlin could happen to be the next big programming thing (but not soon).

Switching to Java… What to do?

1. Learn the basics of Java (Java Core)

You may use one of the course Java for beginners (it will be fast and easy for you) or Java for Python software developers. For example:

  • Practical oriented course to learn Java — CodeGym. Learn Java on CodeGym means to solve a huge amount of coding tasks with fast automatic validation, tips, help and so on. Sure the lectures are also available, it is a complete course.
  • Javarevisited — it is a huge website with good articles, courses and interesting problems to solve.

Or elsewhere you may find these topics:

  • First of all read about this scary Java Syntax a little bit
  • Go and try to write easy coding tasks just to get used to your new life
  • Everything in Java is Object, so learn OOP
  • Learn Java Collections, they are really convenient
  • As well as tough but useful Java Multithreading ### 2. “Translate” your own projects from Python to Java. It could be easy coding tasks you did while your studying or your pet projects if you did them. Just rewrite it in Java. It is very useful. ### 3. Learn about JVM (for more advanced programmers) First, you may read some articles about JVM, Memory management, Garbage collection in Java:
  • How JVM Works — JVM Architecture?
  • Understanding JVM Internals There are some interesting courses about the topic. Such as:
  • Understanding the Java Virtual Machine: Memory Management
  • Understanding the Java Virtual Machine: Class loading and Reflection
  • Understanding the Java Virtual Machine: Security
  • Don’t forget to subscribe to Java’s StackOverflow and some Reddit communities, for example, learnjava. Use them! ## Before you start. “Progressive” Python vs “legacy” Java? I came across the opinion that Java is a relic of the past, and Python is modern and progressive very often. It’s funny, especially when you consider that Python is a little bit older than Java. Of course, Python overtook its time. You may say, Java “shot” immediately, became popular quickly, while Python did it very gradually, and only now began to catch up with Java in terms of prevalence. They both took serious changes through the years becoming better for modern programming and keeping their advantages. I want to say they both have strong and weak sides, and it is not about progressivity. Here I am going to describe real and the most important differences between the languages. ## The main difference: dynamically typed Python vs statically-typed Java Java is a statically typed, while Python is dynamically typed. This is the main difference between the topic’s languages. Technically it means that in Python you create your variables and don’t define their type. Types are defined only at runtime and the only condition for not having a runtime bug is to support the operations you used to the variables. So, if we have two objects such as Bird and Dog and give them a command to move fast before we run the program they are both the same. However, if we did everything right at runtime first will fly and the second will run. In Java, the Dog “knows” that it is a dog before runtime because it was defined by a programmer. You write more in Java while your computer has less work. ## Python laconic syntax vs Java verbose one Python was created keeping short syntax and readability in mind. Java is more about the strict structure and staying away from accidental mistakes. Classical example: Java
public class HelloWord
{
     public static void main (String[] args)
       {
           System.out.println(“Hello, world!”);
        }
}

Python

print “Hello, world!”

Interpreted Python and somewhat compiled Java

Java is a compiled language and Python is interpreted. It is a simplification to say like this, however it is close to the truth.

Anyway during the execution Python code is interpreted at runtime. Java code is compiled into bytecode of JVM at compile-time and JVM runs it.

Cross-Platform

Java Virtual Machine (JVM) is a powerful tool to create portable cross-platform applications. These applications can run on any device with JVM installed. Python compiler converts code into a specific machine code that depends on the operating system.

Multithreading in Java and in Python

Multithreading in Java realized much better than in Python (here we are talking about standard CPython). Memory management in Python is not thread-safe, hence, the language’s implementation prevents the multithreaded execution of Python bytecode.

In Python, there is the global interpreter lock or GIL and this mutex prevents multiple threads from execution in Python. So, the only multithreading option of Python is access to IO resources. So, Python lets bytecode execution of the second thread while the first one should wait for the data from the network or file system.

A Java process successfully uses multiple cores and hyperthreading. Thread safety guarantees hide not inside the language, but it in many thread-safe tools to make it an easy process.

Backward compatibility

Usually, the evolution of a programming language involves backward compatibility. However, this does not apply fully to Python. Python 2 came about in 2000, and Python 3 hit the scene in 2008. They are more or less compatible but have enough differences in functionality and syntax, so that in principle they can be considered as different languages. Instead of modifying new trends and ideas in Python 2, Python 3 was conceived as a new language that somewhat used the experience of Python 2. The development of Python 2 continued separately, and its final implementation (version 2.7) will support only to 2020.

Java was created with keeping backward compatibility in mind. Is this good or bad? This approach has led to a huge legacy codebase. On the other hand, this ancient code can be gradually updated, and the risks that the “veteran” will not start in the modern environment in the case of Java are minimized.

So what?

I described some of the main differences, but hardly mentioned what they lead to. At this point, I promise to fix it and talk about what these differences influence, from the point of view of a software developer.

Coding Speed or Programmer Efficiency

The shorter syntax is the faster the developer can write it. In Python, you shouldn’t write all these brackets and classes and even variable types so… yes, Python programmers work faster than Java developers, especially in small teams with short programs and scripts. Java is verbose, yet very well structured. Therefore…

Сode support and analysis efficiency

…therefore the analysis and support of Java code is much easier than Python’s especially in case of huge projects. In Python lack of type information turns such analysis into a hard problem. Moreover, Java provides type safety and catches all potential errors at compile-time, not at runtime, like Python. So, the probability of errors tends to be reduced. It seriously simplifies the management of large applications. Runtime errors are more difficult to identify and fix than errors during compilation.

Speed of program executing

In the case of Python, you do less, but the computer does more. In Java, everything is upside down compared to Python. So, because of all these statics and compilation instead of dynamic typing and interpretation, Java is working much faster. That means your Java program will execute in a shorter period of time and operations will work faster as well. It is not always a noticeable difference for the user. However, sometimes it could be critical.

Conclusions

Professional software developers don’t always get to choose what tools to use. Times are changing the preferences of employers that may change along with them. The good news is that the tools are much more alike than they may seem. And Java is a good and powerful one even if after Python it seems too verbose.

Top comments (2)

Collapse
 
iceorfiresite profile image
Ice or Fire

It's always good to know more programming languages but if I had to choose, I'd pick Python over Java any day.

Collapse
 
dihfahsih1 profile image
Mugoya Dihfahsih

Wow that was an awesome analysis of #python and #java though i have used pyyhon for a year now, i just feel so comfortable not switching to anyother