Don't confuse Java new with C++ new. I don't know Java too well, but new in Java does something else. In Java, variables with primitive types (int, boolean, float, etc.) can be simply declared:
int x; boolean b; . Same for C++. However, in Java, when you declare a variable with a non-primitive data type, you aren't actually creating an object of that class, you're creating a reference to it (similar in certain aspects of it's behavior, but not the same as, a pointer). This is the only way to declare objects of classes. The reference must be filled with an actual object using new. Similarly, primitives can't be declared as references (or so I believe). So this:
x = new int; will never happen.
In C++, variables of any type, primitive or not, can be directly declared:
int x; myclass y; , and you can declare a pointer to any type:
int* a; myclass* b; . Now, a and b don't hold objects (nor will they ever). They hold the memory address of objects.
One thing you can do is this:
a = new int;
b = new myclass;
Here, new creates a variable somewhere in memory and returns its address. Now, when you create a variable with C++'s new, you have control of when that object is deleted. In Java (again, I believe), all variables are deleted at the end of scope, regardless of whether they were created by new. In C++, variables created by new continue to exist in memory until you manually delete them with the
delete keyword.
Other things we can do with the pointers:
Now, *a and x are the exact same integer, just as *b and y are the exact same object of myclass.
We can overwrite these though, which I believe (deja vu?) you can't in Java:
int myInt = 5;
a = &myInt;
*a = 6;//myInt and *a are both 6
myInt = 7;//myInt and *a are both 7