Short Type variables in Java

Source: Internet
Author: User

First, let's take a look at the basic types in Java, as shown in the following table:

Primitive type

Size

Minimum

Maximum

Wrapper type

Boolean

-

-

-

Boolean

Char

16-bit

Unicode 0

Unicode 216-1

Character

Byte

8-bit

-128

+ 127

Byte

Short

16-bit

-215

+ 215-1

Short

Int

32-bit

-231

+ 231-1

Integer

Long

64-bit

-263

+ 263-1

Long

Float

32-bit

Ieee754

Ieee754

Float

Double

64-bit

Ieee754

Ieee754

Double

Void

-

-

-

Void

Java's basic types of storage have a fixed length, which does not vary with machines. Therefore, Java has good portability.

Recently, when working on a project, you need to store 16-bit long binary numbers in a variable. The above table shows that the short type meets the requirements.

However, because the numeric types in Java are signed, the first part of the short type is used to represent the symbol. The actual storage length is only 15 characters,

That is,-7fff-+ 7fff. What should I do if I want to store + 7fff-+ FFFF numbers? Only a work und is adopted, and the transformation algorithm is used. We can use-7fff--0001 to store numbers greater than 7fff. The conversion formula is-(FFFF-X)-1. Because of the symbols, + 0000 and-0000 are equal. Therefore, one digit is less than the number of unsigned characters, so 1 is required in the formula. Due to this problem, with this method, we cannot store more than 8000.

Using this method can basically meet our needs. If there are special needs, for example, to store more than 8000, we only need to adopt other methods.

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.