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.
(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.
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.
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:
The main function in Java must appear inside a class. In C++, main is a stand-alone function. Java, however, does not have the idea of a stand-alone function. All functions in Java must be members of a class.
The main function in C++ has an int return type (used to supply a status code indicating to the operating system whether the program thinks that is executed correctly (return 0) or crashed (return any non-zero value).
The main function in Java has a void return type and does not return anything.
The C++ main function gets two parameters. The second is an array of strings and the first in an integer indicating how many things are in the array.
In Java, you will shortly see that all arrays “know” their own length, so there is no need for a separate parameter just to say how many things are in the array. Hence the Java main function gets only the array as its parameter.
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.
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.