• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java compared to C++  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I have noticed one huge difference between c++ and Java when it comes to storing objects in memory I'm going to compare C++'s stl's map and Javas container's hashmap

in C++ when you create an object of a map and set it equal to something it creates a new object for example person with it's default constructor then uses the overloaded assignment operator or copy constructor depending how you add it to the map,so essentially the map object which you created and the object you assigned it to are two different objects but in Java this is different when we add an item to a hashmap that item's reference is copied and placed in the map so if you change the reference in the hashmap that is pointing to the object aswell changes are obviously made to both because the are referring to the same object,where as in c++ if you change one objects data such as a member it will only change affect one object as two different objects exist in memory,

I'm just wondering why the difference? why does Java copy references to it's containers such as hashmap would it not be more efficient to use a copy contsructor and have two different objects in memory instead of having different references pointing to the same object?

here is some example code

c++



Java



sorry about recycling code I thought it would save a little time in the first example I use a map from c++ in the second I use a hashmap which stores bodies from the solar system and a hashset which stores planets both copy references


thanks
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . I have noticed one huge difference between c++ and Java . . .
In the early days of Java®, the similarity of the syntax to C++ encouraged people to try Java® out. Nowadays, I think it obscures the differences between the languages. That is a bit like your noticing a huge difference between English and German They are different languages and work differently
in C++ when you create an object of a map and set it equal to something it creates a new object for example person with it's default constructor then uses the overloaded assignment operator or copy constructor depending how you add it to the map,so essentially the map object which you created and the object you assigned it to are two different objects
That strikes me as strange. You have a Map which stores a copy of the original object? Is that as K or V? That is the behaviour of the map, not of the language, if I have understood it right. Do you want to put object A into your map as a V and later on get object B back, even if the two look the same?
but in Java this is different when we add an item to a hashmap that item's reference is copied and placed in the map so if you change the reference in the hashmap that is pointing to the object aswell changes are obviously made to both . . .
No, you can't change the reference. At least not in Java® which doesn't support pass by reference. It is a standard feature of object‑oriented programming however, that it is possible to change the state of an object. The functional programming people don't like such side‑effects, but that is a different story and I haven't got the time to say any more now.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell great answer thanks =)

That strikes me as strange. You have a Map which stores a copy of the original object? Is that as K or V? That is the behaviour of the map, not of the language, if I have understood it right. Do you want to put object A into your map as a V and later on get object B back, even if the two look the same?


sorry I should have said V it stores the value of the object the map object isn't the same,but the map object contains a copy( different object ) as it's value and an int as it's key,

No, you can't change the reference. At least not in Java® which doesn't support pass by reference. It is a standard feature of object‑oriented programming however, that it is possible to change the state of an object. The functional programming people don't like such side‑effects, but that is a different story and I haven't got the time to say any more now.


yeah everything in Java is passed by value,so this is the reason the references are copied? well not copied but a new reference is created and the hashmap stores a reference to the object so when you change the object such as a value in a hashmap the change affects the original object ?

thanks
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I see how a copy would be more efficient.
In C++ the default assignment operator for a class essentially has to build a new instance, whereas the Java assignment simply assigns the reference, which has to be both quicker and save space (you don't have two objects).
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very true Dave,

but wouldn't having two separate objects in memory have some benefits?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Hi Campbell great answer thanks =) . . .
That's a pleasure . . . and I had to knock that reply together in 5 minutes before going elsewhere.
and an int as it's key
An int? That isn't my idea of a hash table. A hash table should use the hash code of its “K”s to find the index, not an int. Are you sure you have read the details right?
. . . this is the reason the references are copied? well not copied but a new reference is created and the hashmap stores a reference to the object so when you change the object such as a value in a hashmap the change affects the original object ?

thanks
The reference is actually copied. Yes, you are storing a reference to the same object, not a clone of it.More details of the of() method here. Java9 only.
Yes, you store (copied) references to each “K” and each “V” in such a manner that you can get access to the original objects, not a copy (as stated previously), well the “V”s at least. Yes, it is usual in OO programming to be able to change the state of an object. That is why you should always use immutable types for “K”s in a Map (or elements in a hash set).
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . wouldn't having two separate objects in memory have some benefits?
Let's try some pre‑Java8 style code:-If you have copies of objects in the Map as its “V”s, can you rely on that little bit of code showing this?
Counter: 0
Or will it tell you how many times you are in that List?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this sort of code will display 0?
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ you could create an object on the stack and add it to a collection. In Java you cannot simply declare an object and have it instantiated, you have to explicitly construct it via operator "new" or its equivalents.

The real issue here is that if you create an object on the stack, add it to a collection, then exit the function or block that created the object, the original copy of the object is destroyed almost immediately - it isn't even garbage collected, because the stack grows and shrinks linearly. So it's essential that the object should be copied or the collection can end up with garbage in it.

That said, it's been a while since I used STL and I seem to recall that there were references being used instead of object copies. Back then the semantics were driving me crazy, though.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!