java - Why should a tag interface appear last in a generic class declaration? -
this extract core java(volume 1,by cay s. hortsman). here have
class interval<t extends comparable & serializable> as goes as:" note:you may wonder happens if switch bounds:
class interval<t extends serializable & comparable> in case, raw type replaces t serializable, , compiler inserts casts comparable when necessary. for efficiency, should therefore put tagging interfaces (that is, interfaces without methods) @ end of bounds list. " have no idea why tagging interface efficiency , wish plain answer.
assuming mr. horstmann says still true, there slight efficiency difference.
everywhere refer instance comparable (i.e. invoke 1 of methods) compiler has insert runtime type check (i.e. cast) verify object indeed comparable. putting tag interface last lets compiler check proper type @ compile time , omit casts.
but you'd never notice difference in execution speed.
Comments
Post a Comment