conn! The difference between =null and conn.isclosed

Source: Internet
Author: User

Tell me the difference between these two pieces of code:
1, Conn! = NULL This is not sure whether the above connection is initialized and judged, because a lot of time to write code, using lazy loading mode, usually first define a connection conn=null, use the time to initialize its object, This can reduce the waste of memory. This time if you are not sure if Conn is initialized, it is best to add conn! = NULL before closing, otherwise it is possible to close conn with a null pointer error.

2, conn.isclosed () This is to determine whether the current connection is closed, the first to ensure that connection has generated an instance.

From a perfect point of view of the program, when closing the connection, it is best to judge two of them:
if (conn!=null&&!conn.isclosed ()) {
Conn.close ();
}

conn! The difference between =null and conn.isclosed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.