DEV Community

Ben Halpern
Ben Halpern Subscriber

Posted on

Python vs Java

Let's compare and contrast these programming languages in terms of features, best use cases for each, and perhaps which is "better" than the other depending on context.

Latest comments (91)

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Python excels at general flexibility and scripting use cases. Java does well at GUIs and GUI prototyping. Both are good at server-side uses. Both are weak at packaging but for different reasons. I use both languages, and I'd recommend that you do too!

Collapse
 
_hs_ profile image
HS

Can you do stuff with SIM cards in JavaScirpt? Highly doubt it. And such examples might trigger "niche" comment which basically is saying let's ignore elephant in the room and focus on mouse - which is web app development and that includes microservices and such.

Go check how big is Spring alone. It's not a framework it's quite a few frameworks which can make a platform. Check Apache Java stuff, like Apache Camel which is similar to Spring Integrations - still Java and still one of the go-to solutions for integrations none of which I've seen in JS and integrations are even out of "businesses niche" now. Almost all of us do it even for mid sized projects. Try to look at Red Hat stuff made for Java alone. Go browse through maven repository. Not even C# ecosystem (or .NET to be more correct) can compare to it in terms of size/quantity. And this is why people are not willing to leave JVM. It has so much things done you rather cry how hard they are to work with than to write it yourself in another platform.
It's plain simple, number of libraries, frameworks and such, regardless of their use case, are bigger for JVM mainly written in Java because it was the language before Scala, Groovy, Clojure, and such, and Kotlin lately.

Basically that "niche" your talking about is waaaaay bigger than your "non niche" which makes no sense for me to call it that word then.

You might call it new COBOL which indicates it will be forgotten by many at some point but live like a plague and poison anyone who touches legacy system that must work, but it doesn't change the fact that we still need something to catch up with it's size to be able to say "Java is dead" and make sense.

Collapse
 
hammertoe profile image
Matt Hamilton

Better to ask forgiveness than permission ;)

Collapse
 
habereder profile image
Raphael Habereder • Edited

Java is niche? I disagree strongly. It might be (falsely so) considered legacy by some, but it is by no means niche.
Java is now, with microservices and containers, facing a huge demand and second wind once again.

I usually don't defend any language, because it's mostly flavour and opinions, but Java is definitely a language we can call full-stack.

Javascript may be much larger in it's userbase, but even so it's ecosystem is smaller.

With Java you get full fledged and battletested CI/CD (Jenkins), package and artifact Management (Nexus, artifactory), security scanning (NexusIQ, Sonar), observability (built into JVM), deployment Management (AppServers of different flavours) and much much more.

Everything managed by commonly accepted and well-defined Standards.

Thats not something you can get with Javascript as of now.

Libraries, of which Java has more than enough for every possible use-case, aren't everything I'm afraid.

I'm not denying Javascript it's deserved glory, far from it. The improvements it made over the last 10 years are astonishing!
But the tooling changes so rapidly and often, getting something as proven and tested as the Java World Counterparts will take a lot more time.

Thread Thread
 
dhruvgarg79 profile image
Dhruv garg

I would also like to add Quarkus. I am not very experienced but It looks really awesome to me. with graalVM and quarkus, we can now make native java executable and size of these executable are very small compared to JAR. It's performance will also be better since it is machine code instead of byte code. The startup time is also very fast, which is important for microservices in some cases.

Best of all, we can use same API's like JPA, CDI etc. so an experienced java developer can get easily started with quarkus.

Do check it out - quarkus.io/

Thread Thread
 
habereder profile image
Raphael Habereder • Edited

Absolutely true.
In one of my clients projects we are using Quarkus and migrate quite a few webservices to it currently.
The performance and minimal overhead of the resulting native binaries is amazing.
It takes quite a bit of modification to the typical CI-Chain, but if you get it to run smoothly, the results are astonishing.

If you want, I could write up a little article about it here

Thread Thread
 
dhruvgarg79 profile image
Dhruv garg

yeah, I would love it.

It will be very helpful as there is relatively less material available on quarkus.

Thread Thread
 
habereder profile image
Raphael Habereder

Consider it in the works :)

Thread Thread
 
habereder profile image
Raphael Habereder

It is done :)

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
 
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.

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
 
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
 
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
 
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
 
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
 
michelemauro profile image
michelemauro

Minor typo: that's Clojure, not Closure.

Collapse
 
rrampage profile image
Raunak Ramakrishnan

Thanks!

Collapse
 
delta456 profile image
Swastik Baranwal

When Python and Java married then Kotlin was born which is better than it's parents!

Collapse
 
siy profile image
Sergiy Yevtushenko

In the attempt to be"better Java than Java" Kotlin made a lot of stupid mistakes. Attempt to achieve brevity by removing semicolons is one of the my favorites: as a consequence of this decision it's possible to break "DSL-like" code just by wrapping line.

Collapse
 
xowap profile image
Rémy 🤖

Let it be noted that Java is one of the first programming languages that I learned and specifically it's the first language that I used to make actual GUIs and it made me very happy at the time. Yet...

Java is an utter lack of flexibility, boring patterns and incredibly unbearable editor (Oracle rot in hell). People will ask "YES BUT HAVE YOU SEEN THE LATEST FEATURES THE LANGUAGE HAS CHANGED" to which I will reply: I don't care this language has so much legacy of abuse and stupid libs and over-engineering that it's just crap. The language is made to make you do stupid shit like silencing exceptions all the time or typecasting pointers because there is no other way to communicate your data (Android Intents I'm looking at you).

Python has all the tools in the box. Strong typing, duck typing, objects, functions, very extensive standard library, JIT in general, JIT for math stuff, C extensions, ..., everything. It just doesn't get in your way to do whatever you need to be done using the paradigms that you want to use. And as a bonus it has some very nice constructs like iterators, list comprehensions or context managers that make your life really easier and that don't really exist in other mainstream languages.

Collapse
 
michelemauro profile image
michelemauro

Your points are valid, however let my suggest you that Java support for VSCode is very good; and if your problem is just with JDeveloper, there are many others to choose from: Eclipse and Idea just to cover the greater part of the market. Also, Android can't completely be called "Java" because technically it isn't, but your point is valid. You seem to have seen lots of what is considered bad Java code.

Collapse
 
xowap profile image
Rémy 🤖

Sorry I meant Oracle as editor of the Java itself.

And I must admit that I never saw any good-looking Java code. Though just switching to Kotlin for Android and using the exact same APIs already feels much better.

Collapse
 
habereder profile image
Raphael Habereder

I assume you meant JDeveloper with unbearable editor? Urgh, the thought already kills me a little bit inside...
And Oracle ADF. God save me

Collapse
 
habereder profile image
Raphael Habereder • Edited

Java has such an enormous eco-system built around itself, it's hard to even compare it to any other language. It probably can do almost anything, setting aside the age-old topic of "if we can do it, should we?".

Pythons ecosystem on the other hand is rather small in comparison, but it got a nice niche in AI/Big Data it dominates handily.

To be honest, it's not the language java itself, that I love. It's what Maven and Gradle make out of it that really eases the process of developing with java. Once you get used to your build-tool of choice, it's just copy-pasting dependencies and bam, you get everything you may need, including the appropriate build plugins for your workload.

Fire up a simple build command and you get a nice deliverable artifact. How amazing is that?

While Java is heavily restricted by it's conventions, you get a lot out of it for free. Getting back to maven, for example. Add your pom.xml with the typical boilerplate stuff and you essentially get the whole build-chain for free.
Python doesn't have as many conventions, but that freedom costs you the effort of doing most things yourself.

Edit: I'll refrain from editing out my wrong statements, but absolutely recommend reading the answers to my comment that correct them!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Pythons ecosystem on the other hand is rather small in comparison, but it got a nice niche in AI/Big Data it dominates handily.

I hear about this purported "niche" a lot, but in nearly a decade of Python coding, I've never observed it. Python has a fairly broad ecosystem when you factor in libraries (installable via pip or poetry in one line): it handles not only AI and data, but is excellent at GUI, networking, system interoperability, and just about anything you can throw at it. The only area Python really doesn't excel at is true multiprocessing, thanks to the Global Interpreter Lock (GIL), but there's work being done to resolve that too.

Python doesn't have as many conventions, but that freedom costs you the effort of doing most things yourself.

Uhm, this is most certainly not true. Python has a greater emphasis on convention and The One Obvious Way (as we put it) than most other languages, Java included. We have no fear of boilerplate — cargo cult programming, as Guido calls it — but we do avoid mindless boilerplate when we can (except, perhaps, in setup.py). Testing, packaging, deployment, all of these work well once you know where a couple of pieces fit, to the same degree as Java and Maven.

Collapse
 
habereder profile image
Raphael Habereder

Interesting, I never really observed Python like that, though judging by your comments, it's absolutely certain you are far more knowledgeable with Python than I am.

With eco-system I meant more the tooling around the languages, like CI/CD, Security Scanning, Lifecycle-Management and the likes.
I would assume a big portion of that is probably not even necessary for Python. What would your take be on that?

So thank you for your input and correcting my wrong statements.
It seems I have to do more Research next time

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited
  • CI/CD: There are a number of robust testing frameworks, including PyTest, Nose, and the newer Ward. Python also integrates beautifully into most CI/CD frameworks, especially given the relative ease of deploying a package. (On that note, many Python projects even automate publishing their package to the Python Package Index using GitHub Actions or the like.)

  • Security Scanning: Bandit is one prominent example, and I know there are others.

  • I don't know about Lifecycle-Management, as I haven't used it, but a casual glance through the Python Package Index, or PyPI, it looks like there are plenty of options.

It's a common myth that Python isn't suitable for large projects. In fact, there are a number of very large applications and libraries built entirely in Python. It has its flaws, but Python is often excellent for software architecture and deployment because it "just works" in most situations, and aims to be intuitive while staying out of your way.

Thread Thread
 
habereder profile image
Raphael Habereder

Thank you for your take, you never stop learning!
I guess I'll spend this weekend on Python and taking a look at the tools you mentioned.

The last time I had any python on my screen was when I wrote custom ansible filters and a few shallow dips into Django, which confused the hell out of me.
It's about time for a refresher :)

Thread Thread
 
codemouse92 profile image
Jason C. McDonald
Collapse
 
siy profile image
Sergiy Yevtushenko

It turned out that many java limitations are very useful in long run. For example, quite strict linking between class and it's file name and location allows to keep at least some level of order in project codebase. Once this restriction is removed (Kotlin) maintaining order became a sensible effort, since devs tend to make shortcuts.

Collapse
 
habereder profile image
Raphael Habereder

I absolutely agree.

Though I am more focused on the conventions part that comes with Java, since that is probably the first thing a Newcomer encounters.

Most frameworks, especially in the EE Version are convention over configuration. Conventions are just that, they can be ignored.
Which you absolutely shouldn't, but hey, you could even use pointers and allocate memory yourself if you want, so theres that.

The conventions are, mostly, of intelligent design and as a bonus it saves you a lot of time you would otherwise spend on cumbersome configuration, by having sensible defaults.

CDI/JPA (especially compared to their initial/earlier versions) would be a great example of convention over configuration and how much is done in the background for you.

Collapse
 
madza profile image
Madza

Chrome vs Firefox would be nice in future :)

Collapse
 
michelemauro profile image
michelemauro

Back in my days (upper '90s), we had stints like these; they were borderline flamebaits, and led to (by far) more heated debates. A couple of those were "Perl vs Python" (the way it ended is... interesting, given that at the time I was in the Perl camp) and "Vim vs Emacs".

Collapse
 
louy2 profile image
Yufan Lou

I want all schools teaching Java to complete beginners to stop and change to Python. The amount of boilerplate to start up a Java project hinders understanding of the essence of computing.

Below is hearsay.

Reserve Python for utility scripts and data exploration. For production, pick Julia if you are doing data analysis and AI, and Java if you are writing a service. The extra speed can keep your monolith fast enough longer so your scale wouldn't force you to split it up.

Collapse
 
daveparr profile image
Dave Parr

Yes Julia for data analysis an AI for speed (apparently, still not found the time to play with it properly yet), but with the tradeoff being that Julia is still far from an 'accepted' language.

A significant proportion of the data-science field that I interact with I would doubt have heard of it. FWIW when asked about why Jupyter notebooks are called Jupyter, I would give it 50:50 that a user would be able to state it stands for Julia-Python-R notebooks. Looking at AWS as a major example, their docs for their data science services talk about python first, R sometimes, and Julia extremely rarely.

That said, I am not embedded in the Julia community at all, so there is significant bias in my understanding. Is Julia something you have put into prod? What were your experiences, would you mind descirbing a high level over view of the tasks and the stack? I'm always keen to learn.

Collapse
 
louy2 profile image
Yufan Lou

Sorry I have to disappoint you, but as I wrote, it was hearsay -- I am not involved in prod system with Julia. TIL that Jupyter is Julia-Python-R! I've read a few cases of unintentionally running loops in Python instead of numpy, which is slow. I hope you have better luck exploring Julia further elsewhere!