Java vs Python: Application code comparison

I recently wrote an article that 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 that a fairer, more balanced comparison would 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.

Apples to applets comparisons

I’d actually be happy to compare Java Applets to any of Python’s browser based capabilities.

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 one up as a Java win.

However, the comparison between Java versus Python in terms of the development of a standalone application with a single entrypoint 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, but it is also flexible enough to support a myriad of enterprise use cases, including the need to pass command-line arguments to the program.

To achieve this common requirement, a Java program’s main method simply references an array of Strings.

For example, to output the number of arguments passed into a standalone Java app, the code would look like this:

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.

Java 21 main method

Java 21 introduces a new, succinct main method as a preview feature.

HelloWorld application in Python

In an attempt to achieve the same functionality as Java’s main method, Python uses the infamous ‘if underscore underscore name underscore underscore equals equals underscore underscore double-quote main double-quote underscore underscore colon‘ syntax as follows:

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, the code would be:

import sys

if __name__ == '__main__':
  sys.stdout.write(str (len (sys.argv) ) )

Notice that in the first Python example, double quotes are used around __main__, while the second uses single-quotes.

Coding inconsistencies like these are ubiquitous in Python codebases. It’s considered a feature of the language.

python vs java applications

Python requires esoteric boilerplate code to isolate entry-point code.

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 of built-in functions to help with onboarding. One of those functions is named print.

The code above, re-written to use Python’s façade APIs, would look as follows:

import sys

if __name__ == "__main__":
  print((len (sys.argv), end='')

Java and Python compared

When Java syntax is compared to Python for the use case of developing a standalone application with a well-defined entrypoint, the takeaway is that:

  • 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

Many Python developers will point out that prior to Java 21, the only way to write 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, a standalone Java program would be written as follows:

public class HelloWorld {
  public static void main(String[] args){
    System.out.print("Hello World");
  }
}

The psvmsa syntax is by no means as crpytic 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 when it comes to 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 makes it impossible for any code defined outside of a class to execute. It protects against unguarded code executing unknowlingly and infiltrating your servers.

One of the most serious flaws of the Python language is the fact that even if a developer puts code inside the name equals main block of code, there is nothing that stops additional code scripted inside the file from executing.

That means any unscrupulous developer could write top-level code in a Python file, and that code would automatically get executed as soon as the component is imported.

Yes, code written outside of a method in a Python file will get executed on import.

No call to the file ever needs to be made, and no instance of the component ever needs to be created. Simply linking to the file with an import statement is enough to fire off trojans, script kiddies and viruses that a disgruntled employee may have hidden inside.

The dangers of Python’s unguarded code

Acknowledged as the ‘ungaurded code’ flaw in the Python community, there is no standard way to protect a system against this menacing security flaw, other than 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 resiliancy has always been a top priority for Java language architects, and the syntax of the language reflects that.

Java verus Python analysis

An honest comparison of Java’s scripting features against Python’s scripting features was said to be unfair to the snake, and that comparing standalone applications written in both languages would 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 the actual goal which was to 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 a language can be judged.

There are many other important axes upon which programming languages should be compared, and in future articles I will do exactly that, as we compare Java versus Python in terms of:

  • Multithreading support across cores
  • History of backwards compatibility
  • Runtime performance and energy use
  • Community-based vs dictatorial stewardship
  • Common operator support (++ and switch statements)

It is only when the totality of a programming language is taken into account that a fair analysis of its suitability for enterprise development can be properly assessed.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close