Basic and packaging type practices in java and java

Source: Internet
Author: User

Basic and packaging type practices in java and java

So far, I have been using java for almost two years. Some things should also be summarized.

The concepts of Basic Types and packaging types are not described in detail in this article.

If these two types are used directly, nothing is worth discussing. It is nothing more than automatic packing and unpacking. java can make you feel that they exist, but if we use it in database ing entities (hereinafter referred to as bo, that is, business object), we should pay attention to it!

For example, there is a rating table with a scoring field. When writing a bo, define the scoring attribute, whether to use int or Integer?

If Integer is used for data insertion, a bo is first instantiated. At this time, the Integer field in bo is null by default. If we forget to give it an initial value of 0, the value inserted into the database is also null, and then the query is also null.

Imagine how the lower limit is zero as the score. How can it be null? The reader may not pay much attention to this problem. The following code throws a null pointer exception:

 

1 Integer i = null;2 3 if(i == 0){   4 5 }

 

Why? Because java will automatically split the I, and how to split the null, of course, it will be a null pointer, which is unreasonable.

Since it is unreasonable, it seems that we can only use the basic type int, And the int just solves the above problem, because the int is the basic type, there is a natural initial value of 0.

In some cases, bo is directly placed on the controller layer to encapsulate parameters. The parameters submitted by the client are accepted directly by a bo, so that a bunch of variables do not need to be defined repeatedly, it is also convenient to insert data to the database.

The problem arises. If our bo is defined by an int of the basic type, the default value is 0, even if the client does not submit this parameter, in this way, the client submits the score 0, which is still the default value.

Xiao Cai believes that it is not difficult to solve this problem. The simplest way is to regard 0 as an invalid value or a null value. If a value is 0, the parameter is not submitted or the submitted parameter is invalid.

Of course, we cannot generalize that a stick will kill all 0. In the classic design, the concept of soft deletion is to add a field (such as status) to mark it. 0 indicates that the record is normal, and 1 indicates that the record has been deleted. Here we can still use 0, because this is an internal parameter, which is used as an internal operation of the system and does not involve interaction with the customer.

 

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.