java how expensive is a method call

后端 未结 12 1556
情书的邮戳
情书的邮戳 2020-11-27 04:04

I\'m a beginner and I\'ve always read that it\'s bad to repeat code. However, it seems that in order to not do so, you would have to have extra method calls usually. Let\'s

12条回答
  •  有刺的猬
    2020-11-27 04:48

    As others have said, the cost of the method call is trivial-to-nada, as the compiler will optimize it for you.

    That said, there are dangers in making method calls to instance methods from a constructor. You run the risk of later updating the instance method so that it may try to use an instance variable that has not been initiated yet by the constructor. That is, you don't necessarily want to separate out the construction activities from the constructor.

    Another question--your clear() method sets the root to EMPTY, which is initialized when the object is created. If you then add nodes to EMPTY, and then call clear(), you won't be resetting the root node. Is this the behavior you want?

提交回复
热议问题