Small java data type reminder

Source: Internet
Author: User

Byte: eight-digit integer-128--127, which can be used to save memory usage.
Short: 16-bit integer-32768--32, 767, also saves memory.
Int: 32-bit integer-2,147,483,648 -- 2,147,483,647. Generally, integers are enough.
Long: 64-bit integer-9,223,372,036,854,775,808 -- 9,223,372,036,854,775,807, generally not required
Float: 32-bit floating point. This is used if the floating point needs to save memory.
Double: 64-bit floating point. This parameter is generally applicable to non-integer floating points.

Use java data type short

  1. Public class MainClass {
  2. Public static void main (String [] args ){
  3. Short numA = 5;
  4. Short numB = 10;
  5. Short numC = 0;
  6. NumC = (short) (numA + numB );
  7. System. out. println (numC );
  8. }
  9. }
  10. The code is above. Let's see what you should pay attention. Pay attention to the 7th rows and see (short ). Here we will explain it as follows: (DataType) means forced type conversion. in Java, the calculation results of short are converted to integer, therefore, when assigning values to the short type variable numC, it must be forcibly converted to the corresponding short type. Otherwise, the Java compiler reports an error.

Use java data float

  1. Public class MainClass {
  2. Public static void main (String [] arg ){
  3. Float f1 = 9E-28F;
  4. System. out. println (f1 );
  5. }
  6. }
  7. The above code can be explained in a few ways. The only note here is that the Float type should be followed by F or f. Otherwise, the compiler will report an error.

 

 

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.