Cainiao learns Java (20)-Do you know the difference between long and Long?

Source: Internet
Author: User

There are two data types in Java:


1. Basic types: long, int, byte, float, double
2. Object Type: Long, Integer, Byte, Float, Double, or other classes provided by java.


Long is called the long packaging class. Integer, Byte, and Float are also similar. Generally, the first write of the packaging class name is the first capital of the Value Name.


What is packaging?


In java, sometimes operations must be performed between two class objects, not between objects and numbers. Therefore, an object is required. This object wraps the number so that this object can be computed with another object.


For example, we can define a class:

public class Long {int i=0;public Long (int i){this.i=i;}}


This Long is a packaging class, which encapsulates an integer, and then you can write some methods to overload operators to support some operations. You can assign values at this time:
Long l = new Long (10 );
Now variable l is an object, not a number.



Long is the raw data type and has no attribute method. It can only be used for mathematical operations. Long is the reference data type corresponding to long. It has methods and attributes, one has no method attribute, and the other has method attribute, this is their difference.


Let's look at the following small program:

Long l = 1; Long l1 = new Long (1); Long l2 = new Long (1); if (l = l1) {System. out. println ("==== l equal to l1 ====");} if (l = l2) {System. out. println ("==== l and l2 equal ====");} if (l1 = l2) {System. out. println ("====== l1 and l2 equal ==== ");}


Output:

===== L equal to l1 =========== l equal to l2 =====

If you replace the condition of the third if statement with "l1.equals (l2)", the output result is:

====== L and l1 equal ============= l and l2 equal ========= l1 and l2 equal ==== =

The variables defined by Long are Long objects, while those defined by long are numerical variables with Long integer values.

 
Long or Long is used for ID?

 
Hibernate, el expressions, and so on are all packaging types. Using the Long type can reduce packing/unpacking;

The type of the Self-incrementing hid in hibernate must be defined by Long instead of long. Otherwise, the error message "invalid allingerror: null" appears during the DWR matching process.
 
Whether to choose Long or long depends on the specific environment. If you think this attribute cannot be null, use long because it defaults to 0. If this field can be null, select Long.

 
 

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.