• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NOT an array of pointers

 
Greenhorn
Posts: 4
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the pointers vs. references thing, and purely object-oriented vs. not, and with those things in mind what I wonder is... for example, if I wanted to write an emulator in C++, I would set up an array of function pointers, and the code would assign each instruction a number, and we would do a little:

and be done with it.

so the short version of my question is "how do we do this in Java?"

the long version is that I've looked around on google somewhat, and seen mention of... creating some kind of base class, and creating derived classes for each instruction, and having an array of references to instances of those derived classes? I think? and then calling a common method from there... and that kind of makes sense to me, but I'm having a bit of trouble imagining what that really looks like in code, because anything I imagine seems very cumbersome.

are we looking at something like...

?

or maybe...


or maybe I'm completely off-base! again, this all seems rather cumbersome compared to the C++ way, once you consider all the Java classes you have to make, just to contain each little instruction function (it would seem preferable to have an array of references to member functions of the virtual machine object, if possible). all this makes me think I've got something all wrong from the very beginning... or maybe it's a matter of "the best tool for the job" and Java is not the best tool in this case, requiring something more cumbersome, however I'm more inclined to believe that the fault lies in my lack of experience, rather than in the Java language itself :P
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Hendel wrote:array_of_something_references.get(instruction_index).DoYourThing(argument1, argument2);


I don't understand your question. The above Java code is very similar to the equivalent C/C++ code. In your examples, you added a bunch of fairly ugly code to initaialize the "array_of_something_references" but you left out the parallel code to initialize it in C/C++. Its probably a little more cluttered in Java, but who looks at once-only code anyway?

If I was doing something like this, I'd use the Google Guava Collections classes, this begs for an ImmutableList, since you never would dynamically change the instructions you are emulating.

Java is less terse than C, that is not a bug, its a feature.
 
Ben Hendel
Greenhorn
Posts: 4
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right! that line, for executing the code, is quite similar. populating and accessing that array is not the issue, really.

my understanding for Java is that I will wrap each instruction function/method/whatever in its own class. but it seems like those instruction functions should be member functions of the virtual machine, not some external class whose only purpose is to hold a single function (and will need to be passed a reference to the virtual machine it's operating on...). right: and that's the biggest thing, I guess: if I'm sending a reference to the virtual machine I'm working on for all these instruction functions, doesn't that indicate to me that these functions should be member functions of the virtual machine, so that they already have access to it? they're going to need access to a bunch of what would be private data (the virtual machine's memory)... to make this data public for the sole purpose of allowing what should be member functions to access them... that's when I start to feel like I've done it all wrong :P and I guess I could have a huge switch block, but that's exactly what an array of function pointers wants to replace.

I hope that all made sense >_> I can try to explain another way, if that was too jumbled.

I guess what this really boils down to is not C vs. Java, but non-OO vs. OO.
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are *no* pointers in Java. Its a feature that was carefully considered when Java was invented. Java doesn't have pointers and its good, but it is different than C (and your idiom is really just C, one of my most serious problems with C++ is that it stayed too compatible with C, rather than fixing the many problems that C has).

You can access methods indirectly in Java using Reflection, its a bit ugly, but its very do-able. But your idiom is not very OO, it would be better style to have a base class that has a "evaluate(arg1, arg2)" method, and have a bunch of separate classes named Add, Subtract, Branch, etc. that subclass from the base and are executed.

There are probably other idioms in Java that can do essentially what you want, but there is no exact equivalent to the C idiom.
 
Ben Hendel
Greenhorn
Posts: 4
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps the best question is, then... can there be a "Vector of references to methods?" (to use all-Java language.) is that what "Reflection" allows me to accomplish? (is "Reflection" a Java keyword or class that I can google?)
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vectors are deprecated. New code should use Lists such as ArrayList or TreeList.

Reflection is a whole technology that was added to Java about a decade ago. Google "Java reflection" with the quotes and you will see a ton. Essentially, reflection lets your code query other code, find the list of methods, call them, etc.
 
Ben Hendel
Greenhorn
Posts: 4
jQuery PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fantastic! Reflection seems to be the key thing here! or, rather, the Method class, specifically. we can have an "ArrayList of Methods", and that is definitely the closest analog to an "array of function pointers" that I've seen, both conceptually, and in terms of what can be accomplished with it.

thanks!
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, let's say you have a collection of strings that name your methods, and you use java reflection to access those methods, and you want to put all those accessible "thingies" in an array or a list.

Guess what? They're all objects! Meant to hold a reference to a method! In other words, looked at in one way, you have gone around Robin Hood's barn to avoid having an object that "just holds a method", and you do it anyway!

As has already been stated, there is no direct equivalent of this exact idiom in Java, because Java doesn't have the concept of a pointer to a method (as opposed to an object that represents a method -- java.lang.reflect.Method).

And you were headed down the right track in thinking about "OO vs. non-OO" -- you think of all those objects as "cumbersome", when in fact they're just the way to get the job done.

C is a structured assembler; C++ started out as a set of C macros and never got away from being an extension of C. One has to expect that it has ways of manipulating things at a lower level than Java -- with its OO nature and (originally) a target of simplicity -- just isn't going to have. And if you just can't get your head around Java's constructs, then stick with the things that need to be done in C and C++ -- there's plenty of programming to be done in whatever languages.

rc
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat Farrell wrote:Vectors are deprecated. New code should use Lists . . . ..

[pedantic mode]Not quite. Vector is not deprecated, but most people regard it as legacy code, but it does now represent a List.[/pedantic mode]
You should usually use ArrayList in preference to Vector.
 
reply
    Bookmark Topic Watch Topic
  • New Topic