Short type variables in Java

Source: Internet
Author: User

First look at the basic types in Java, such as 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 length are fixed, not because of the different machines, so that Java has a good portability.

When you are doing a project recently, you need to store a 16-bit long binary number in a variable to see if the short type fits your needs.

But because the number types in Java are signed, the first bit of the short type is used to represent the symbol, and the actual storage length is only 15 bits.

That is -7fff-+7fff. What if we want to store +7FFF-+FFFF's numbers? Only a workaround is adopted, using a transform algorithm. We can use -7fff--0001 to store numbers larger than 7FFF. The conversion formula is-(ffff-x)-1. Because of the symbol, so +0000 and 0000 are equal, so less than the unsigned number of 1 numbers, so to subtract 1 in the formula, because of this problem, we can not store +8000 with this method.

This method can basically meet our needs, if there is a special need, such as to store +8000, that only to take another approach.

Short type variables in Java

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.