Ofek Ron
Ofek Ron

Reputation: 8580

can equals() tell if an object sent and then recieved back is actually the same?

Im using the OCSF framework explained here, assume i send originalMssg using sendToServer(Object msg) which uses java's Object streaming with a socket on the client side, and then on the server side i run sendToClient(Object msg) where the mssg object is the same as i recieved from that client.

now, consider the following callback which is called once the mssg has been recieved back from the server on the client side code:

@Override
protected void handleMessageFromServer(Object msg) {
  System.out.println(msg.equals(originalMssg));
    
}
  1. on the above scenario will it always print true?
  2. if not in what cases will it not? and is it possible to make it always return true?
  3. if in all cases it returns false, how do can you mark your mssg object so that you can recognize it when it gets back from the server?

Upvotes: 1

Views: 108

Answers (2)

One Man Crew
One Man Crew

Reputation: 9578

The main proper of OSCF is to send String's or array of strings.....so first of all check if your object is serializable.

Then there is the equals method you need to @Override for each class you made.

1)NO.

2)Almost any time it's be false , you can try to sent a string a object and you should get true.

3)See my introduction to my answer.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718718

(This is purely based on reading the OSCF website and javadocs ... YMMV)

The website says that OSCF sends and receives plain Java objects (POJOs) that are serializable. From this I infer that the msg object can be an instance of any serializable class.

On the above scenario will it always print true?

It is not possible to say. It depends on how the actual class implements equals.

if not in what cases will it not?

Not possible to say (see above).

and is it possible to make it always return true?

Ermm ... you would need to change the implementation of the equals method on the class you are sending / receiving.

if in all cases it returns false ...

It is not possible to say if that precondition holds (see above).


Bottom line. The msg is an instance of some class that you have implemented yourself or chosen. It is up to you to implement / choose it with equals behaviour that suits your requirements ... including (apparently) comparison with instances of the object that have made the round-trip from the client to the server and back again.

Upvotes: 1

Related Questions