My suggestion is to use an ObjectStream, to output the variable or the integer. You can use the readObject() method on the ObjectInputStream to read an object, and use getClass or instanceOf to determine what it's an instance of.
For example: Object o = objectInputStream.readObject(); if (o instanceof String) { System.out.println("It's a string!"); } else if(o instanceof Integer) { System.out.println("It's an integer"); }
Note that you might need to send an Integer as opposed to int. If you send an int, then using the readInt() method would read an int, but since you don't know if the next thing you're getting is an int or String, you can't really do that.
What I've done before in this case is to send a known thing, followed by the unknown. For example, an enum or string that explains what the next thing is. So you could send "S" for String or "I" for int. That's probably a lot more difficult then just saying out.writeObject(Integer.valueOf(i))