当前位置: 代码迷 >> 综合 >> Difference between final. finally , and finalize() final, finally 和finalize区别
  详细解决方案

Difference between final. finally , and finalize() final, finally 和finalize区别

热度:43   发布时间:2024-01-05 09:42:01.0

Final --- constant declaration.  常量声明

   A final method or variable is one that can't be overridden, you can define a method as final within a class to ensure that any extesions to the class don't replace it.

 

    Finally-- executed by try catch block undoubtedly     异常必定执行

    The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed enven if an unexpeted exception occurs. But finally is useful for more than just exception handing , it allows the programmer to avoid having clearnup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

 

    Finalize()--- garbage collection 垃圾回收

    finalize()  method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.

 

 

  相关解决方案