Do you know what the difference is between long and long?

Source: Internet
Author: User

There are two types of data in Java:


1. Basic type: long,int,byte,float,double
2. Object type: long,integer,byte,float,double All other Java-provided, or classes you create yourself.

Where long is called Long's wrapper class. Integer, Byte, and float are similar, and the first name of the generic wrapper class is the beginning of the capitalization of the numeric name.


What is a wrapper class?


In Java, sometimes the operation must be between two classes of objects, do not allow the operation between the object and the number. So there needs to be an object that wraps the numbers so that the object can be operated on with another object.


For example, we can define a class:

[Java]View Plaincopy
    1. Public class Long {
    2. int i=0;
    3. Public Long (int i) {
    4. this.i=i;
    5. }
    6. }


This long is a wrapper class that wraps an integer value, which can then be used to write operator overloading to support certain operations. You can assign a value at this time:
Long L = new long (10);
Now the variable L is an object, not a number.



Long is the original data type, there is no attribute method, only mathematical operation, Long is the corresponding reference data type long, it has methods and properties, a no method property, a method attribute, this is their difference.


Look at the following applet:

[Java]View Plaincopy
  1. Long L = 1;
  2. Long L1 = new Long (1);
  3. Long L2 = New Long (1);
  4. if (l = = L1) {
  5. System.out.println ("=====l and L1 equal =====");
  6. }
  7. if (L = = L2) {
  8. System.out.println ("=====l and L2 equal =====");
  9. }
  10. if (L1 = = L2) {
  11. System.out.println ("=====l1 and L2 equal =====");
  12. }


Output:

[Plain]View Plaincopy
    1. =====l and L1 equal =====
    2. =====l and L2 equal =====


If you replace the condition of the third if statement with "L1.equals (L2)", the output will become:

[Plain]View Plaincopy
    1. =====l and L1 equal =====
    2. =====l and L2 equal =====
    3. =====L1 and L2 equal =====


It can also be seen that the variable defined by long is a long object, while the variable defined by long is a numeric variable with a longer shape.


ID with a long or long?


Hibernate, El expression and so on are packaging types, with a long type can reduce the box/unboxing;

In Hibernate, the type of the self-increasing hid in the entity is defined with a long rather than a long. Otherwise, a marshallingerror:null error will appear during the DWR matching process.

Whether to choose long or long this also depends on the specific environment, if you think this property can not be null, then use long, because it defaults to 0, if this field can be null, then you should choose Long.

http://blog.csdn.net/liushuijinger/article/details/30726433

Do you know what the difference is between long and 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.