Programming Windows: Hello, Java (Premium)

James Gosling never intended to write a new programming language. Instead, he was trying to bring interactivity and interconnectedness to an emerging market for TV set-top boxes in the early 1990s. But those systems ran on a variety of different hardware, which made traditional programming languages like C++ inefficient. The code would need to be recompiled and, in many cases, rewritten to some degree to run on all the available systems of the day.

So Gosling took a different path and created a platform-independent runtime environment that abstracted the underlying hardware. Then, he created a new object-oriented programming language, based largely on C and C++, that targeted the new runtime environment. This platform, collectively called Oak, was later renamed to Java. And its compiler would create software code, called bytecode, that ran within the Java runtime environment and was thus identical across platforms. At runtime, the Java Virtual Machine (JVM) translated the bytecode into native code for the underlying hardware.

As a more modern language that didn’t need to maintain compatibility with the C programming language, Java offered some key benefits over C++ and other attempts at bringing OOP methodology to C. It was far more readable than C++, even for non-programmers, and used a more elegant syntax. And while C++ required external libraries even for basic objects like strings, Java simply included this capability.

To see the changes in action, let’s compare a basic Hello, world application written in C, C++, and Java.

I previously used the following example in C, taken from book The C Programming Language by Brian Kernighan and Dennis Ritchie.
#include <stdio.h>

main()
{
   printf("hello, world\n");
}
This is mostly straightforward, I think. But the C++ version, found in Bjarne Stroustrup’s The C++ Programming Language, is less readable, thanks to some non-obvious syntax.
#include <iostream>

int main()
{
   std::cout << "Hello, World!\n";
}
So what does the Java version of this application look like? It’s a bit wordier than the C++ version, but is, I think, far more readable, even to a non-programmer.
class hello
{
   public static void main(String args[])
   {
       System.out.println("Hello, Java!");
   }
}
As with C and C++ , Java utilizes a method (function) called main as the application’s entry point. And even though the syntax looks a bit different, each version works similarly in that they accept some number of parameters (explicitly in Java, an array of strings called args) and return some value (void in Java, and an integer in C/C++). Java also introduces a bit of OOP padding in the form of the class (which is a template for an object).

But the result of running the application is, of course, predictable, and is essentially identical to the previous C and C++ applications.
Hello, Java!
But where the C and C++ compilers used with t...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC