Saturday, May 31, 2025

Polyglot - Hello World in 7 Languages

To those who love to learn languages, I understand the appeal. Being able to communicate with a wide array of people you might meet is an empowering feeling. New ideas are unlocked that had been hidden away in previously inaccessible texts.

Similarly, in the world of programming, building familiarity with other programming languages exposes you to new ideas and gives you a deeper understanding of how programs work. Possessing broad capabilities prevents becoming boxed in with opportunities limited to a narrow set of technologies - some of which are undoubtedly becoming obsolete.

To begin to get a flavor for how today's most popular programming languages are in some ways similar while also distinct, we'll look at what it takes to run what is perhaps the simplest program in most languages: a program that says Hello World.

Here are the arguably top seven languages in today's programming circles:

  1. Python
  2. C
  3. C++
  4. Java
  5. C#
  6. JavaScript
  7. Go

For each, we'll take a quick look at a program that prints Hello World along with the tooling and commands required to run each sample. I'm building each of these on a Linux desktop using command-line tools. (I prefer to avoid IDEs if possible, thoughts for another time.)

 Lets hit it.

 Python

A longtime favorite of mine, I began exploring Python years ago to interview for a job and was instantly drawn to it's clean and straightforward syntax. It comes as no surprise that its popularity has steadily grown. The Python interpreter comes preinstalled in many operating systems, making it perhaps the simplest language in today's list to try out.

Here is hello-world.py:

 def main():
  print('Hello World')
  return 0
 
 
 
if __name__ == '__main__':
  main()

To run it, we can execute the command

python hello-world.py

Now experienced Python programmers may point out, this is far from the simplest Hello World program in Python. It could be as simple as 

print('Hello World')

For the sake of consistency though, we wrap our code in a main function to match the conventions present in many other languages. Also, the __name__ == '__main__' check will allow code to be executed only if the module is being run directly. It will not execute if the file is being loaded as a Python module imported by another script. 

Onward to the next:

C

This was the first programming language I've learned, which I think has been a valuable way to start. Certainly the oldest language from our list, it's still widely used today. It's comparative simplicity sometimes requires extra work, managing memory usage stands out as a clear example, but it offers a greater degree of control. When carefully written, C can offer high performance and it's often the foundation for operating systems and other programming languages. Here's a Hello World program in C hello-world.c:

#include<stdio.h>
 
int main(void) {
  printf("Hello World\n");
  return 0;
}

The C compiler is often already available in an OS (Linux and MacOS at least) so it's easy to get started. We first compile the program into an executable, and then run it. The process can look like this:

gcc -o hello-world hello-world.c

./hello-world

Neat!

C++

Next we turn our attention to a highly popular language which extends C with the cleverly named C++. When I began learning C++ in high school (at the time it was the language used in the AP Computer Science exam), it's claim to fame was the addition of classes to support Object Oriented programming, a trend which every other language in this list continues (to at least some degree). It continues to be a popular choice for high performance applications and has a wider degree of helpful language features compared to the simpler C.

When it comes to our Hello World example, the differences appear relatively minor. This is hello-world.cpp:

#include <iostream>
using namespace std;
 
int main() {
  cout << "Hello World\n";
  return 0;
}

Compiling and running was similarly straightforward compared to C. The compiler was already present in the Linux OS I was using:

g++ -o hello-world hello-world.cpp

Java

I learned Java during my first programming internship and picking it up from C++ was fairly quick. It's claim to fame was the promise to "write once, run everywhere" thanks to it's unique approach to compile Java programs into a bytecode that could be run in an interpreter. This made it one of the few languages to offer a hybrid approach to running the programs, it was initially compiled and yet also interpreted.

Here's HelloWorld.java, pretty similar to what we've seen so far with one visible difference being that this program is declared as a class, which is traditionally required for all Java programs.

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


To run a Java program, you can use the java command which comes pre-installed in most Operating Systems. To compile the source code though, the Java Development Kit must be installed to run the Java compiler: javac.

javac HelloWorld.java 

java HelloWorld

Next up, a language that is in some ways similar to Java: C#.

C#

I recall learning about C# in college and trying it out a bit. Coming from Java, it felt very similar and this may have been intentional as the word on the street was that Microsoft had created C# as a competitor to Java when they were prevented from extending the language for use in their own applications. At the time, Java had taken the world by storm and C# brought similar ideas to the Microsoft ecosystem.

Here's a Hello World program in C#, hello-world.cs:

using System;
 
public class HelloWorld
{
    public static int Main()
    {
        Console.WriteLine("Hello World");
        return 0;
    }
}

When it comes to running the above code, the dotnet program can both compile the code and also execute the compiled program. To perfrom compilation though, the dotnet compiler requires that a project file describe the source code and the type of program to be created. Using an IDE like Visual Studio, the program file is often generated for you. Sticking to my approach though of using command line tools only, the same can be accomplished using the dotnet command when the following csproj file is in the same directory as our source code hello-world.csproj:

 <Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>
 
</Project>

With the project file in place, the program can be compiled and then run using these two commands:

dotnet build
dotnet run

Next, we'll change gears to something a bit different: JavaScript.

JavaScript

While similarly named to Java, the language has very little in common. The programming syntax looks like Java, C, really most of the languages we've looked at, but under the hood it works very differently. JavaScript was initially launched as a language to run inside of Web Browsers and so it has historically been closely aligned with web applications. I started learning it when I began creating web pages and for a time I was programming in it heavily professionally. Concepts like prototypical inheritance and functions as first class objects make it possible to construct logic and dynamic behavior in JavaScript which is difficult to express in other languages.

A few years back, the NodeJS project made it possible to run JavaScript programs directly in the OS instead of being limited to only run in a browser context. This Hello World example uses the node command to interpret the following hello-world.js program:

function main() {
  console.log('Hello World');
}
 
main();

As with Python, this example is slightly more complex than strictly needed, for consistency. This program could be as short as console.log('Hello World');. 

Running this JavaScript program can be executed with the node command. which I had to install on Linux.

node hello-wolrd.js

Now we're ready for our final language Go.

Go

This is the most recent language on our list and one that I've learned over the years at work. In some ways it blends the simplicity of C with the conciseness of Python. With it's similarity to two of my favorite languages I was instantly hooked. Some of the unique design goals I recall that motivated the creation and design of Go include simplifying parallel processing - thanks to a built-in co-routine feature called goroutines - and also making it easy to reason about the flow of complex programs - hence the elimiation of throwing exceptions in favor of functions returning multiple values, one of which would be an error indicator. 

Here's a simple Hello World program in Go, hello-world.go:

package main
 
 
import "fmt"
 
 
func main() {
    fmt.Println("Hello World")
}


The compiler / interpreter for Go was already present in Linux, and running the above program was as simple as executing this command:

go run hello-world.go

Fun!

In Closing

Looking at the languages and comparing how to do the same things in each can be a fascinating exercise that deepens understanding and appreciation for how unique each programming language can be. This has been a long time fascination for me and inspired explorations like how to write classes in C, how to load libraries dynamically in Linux, and differences in how closures work in different languages. Hopefully this quick survey of Hello World examples has been instructive and inspires new explorations.

While writing the same program in 7 languages isn't the most innovative endeavor, a nice side benefit is that I now have a development environment set up for each of these languages. Another stepping stone to bigger and more exciting ideas.