3

I'm using kryonet to send objects back and forth from a server and client. There is a listener that is run whenever an object has been received. The only way it seems to determine the object's instance is by using:

if(object instanceof ClientLoginPacket){
    //Do stuff
}

I want to know what instanceof checks to determine if an object is of a specific type or not. Does it check if the class is exactly the same with all the code, does it check the variables and there names? Does it check the imported packages too? Any information you give me can help.

The reason I want to know this is because the way I'm making the packet, the code in the methods for the server are different from the client. For example, on my client to send the packets I do:

public void send(){
    Client.sendPacketTCP(this);
}

and on my server I do this:

public void send(){
    Server.sendPacketTCP(this);
}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
vedi0boy
  • 1,030
  • 4
  • 11
  • 32
  • instanceof is a sign of bad design. This is what polymorphism is supposed to handle. If you think your code requires it, I would recommend redesigning until it doesn't. – duffymo Sep 08 '14 at 10:27
  • I can easily use something else. But in my case, the less stuff instanceof checks, the better. I would like it if it only check variables and class name. – vedi0boy Sep 08 '14 at 10:29
  • Possible dup of [How is instanceof implemented in modern JVM implementations?](http://stackoverflow.com/questions/12386789/how-is-instanceof-implemented-in-modern-jvm-implementations) – DavidPostill Sep 08 '14 at 10:35
  • http://mindprod.com/jgloss/instanceof.html – DavidPostill Sep 08 '14 at 10:39
  • Thanks, thats pretty interesting. – vedi0boy Sep 08 '14 at 11:34

4 Answers4

6

instanceof is a jvm instruction, it's probably native code, but the specification says that it must do:

Instanceof

I really don't think it checks the variable and its names, this is useless. It has to traverse the class hierarchy until it can prove that it is of some type: extends, implements or it is the same class

duffymo
  • 305,152
  • 44
  • 369
  • 561
Eugene
  • 117,005
  • 15
  • 201
  • 306
2

As per java docs, isInstance#Object method is the dynamic equivalent of the Java language instanceof operator.

This method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion.

See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
1

The reason I want to know this is because the way I'm making the packet, the code in the methods for the server are different from the client.

I've never used Kryonet, but according to its documentation this is exactly what you shouldn't do:

It is very important that the exact same classes are registered on both the client and server, and that they are registered in the exact same order.

So ask yourself: why are you using different classes on client side and server side? Could you find an abstraction "data only" class that would work on both sides and send only this one across?

Ray
  • 3,084
  • 2
  • 19
  • 27
  • Yes, they are the same class just different code, but you might be right though. I have to research it more. I would still like to know about the `instanceof` a little more, it still might help. – vedi0boy Sep 08 '14 at 10:37
  • Then they are NOT the same class. They are two different classes with the same name. – Ray Sep 08 '14 at 10:51
  • Does it matter as long as they have the same variables and methods, just the code in the methods are different. – vedi0boy Sep 08 '14 at 11:29
0

Results of my test: Everything matters (variables, methods, package declarations, you name it!)

I don't understand why people say it's inaccurate, it's actually too accurate for what I want to do but I know what adjustments I need to make anyways.

Thank you everyone for you help!

vedi0boy
  • 1,030
  • 4
  • 11
  • 32