wood burning stoves 2.0*
The moose likes Java in General and the fly likes Function pointers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Function pointers" Watch "Function pointers" New topic
Author

Function pointers

Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
Hello to all Java Geeks
Those of you who are familier with C/C++ must have heard of function pointers.i want to know wheather such concept applies to java or not.And if it does then how to implement it.
Thanks
Shyam
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi
no, java doesnt have function pointers.
one intersting thing is, that you can get information about supported methods trough the java.lang.reflect package and the java.lang.Class objects.
have fun with it...
k
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Advice: Don't use reflection until you really have to...
Instead you typically would use a reference to an appropriate interface instead of a function pointer.
If you had a concrete example where you thought a function pointer would be handy, we could discuss the proper Java solution...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
Hi guys
Thanks for your advce
Actually I am designing a simulation system for 8051 microcontroller.I have implemented all the instructions of 8051 in the form of methods in the main CPU class .
Earlier I wrote the CPU class in C++ and thought I will use JNI to link it with java.I used function pointers there to invoke each instruction.But I faced problems and decided to implement in pure java.
NOw I have written it in java but I cant invoke the instructions as there is no function pointer.As you have adviced I will try to use reflection now.
But one thing i am worried about: Is reflection slow?
thanks
shyam
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
There is a better solution to your problem in Java: Instead of putting the instruction methods all in one method, make seperate objects for them.
Start by creating an interface

Now, you can create a class for every instruction and use references to objects of these classes in the place of function pointers.
One advantage of this approach is that you can easily factor common behaviour of some instructions into subclasses.
Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
I have done exactly the same thing but i have grouped several instructions in a class.For example I have grouped the Arithmetic instructions,memory access instructions in different classes.I want to know will there be any performance problems if I call these methods using reflection.
thanks
shyam
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I agree with Ilja: in an OOP language, creating Instruction-type objects would be a far more powerful means of expressing your program. Reflection gets you back the C-style semantics you're used to, sure, but using Reflection like this is rather like putting a sprinkler at the end of a fire hose because you want fine-grained control at 200 gallons per minute flow.
One of the advancements of OOP is to remove the useful but rudimentary semantics of constructs like function pointers.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Shyam Purkayastha:
I want to know will there be any performance problems if I call these methods using reflection.

Possibly, though reflection performance has greatly improved in 1.4
But that is not what you should worry about - performance problems are rather easy to fix once you *experienced* where they are coming from.
What you really should worry about are *maintenance* problems. For that it would be advisable to use the full power of OO, namely polymorphism, to your advantage.
You can still group the different categories of instructions via adequate packaging.
Shyam Purkayastha
Ranch Hand

Joined: Jan 15, 2001
Posts: 57
Thanks a lot Ilja and Michael for your advice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Function pointers
 
Similar Threads
Pure virtual function
Why Java ?
method and function
system calls in java
Aline decimals