Java Class .vs. a C/C++ struct |
|
In its simplest form a class is just a struct. For example, the simple class |
class Point{ public int x, y; }is nothing more than the C/C++ struct
struct Point{ int x, y; };
|
There is a small but important difference in their use in C/C++ and Java, however. In both cases they are templates that are used to declare variables of a new type. But the C/C++ declaration |
Point p; p.x = 3; p.y = 5;allocates an actual struct in memory to which values can be assigned to the various components, whereas the Java declaration
Point p; p = new Point(); p.x = 3; p.y = 5;of the Point p just allocates storage for a pointer (called a reference) to a struct. The statement