• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I compare two jclass refs for equality?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I do the following:



Are they guaranteed to be the same within one instance of the VM? Will it ever change? What about across startups of the VM (so if I serialized one of them)?

If not, in JNI how do I check the equality of two object's classes?
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best thing is to test this first. See if you get the exact same references, using printf("%p, %p\n", a, b); If not then you know that they are different objects. Unfortunately, if so it could possibly still be the case that sometimes they may be different.

What should of course always work is use the equals method:



Man these smileys can tick me off...
[ May 07, 2008: Message edited by: Rob Prime ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Best thing is to test this first.


I don't approve of this kind of "software design by experiment", where an alternative exists.

If the JNI documentation doesn't say that you can use == to compare two jclass pointers, then you should try hard to avoid doing so, even if experiment suggests it could work.

In this case, there's a perfectly good proper way to do it, by calling the equals() method, isn't there?
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to my defense, I did say that even if the two pointers are equal one time, they may not be equal all of the times.

As for equals, yes it works, but it may be a small hit on performance, as well as a bit more complex to code.

I have made one mistake though in my code. I'm using the equals method of class A, where you need the equals method of class Class. You'll need to get a reference to that one first.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

In this case, there's a perfectly good proper way to do it, by calling the equals() method, isn't there?[/QB]


Sure, but I was hoping there was a way to compare on the native side without having to poll into the JVM. I guess there isn't.

(And the specs say nothing about jclass refs. Not that they can nor that they cannot be compared.)

And I agree. Even if it worked, without documentation that it was allowed, how could I be sure they'd work under all cases/vm's?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!