DEV Community

Discussion on: Python vs Java

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

I use both languages. Let's compare the two languages:

Cross platform support:

  • Java : Requires JRE/JVM to run. The JVM has been ported to a lot of platforms. This removes the headache of distribution as you can simply share the jar file. It is also possible to generate a binary using GraalVM and other ahead of time compilers.
  • Python: If you are using pure python libraries, most things work fine. However, if you are using C extensions like numpy, it is more involved. There are projects like PyInstaller and nuitka. But cross-platform distribution of complex programs involves a lot more work.

Concurrency and Parallelism

  • Java : Sane multi-threading (OS level threads). Very good concurrency primitives. The java.util.concurrent package is a great for different types of abstractions like threadpools, atomic primitives, concurrent hashmaps. Futures and CompleteableFutures provide library support for async functions (no syntactic sugar).
  • Python : Multi-threading not advisable for compute intensive code due to Global Interpreter Lock. People have observed their programs becoming slower due to threads. Python also has a multiprocessing module which is good for a lot of tasks we use threads in other languages. Python also has asyncio and async/await keywords to mark functions as async and not block the event loop.

Type safety

  • Java : While certainly not on par with FP languages like Haskell/Ocaml, Java's type safety is ok for a lot of everyday programs. Java has generics (with some limitations like runtime type erasure). Features like Reflection and frameworks using dependency injection make Java's type system a lot less safe because these features are checked at runtime, thus a correctly compiling project is not guaranteed to work correctly.
  • Python: Python is a dynamically typed language with strong typing. This means that if you initialize a variable as a int, you can not implicitly cast it to a str. Example:
a = "Hello "
b = "World"
c = 123
print(a + b) # Works and prints Hello World
print(a+c) # Does not work as a is str and c is int
Enter fullscreen mode Exit fullscreen mode

Python's built-ins are very expressive (list, set, dict, tuples) and most are sufficient or most simple programs. This allows you to quickly prototype your projects.

Performance

  • Java: In terms of memory usage, Java programs use 10x more memory than their C/C++ counterparts. JVM has a JIT compiler which optimizes code that is called frequently. This allows Java programs to be competitive with C programs for some use-cases. Generally, Java programs are 3x slower than their C counterparts.
  • Python: Python programs also take more memory than C/C++ programs but not as much as Java. But this can be compounded by the fact that most python programs are single threaded, so they may run multiple processes. In terms of CPU performance, Python can be anywhere from 3-100x slower than C programs. A lot of popular libraries are C extensions which merely use Python for gluing code.

Expressiveness

  • Java: It is extremely verbose. Popular frameworks require boilerplate like getters/setters.
  • Python: Very expressive. Can use meta-programming to make your programs even terser at the cost of maintainability.

Ecosystem

Both Java and Python have extensive ecosystems. You will generally find libraries doing what you need in either of these languages.

The JVM ecosystem also has other languages which avoid the short-comings of Java e.g Scala, Kotlin, Clojure. These languages are much less verbose and can also leverage the extensive Java libraries.

Summary

At the end of the day, choosing Java or Python boils down to your preferences.
Here are a few niches where one can be better than the other (just my opinion):

  • AI/ML - Python
  • High Concurrency servers - Java
  • Command line tools - Python
  • Streaming data processing - Java (due to Kafka/Spark/Flink)
  • Web development - Java/Python
  • Cross platform GUI programs like IDEs - Java
Collapse
 
rhymes profile image
rhymes

Additional info: there's a version of Python (stuck at Python 2 unfortunately) which runs on the JVM: jython.org/

Collapse
 
rrampage profile image
Raunak Ramakrishnan

Yes, it is unfortunate that JPython never took off as much as JRuby did.

Collapse
 
michelemauro profile image
michelemauro

Minor typo: that's Clojure, not Closure.

Collapse
 
rrampage profile image
Raunak Ramakrishnan

Thanks!

Collapse
 
adipolak profile image
Adi Polak

Thank you, Raunak!
I know that the question was Java vs. Python.
But I think that for web development, at least from my end, JavaScript is the top.
I don't know many people who still develop frontend with Java and Python.

Another point regarding Expressiveness, there are many languages that are built on top of the JVM and with a goal to reduce verboseness from the code.

What do you see from your end?
which JVM language do you use? ( if any :) )

Collapse
 
rrampage profile image
Raunak Ramakrishnan

In web development, I feel that familiarity with a framework (its strong points and pitfalls) often tops any language feature. I use Flask in Python for quick prototyping and Dropwizard in Java for production ready applications. I have heard good things about Express in Node.

Re JVM languages I am getting familiar with Kotlin. It has many good features like data classes, Sum types, pattern matching and coroutines.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

In nearly any development, familiarity is king! If someone understands general principles of good software design, they can make good software in any language or framework they know well, whether that be Java, Python, C++, Haskell, or COBOL.

Collapse
 
siy profile image
Sergiy Yevtushenko

One clarification: usually Java performs very close to C (no 3x slowdown). In some cases (heavy allocation/deallocation) it may outperform C due to much lower overhead for such operations in Java runtime.

Collapse
 
siy profile image
Sergiy Yevtushenko

And one comment: yes, Java is verbose. But Java verbosity is not a bug, but feature. It allows Java code to explicitly provide context. This context significantly reduces mental overhead and navigation back and forth between usage and definition.

Collapse
 
thefern profile image
Fernando B 🚀

New java versions can create custom small jre images within applications so the need for a user to have a jre installed is no longer needed. Also jpackager can create installers. Overall java experience to deliver a full application as a whole has gotten a lot better.

Collapse
 
dagnelies profile image
Arnaud Dagnelies

I really like the accuracy and neutrality of your comment. However, I would like to add two more aspects to it.

Learning curve

Java: requires more overhead to get started, has more concepts to grasp (proper OO, == vs equals, hashcodes, ...) and lower "expressivity"
Python: trivial to pick up, very intuitive, easier to learn and get something done quickly

Culture

Java: many libs tends to over-engineer stuff, perhaps because their tooling is so good. But this leads to a large slow paced ecosystem. The JDK itself has also a poor "stewardship" (you still need a signed Oracle Agreement to even suggest a bugfix to the *Open*JDK)
Python: embraces simplicity and intuitiveness as its core philosophy. Fast paced ecosystem. Contributions and evolution of Python itself is also very open and community driven.

My own opinion: I find python very pleasant to work with. It's easy, it's intuitive, it has lots of "batteries included" and my usual choice for small projects. However, for bigger projects, I still tend to Java. The typing system, the tooling and good multithreading just beats it at some point when the project grows.