Hello World in Python vs Java
Java and Python Hello World apps compared
A simple application that prints nothing more than the words ‘Hello World’ is the seminal start to learning any programming language.
Furthermore, the relative verbosity and expressiveness with which a given language prints these eleven Unicode characters has become a popular ‘flex’ between platform advocates.
Python and Java developers often go toe-to-toe on the ‘Hello World’ debate, which is why a thorough comparison of how to write ‘Hello World’ in the two languages is necessary.
Hello World in Java
To print ‘Hello World’ in Java’s JShell, it’s one line of code:
System.out.print("Hello World")
This one line of code prints ‘Hello World’ to the system’s default output stream, which Java’s expressiveness makes fairly obvious.
If you wanted to print to just the console window and not the default output stream, the code would be this:
System.console().writer().print("Hello World")
Windows components in Java
A Java developer could even print ‘Hello World’ to a Linux or Windows dialog box with a single line of Java code:
javax.swing.JOptionPane.showInputDialog("Hello World")
In fairness, this option should be broken into two statements:
- An import for the Swing package
- The call to display the input dialog box
A single line of code allows you to create complex Windows components in Java.
Nevertheless, it’s certainly impressive to see how easy it is to create windowing components with a default installation of the JDK. Generating GUI components is not possible with a default Python install.
In fact, I’m not sure if it’s even possible to print only to the console window with a default installation of Python. In Java, it’s just one, simple and expressive line of code.
I was told it would take a hundred lines of code to write 'Hello World' in Java.🤔
It didn't. 🤷♀️
It took one.🤦♀️ pic.twitter.com/sSIzq6zmwg— Miss Darcy DeClute (Scrumtuous Inc) (@scrumtuous) April 25, 2023
Hello World in Python
In the world of software development, Python developers can’t wait to ‘flex’ how easy it is to print ‘Hello World’ in the popular scripting language. Here’s how most Python tutorials demonstrate a ‘Hello World’:
print("Hello World")
It’s impressively terse, and contrasts directly with the expressive nature of Java. It may actually be too terse.
Python’s lack of expressiveness
In the prior Java examples, it was expressively clear where the output would be displayed. With Python, it’s not clear where ‘Hello World’ is being printed to.
- Is it printed to the log files?
- Is it printed to standard output?
- Is it printed to a terminal window?
- Is it printed to a printer?
- Will it display in a Windows dialog box?
Python definitely lacks the expressiveness of Java, but there’s a much more serious problem with this code.
Do requirements matter?
The fact is, this Python code doesn’t print out ‘Hello World’. It actually prints out ‘Hello World\n‘
The clearly stated requirement was to print out eleven Unicode characters. The Python code prints out 13, as the print method insists on always adding ‘\n’ to every print statement, regardless of whether it’s part of the requirements or not.
This is actually an interesting contrast between Python and Java.
New developers love Python because it often presupposes what a developer needs and makes arguably intelligent decisions for the programmer. In contrast, Java provides developers with a pure, unadulterated set of powerful APIs and trusts the developer to make their own intelligent and informed decisions about how to use them. There’s no hand-holding in Java.
Who decides what matters?
Most Python developers will argue that the result is ‘close enough’ and that the specific requirements don’t matter.
The ‘it doesn’t matter’ refrain is a common mantra amongst Python developers.
- Why is Python 300-500% slower than Java? Performance doesn’t matter.
- Why does Python consume 300% more energy than Java? The environment doesn’t matter.
- Why did it take 30 years to get a switch statement in Python? Language features don’t matter.
- Why can’t Python thread across multiple cores? Multithreading doesn’t matter.
And while it may be true that performance, language features, and global warming doesn’t matter, I would argue that requirements do.
A developer’s first program certainly isn’t the right time to be teaching aspiring young programmers that requirements are just nuisances they should dismiss.
Hands on with the Python plumbing
There are actually multiple ways to fix this problem and write some Python code that meets the requirements.
The easiest fix is to simply call the standard Python APIs, which requires two lines of code:
import sys sys.stdout.write("Hello World")
Unlike Java, Python does not automatically import the system object, which results in twice as many lines of code to accomplish what Java achieved earlier in just one.
Python’s built-in functions
The other way to fix the problem is to mess around with Python’s print method, which means learning about Python’s collection of built-in functions.
One of the most fundamental concepts in object-oriented programming is SOLID’s single-responsibility principle which says that a component should have one, single, clearly defined purpose.
However, to avoid the extra lines of code interacting with the standard Python APIs would generate, Python decided to violate the sacrosanct SOLID principles by creating a single, top level component that provides a dogs breakfast worth of cryptic and unrelated functions including:
- __import__()
- abs()
- globas()
- str()
- repr()
- chr()
- dict()
I’d like to spin, Jack
Putting aside the fact that most of these functions look like they were written by a Wheel of Fortune contestant who couldn’t afford to buy a vowel and didn’t want to spin again, these methods do come in handy and are loved by Python programmers, even though they create frustrating problems when trying to do something as simple as printing out ‘Hello World.’
Python provides a variety of built-in functions to hide the complexity of the underlying API.
Playing with Python’s porcelain
When you dig into the Python porcelain, you see that the print method, which is designed to simplify a developers life by not forcing them to touch the Python plumbing, is defined as such:
print(*objects, sep=' ', end='\n', file=None, flush=False)
This is certainly an intimidating method definition for a developer just trying to write a simple ‘Hello World’ program, but it’s a necessary evil if the developer is to deliver on the original requirements.
You can see from this definition that a newline character is automatically appended to whatever is printed out, so according to the documentation, to use Python’s print method without the newline being added, it would look like this:
print("Hello World", sep=' ', end='', file=None, flush=False)
Single and double quote confusion
Fortunately, certain arguments can be left out of Python’s print method, so after some time on StackOverflow and ChatGPT, the following solution was obtained:
print("Hello World", end='')
All of this work prints ‘Hello World’ to Python’s standard output stream.
I couldn’t find anything in the print method’s documentation that discussed how to print just to the console window like we did in the Java code above, and of course, a standard installation of Python doesn’t have any windowing capabilities. Printing ‘Hello World’ to a dialog box is simply not possible with a standard Python install.
Python vs Java compared
The simplicity and ease with which a programming language can print out eleven simple characters is a common comparison made to evaluate how user friendly a given platform is.
In this tutorial, you have been provided a judgment free, side by side comparison of how to write a ‘Hello World’ application in Java versus Python. Whichever one seems simpler, more expressive and most straight forward is entirely for you to decide.