Comparison Summary: Java Hanoi vs. C++ Hanoi
-
In Java, we use an import statement, whereas in C++ we use a
#include statement.
as opposed to
Java's java.lang.* library is already automatically found, so
it is not necessary to import it.
-
In C++, main is a free function.
In Java, it is a static method
of a class. Being static, it is tied to a class, but not to any particular
object of a class. This is as close as Java gets to free functions.
-
In C++ it is good programming practice to make the main function return
an int when complete.
In Java, we always make the main function a
void function. In fact, our declaration should always read
public static void main(String args[])
-
In C++ it is good programming practice to put a function declaration (or
prototype) before defining or using the function. In Java,
this is not necessary.
-
In Java, we use
System.out.println("text");
as opposed to
-
In C++, we have a useful utility for keyboard input, cin.
In Java,
this is not built in because there are much cooler ways of getting user
input. However, it is sometimes useful to get input the boring keyboard
way, so we have introduced a Keyboard class which you may use
for getting keyboard input.
-
Variables in Java are always references (excluding primitive types).
In C++ we could have
and hanoiGame would
be an instance of the TowersOfHanoi class.
In Java, that same line
just creates a reference to a TowersOfHanoi object. To construct
the TowersOfHanoi object, we need to call new.
TowersOfHanoi hanoiGame = new TowersOfHanoi();
-
if statements and many general statements will be very similar
or the same.