-2

I have a doubt with the management of the objects using java or c++.

The case is, in c++, when you want to create a dynamic object, one that survive more than the block scope on where it is created, you have to do a new and you will receive a pointer. Otherwise, if you just want to use this object in the block scope, you don't need to create it using new...

But in Java, you always have to create them using new, because if not, the object is null and you can use it.

Why is that? Is it just how it works?

Thanks

Frion3L
  • 1,502
  • 4
  • 24
  • 34
  • 1
    Why? Because Java and C++ are two different languages with entirely different object models and different rules. (They both happen to have a keyword `new`, with different meanings.) – aschepler Mar 15 '13 at 14:44

5 Answers5

2

The best analogy I can think of, is that all types in C++ behave somewhat like primitives in Java. If you declare a primitive in Java, you don't have to use new, you can just use the variable right away. But such a primitive, much like most objects in C++, will only survive the current scope. In C++, if you want an object to exist outside of the current scope, you need to tell this to your compiler, because it will have to allocate memory on the heap instead of the stack. You can do this by using new. In Java, all objects (save primitives) are allocated on the heap, the only data on the stack are references to heap memory, and primitives. Therefor, in Java, all memory allocations are done using new.

The above is a simplification of the actual memory management in Java. For a more thorough discussion on stack/heap memory regarding primitives, take a look here.

Community
  • 1
  • 1
JSQuareD
  • 4,641
  • 2
  • 18
  • 27
  • 1
    There is a good explanation of heap vs. stack/primitives vs. objects here; the above is a bit of a simplification. http://stackoverflow.com/questions/3646632/does-the-java-primitives-go-on-the-stack-or-the-heap – James Mar 15 '13 at 14:57
  • Well, that's what I hinted at with "*somewhat*". ;) But thanks for the link, I'll include it in the answer. – JSQuareD Mar 15 '13 at 15:01
1

This difference is becaase Java is using a garbage collector for memory management. Since the garbage collector automatically deallocates objects when their scope ends (and it has no reachable reference), there is no need to have two different methods for creating objects.

You can say that objects in Java automatically behaves like objects in C++ which are initialized without new, in that you don't have to think about deleting them.

ghdalum
  • 891
  • 5
  • 17
0

Basically, that's just how it works. Once the new keyword is used then the Object is created and popped onto the heap. If you do not reference the object outside of a method then it will be automatically reclaimed by the garbage collector. I suggest that you do some reading around the basics of the Java heap and garbage collection to get a better understanding. There are plenty of resources out there. I always recommend the head first books for new comers.

James
  • 1,541
  • 12
  • 25
0

In C++, anything can be allocated on a stack (which is what happens when you say

ObjectType o;

in C++.

In Java, only primitives are really allocated on the stack. Objects are never on the stack (It's just how it is). When you say

ObjectType o;

in Java, no object is allocated, only a "variable". A variable can have a reference to an object, but at the moment it has none. In essence, it's the same thing as saying

ObjectType *o = NULL

in C++.

In order to actually allocate an object for this reference to refer to, you have to use new in Java.

0

The case is, in c++, when you want to create a dynamic object, one that survive more than the block scope on where it is created, you have to do a new and you will receive a pointer.

The new operator in C++ allocates space on the heap. The heap is where the larger part of the main memory is. If you do this, you are responsible for freeing that space when you're done with it using the free operator.

Otherwise, if you just want to use this object in the block scope, you don't need to create it using new...

When you declare variables in C++, memory is allocated on the stack. The stack is where local data is stored and everything you push(add) on it while executing a function will be automatically popped (removed) when the function returns. The stack is usually a lot smaller than the heap, but there are advantages to using it: you don't need to worry about memory management, it is faster, etc.

But in Java, you always have to create them using new, because if not, the object is null and you can use it.

When you declare variables in Java, they are again stored on the stack As you know, you don't call new on primitive data types (e.g. int i = new int(3);). When you do something like Object x; you declare that x would be a reference to an object of type Object. However, you do not assign a value to it, so the reference is null (not the object, because there isn't one).

The new operator in Java, roughly speaking, allocates space on the heap, calls the constructor of the object that it is invoked on, and returns a reference to the constructed object. The difference with C++ is that you don't need to free the object yourself - there is a garbage collector. In essence, what it does is that it monitors how many references point to an object, and if they go down to zero it deletes the object automatically.

So when you do

Object y = new Object();
x = y;
you will get two references (x and y) pointing to the same object. When you have a function call like this
Object foo() {
    Object y = new Object();
    return y;
}

void bar() { Object x = foo(); ... }

in the ... part of bar() you would have the reference x, pointing to the object created in foo(). Since foo has returned, the y reference has been freed, thus there would be only one reference to this object in the ... part of the program. If you don't copy the x reference anywhere in bar and bar returns, then there would be 0 references to the object, and the garbage collector would collect it (although not immediately).

-Stan

stanm
  • 3,201
  • 1
  • 27
  • 36