Effective Java 54 Use native methods judiciously
2014-04-17 20:29 小郝(Kaibo Hao) 阅读(390) 评论(0) 编辑 收藏 举报Java Native Interface(JNI) allows Java applications to call native methods, which are special methods written in native programming languages such as C or C++.
Historical usages of native methods
- Access to platform-specific facilities such as registries and file locks.
- Access to libraries of legacy code.
- Access to native methods written for performance-critical part of application.
Disadvantage of native methods
- Native language are not safe (Item 39), native methods are no longer immune to memory corruption errors.
- Since native languages are platform dependent, application use them are far less possible.
- Difficult to debug.
- Fixed cost associated with going into and out of native code.
- It requires "glue code" that is difficult to read and tedious to write.
Summary
Think twice before using native methods. Rarely, if ever, use them for improved performance. If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly. A single bug in the native code can corrupt your entire application.
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。