Variables differences in C++ and Java

Hi, while going through the differences between the c++, prolog and java, I came to see a question asking the "variable differences in c++, prolog and java, and how does it affect garbage collection"

How does variables make differences in garbage collection? any ideas would be of great help.
Last edited on
C++ doesn't have garbage collection.
Correction: C++ doesn't need garbage collection. However, there is a standard ABI for garbage collection defined in the standard, if any compiler chooses to implement it.

As for what the variables make differences for, I presume its to do with how they are represented. From what I know, Java's variables are all references to variables, and the garbage collection cleans up once no more references to the variable is detected.

C++, however is by default on the stack, which requires no garbage collection, or using a reference or pointer. References are the same thing as with Java, but with C++ using pointers you can change a pointer and then change it back and still want the variable to be there. For example, using Java-style garbage collection:
1
2
3
4
5
int* i = new int;
++i;
// the new int is removed by garbage collection here
--i;
*i = 5; // this should work, but doesn't - the memory has been disallocated 


As for Prolog, never used it, don't know anything about it.
Last edited on
The main issue I have with Java is that I never know when I am dealing with a reference to the original or a copy.

When making GUIs with javax.swing, I am able to pass parents into the constructor of my children as implementations of interfaces and I am able to modify the original parents. That makes me think that Java uses references by default.

However when I do this simple example, the original objects aren't modified. Therefore I just swapped the copies:
1
2
3
4
5
6
7
class SomeClass {
  public static <T> void swap(T a, T b) {
    T c = a;
    a = b;
    b = c;
  }
}


I find Java to either be un-intuitive, or just vague. Being vague is a terrible thing for a programming language.

Don't even get me started on the problems with generics (Java) versus templates (C++).
Last edited on
First difference between java and C++ is that all objects in java are either primitives or reference to other objects. C++ has one more called pointer which is actually analogous to java's reference objects.

As primitive variables in both C++ and Java are allocated on the stack, there is no need to have an explicit method of freeing the memory they contain as this is automatically done when the scope of the method the primitives are declared in, has finished.

However, java's automatic GC (garbage collector) counts references to an object and when this counter gets to zero, the object is now ready to be garbage collected. So when an object is allocated with the `new` keyword, it's reference counter is set the number of references pointing to the object. So if you initially declare an object:

1
2
String mystr = new String();
// reference count of mystr is 1 so this object is not garbage collected 


Or this:
1
2
3
new String()
// reference count of this object is 0, so as soon as the constructor is 
// finished, the object is garbage collected 


In c++, there is no such thing as garbage collection, but as of c++11, smart_pointers were introduced to the language and these implement the reference counting of java gc.
http://en.cppreference.com/w/cpp/memory



@Stewbond
In java function arguments are pass by value, but since the values are either primitives or references, this doesn't affect performance. So in your swap example, your swap function simply received variables of type `T` which is a copy of the reference to the actual objects you are working with. So your swapping has no effect on the original reference.
Last edited on
In C++ you deal directly with the type you intend. In Java you deal with either a primitive (including a reference) or an object through a primitive reference, and the line is blurred in many many cases.
Last edited on
since Prolog was brought up, a Prolog variable is essentially a pointer, which starts off null and is pointed and repointed to different terms as the runtime makes progress and backtracks looking for the solution. This isn't directly related to garbage collection, which is an optional (but common, especially in older prologs) implementation detail.
Topic archived. No new replies allowed.