Java vs. Python: Application code comparison
I recently recently compared Java’s REPL scripting environment to Python’s.
Many detractors felt that such an apples-to-apples comparison was unfair.
The general consensus from the Python community was for a fairer, more balanced comparison: pit a modern Python app written in 2023 against a Java applet from 1998, or a standalone Java application written for the first release of the JDK.
I’d be happy to compare Java applets to any of Python’s browser-based capabilities.
Apples to applets comparisons
For 20 years, Java applets made it possible to run Java code in a browser. Python has never had any similar functionality, so mark that as a Java win.
However, to compare Java versus Python through the development of a standalone application with a single entry point is a bit more nuanced. That’s what I’ll tackle here.
HelloWorld application in Java
To write a modern, standalone Java application that uses Java 21’s unnamed classes and instance main method features, the full code is as follows:
void main() { System.out.print("Hello World"); }
Enterprise grade Java
Java’s main method syntax is not only elegant and familiar, it is also flexible enough to support a myriad of enterprise use cases. That includes the need to pass command-line arguments to the program, a common requirement.
To do this, Java program’s main method simply references an array of Strings. For example, here’s the code to output the number of arguments passed into a standalone Java app:
void main(String args[]) { System.out.print(args.length); }
Strengths of Java’s main method
The syntax and semantics of the main method in Java provides a variety of important benefits:
- It is explicitly clear where the thread of execution will start.
- The syntax is friendly to any developer familiar with a C-based language.
- Java’s main method ensures no unguarded code is ever executed on import.
- Command-line arguments can be easily passed to the Java program at runtime.
- The construct is simple, elegant and easy for new developers to understand.
So how does Python achieve the same functionality? It can’t. But it tries.
HelloWorld application in Python
In an attempt to achieve the same functionality as Java’s main method, Python uses the infamous syntax: ‘if underscore underscore name underscore underscore equals equals underscore underscore double-quote main double-quote underscore underscore colon‘. Here’s what that looks like:
import sys
if __name__ == "__main__":
sys.stdout.write('Hello World')
To print the number of arguments passed into a Python program, as the Java program accomplished above, this is the code:
import sys
if __name__ == '__main__':
sys.stdout.write(str (len (sys.argv) ) )
Notice that the first Python example uses double quotes around __main__, while the second uses single-quotes.
Such coding inconsistencies are ubiquitous in Python codebases. It’s actually considered a feature of the language.
Python’s helper functions
The example above uses the core Python APIs.
To hide the complexity and verbosity of the language from new users, Python provides developers a Potemkin village of built-in functions to help with onboarding. One of those functions is named print.
We can rewrite the code above to use Python’s façade APIs, as follows:
import sys
if __name__ == "__main__":
print((len (sys.argv), end='')
Java and Python compared
Compare Java syntax to Python for the use case of developing a standalone application with a well-defined entry point, and these are the takeaways:
- Java is less verbose.
- Python is more cryptic.
- Java is more feature-full.
- Python syntax is inconsistent.
- Java doesn’t require facade functions.
Java’s psvmsa entry method
Python developers might point out that prior to Java 21, writing a Java application was significantly more verbose. That’s true.
Java’s psvmsa syntax — public static void main String args — has always been a source of ridicule.
Historically, one would write a standalone Java program as follows:
public class HelloWorld {
public static void main(String[] args){
System.out.print("Hello World");
}
}
The psvmsa syntax is by no means as cryptic and esoteric as Python’s name equals main monstrosity, but Python is not the high-bar to which Java language architects hold themselves.
Java has always had a high standard for simplicity and ease of use.
Many critics felt the psvmsa construct was one instance where the Java language fell short. That’s why the language architects have changed it.
One of Python’s fundamental flaws
However, Java’s traditional syntax, which wraps the main method inside a Java class, actually serves a higher purpose. The syntax prevents execution of any code defined outside of a class. It will not allow unguarded code to execute unknowingly and infiltrate your servers.
One of the most serious flaws of the Python language is that even if a developer puts code inside the name equals main block of code, nothing stops the execution of additional code scripted inside the file.
That means any unscrupulous developer could write top-level code in a Python file, and that code would automatically execute as soon as the component is imported.
I’ll repeat that: Code written outside of a method in a Python file can execute on import.
The file never needs to be called, nor an instance of the component created. Simply link to the file with an import statement, and that’s enough to fire off trojans, script kiddies and viruses that a disgruntled employee may have hidden inside.
The dangers of Python’s unguarded code
The Python community acknowledges this menacing unguarded code security flaw, but there is no standard way to protect a system against it, except to simply trust every developer in the world to be honest and good.
In contrast, it has never been possible for Java classes to run unguarded code.
Security and resiliency have always been a top priority for Java language architects, and the syntax of the language reflects that.
Java versus Python analysis
Some Python supporters complained that an honest comparison of Java’s scripting features against Python’s scripting features would be unfair to the snake. Compare standalone applications written in both languages, they believed, to give Python a better chance to compete.
In the end, an unbiased, head-to-head comparison of the two languages in this context demonstrates that:
- Java is less verbose than Python.
- Java syntax is more familiar to experienced developers.
- Java is less cryptic than Python.
- Java is more secure than Python.
Most importantly, Python fails to achieve what was the actual goal: Define a program with a single, well-defined entry point.
Python simply can’t do that, as the existence of the cryptic name equals main construct doesn’t actually forbid other top-level code from running.
Additional axes of Java vs. Python comparisons
Of course, the manner in which a language supports REPL scripting or addresses the development of a standalone application are only two dimension upon which to judge a language.
There are many other important axes upon which to compare programming languages. In future articles I will do exactly that, as we compare Java versus Python in terms of several factors:
- Multithreading support across cores.
- History of backwards compatibility.
- Runtime performance and energy use.
- Community-based vs dictatorial stewardship.
- Common operator support (++ and switch statements).
Only when we take into account the totality of a programming language can we properly and fairly assess its suitability for enterprise development.