• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Function pointers

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Shyam Purkayastha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ilja and Michael for your advice.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic