• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple object question  RSS feed

 
Benjamin Barkerissimus
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In c++ I can create a string variable. I can then use other functions seperate from that string to manipulate it. I only need one set of functions to manipulate any number of strings. It seems in Java I need to create a string object, which has built in methods for operating upon that string. I suspect I've got the wrong idea, but doesn't this mean that each string is enourmous, since each object contains not just sthe string data, but all the possible methods that could be used to operate upon the string ....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler writes all the code for the methods for a given object type, and puts them in a special place. then, when you run your program, you make your objects (String or otherwise). Each object is only as big as is needed to hold it's member data (slight lie there, but not much).

then, when you call a method, the JVM says "Oh, thats a <whatever> object. I know where to get it's methods", and goes elsewhere to find them - it doesn't look in the object itself.

This is true for ALL object types, not just Strings.

So basically, the methods are stored in one place, and the object (and member variables) are stored elsewhere. the JVM knows how to go from one to the other (and which ones are appropriate).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's more, the same is true in C++. The std::basic_string<char> class (typedefed to "string") in C++ has lots of methods in it, and you generally use both methods and free functions to work with strings. Just as in Java, there's only one copy of the code for the methods (well, unless your template processor sucks.)

Anyway, my main point is that C++ objects have methods too -- were you not aware of this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!