casting - I want to know the meaning of a line in java -


what meaing of below line of java code

  class dog {     int size;      dog(int size) {         this.size = size;     }      public boolean equals(object o) {         return this.size == ((dog) o).size; // im not getting whats meaning of line     } } 

i want know meaning of following line:

return this.size== ((dog)o).size; 

this meaning: current object's size variable compared equality other object's size variable. result, boolean value, returned result of equals method.

an implicit assertion made other object of same type. proper implementation of equals must not throw classcastexception in case, rather return false. therefore implementation not comply contract of object#equals method.

to me looks developer feeling smart , "found out" way concisely implement equals. correct, still quite concise, implementation go this:

return o instanceof dog && ((dog)o).size == this.size; 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -