Hello World: Commentary

Steven Zeil

Last modified: Aug 19, 2016
Contents:

You can advance through multi-page documents like this one by clicking on the appropriate icon at the top or bottom of the page or, on most machines & browsers, by using the left/right arrow keys on your keyboard.


Many of the readings for this course consist of sections of the Sun/Oracle Java Tutorials accompanied by my own commentaries upon them.

You will recognize these when you see this symbol: , usually in a section header. Clicking on that icon will open up the related section of the Tutorials in a separate browser tab/window. You can try this out on the next page.

Some people will prefer to read all the way through a tutorial section first, then go through my commentary. Others may prefer to arrange their tabs/widows so that the tutorial and my commentary are side by side, and read through them together. To facilitate this, I will generally try to keep my own section headings identical to those of the tutorial section that I am commenting upon. In some cases, I may have empty pages of commentary, just to help keep the synchronization consistent, and also so that you can use the “email” button at the bottom of my own pages to ask both about my comments and about anything you have read in the related section of the Tutorials.

1 A Closer Look at the “Hello World!” Application

(Click on the Java icon above to open up or synchronize the related section of the Tutorials.)

It probably won’t escape your notice that comments in Java are formed in the exact same way as in C++. In fact, the whole program example here will seem almost, but not quite like C++.

You’ll see that a lot. With the exception of some rather minor differences, you already know the syntax of the Java programming language. You’ll need to learn, for example, that you don’t put semicolons at the end of a class in Java, that “->” and “::” from C++ become “.” in Java, but you’ll find that there are far more similarities than differences in the way that C++ and Java programs are put together.

A bit more dangerous, however, is that you’ll need to learn how sometimes things that look similar in C++ and Java will not mean the same thing. It’s very often possible to write code that would compile under either language, but which might produce very different results when compiled.

1.1 The HelloWorldApp Class Definition

A good example of that kind of “looks the same but means something different” structure comes up right away in this example.

In C++, you are used to separating declarations of classes from the definitions of their members. Often, the declaration would be in a header file (e.g., helloworld.h):

#ifndef HELLOWORLDAPP
#define HELLOWORLDAPP

/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public:
   static void greet();
};

#endif

which declares a class whose members would be defined in a separately-compilable file (e.g., helloworld.cpp):

#include <iostream>
#include "helloworld.h"

using namespace std;

static void HelloWorldApp::greet()
{
   cout << "Hello World!" << endl;
}


int main (int nargs, char** args)
{
   HelloWorldApp::greet();
   return 0;
}

Would you ever see a class declaration in which the function bodies appeared immediately within a declaration?

#ifndef HELLOWORLDAPP
#define HELLOWORLDAPP

#include <iostream>

/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public:
   static void greet()
   {
      using namespace std;
      cout << "Hello World!" << endl;
   }
};

#endif

Actually, yes, that is a fairly common pattern in C++. But, in C++, a function body that appears directly within a class declaration signals that the function is inline. There’s no such implication in Java. In fact, the very notion of an inline function is pretty much irrelevant to Java. To see why, and to understand why Java programs are not split into separate sets of header and compilable files, we need to talk about how Java programs get compiled.

1.2 The main Method

Both Java and C++ programs begin execution at a function named “main”. In both languages, the main function receives, as its parameters, an array of any arguments (strings) supplied on the command line when the program was launched.

The differences to be aware of:

Basic I/O

The Java System class has data members in, out, and err, which correspond to the C++ cin, cout, and cerr. Java does not use the overloadable operator << to do output, however. It uses the member function print(...) to print a string and println(...) (“print-line”) to print a string followed by an end-of-line marker. Although this may seem restrictive at first, we will later see that converting things to strings for the purpose of output is both common and fairly easy in Java.

2 Questions and Exercises: Getting Started

As noted earlier, I may provide pages without any particular commentary to maintain synchronization and so that you can use the “email” button at the bottom of my own pages to ask about anything you have read on a related page of the Tutorials.