Home > Java > javaTutorial > body text

What are the integer variables in java

下次还敢
Release: 2024-04-28 23:30:22
Original
787 people have browsed it

There are four integer variable types in Java: byte (8 bits), short (16 bits), int (32 bits, default type), long (64 bits). Choosing the appropriate type depends on the size of the number that needs to be stored. To store a smaller range of numbers, use byte or short, and to store a larger range of numbers, use int or long.

What are the integer variables in java

Integer variables in Java

Integer variables are variable types used to store integer data in Java , divided into four types:

  • byte

    • 8-bit signed integer, the value range is -128 to 127
  • short

    • 16-bit signed integer, ranging from -32,768 to 32,767
  • int

    • 32-bit signed integer, the value range is -2,147,483,648 to 2,147,483,647 (default)
  • long

      ##64-bit signed integer, ranging from -2^63 to 2^63-1

Choose the appropriate integer type

Different integer types have different value ranges, and choosing the appropriate type depends on the size of the number that needs to be stored. For smaller ranges of numbers, you can use byte or short. For larger ranges of numbers, you can use int or long.

For example:

<code class="java">byte b = 127; // 8 位,取值范围:-128 至 127
short s = 32767; // 16 位,取值范围:-32768 至 32767
int i = 2147483647; // 32 位,取值范围:-2147483648 至 2147483647
long l = 9223372036854775807L; // 64 位,取值范围:-2^63 至 2^63-1</code>
Copy after login

Note:

    byte, short and int are signed integers and can represent negative values.
  • long can represent an unsigned integer, but L or l needs to be added after the literal value, such as 9223372036854775807L.

The above is the detailed content of What are the integer variables in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!