官术网_书友最值得收藏!

Integral Data Types

Integral types are types that have integer values. These are int, long, short, byte, and char.

int Data Type

The int data type is used to represent integers. Integers are 32-bit numbers in the range of -2,147,483,648 to 2,147,483,647. Example of integers are 0, 1, 300, 500, 389 230, 1,345,543, -500, -324,145, and others in that range. For example, to create an int variable to hold a value 5, we write the following:

int num = 5;

The num variable is now an int with a value of five. We can also declare more than one variable of the same type in one line:

int num1, num2, num3, num4, num5;

Here, we have created five variables, all of the int type, and initialized to zero. We can also initialize all of the variables to a specific value, as follows:

int num1 = 1, num2 = 2, num3 = 3, num4 = 4, num5 = 5;

In addition to expressing integers in decimal format, we can also express integers in octal, hexadecimal, and binary format:

  • To express in hexadecimal format, we start the int with 0x or 0X, that is, a zero followed by x or X. The number has to be at least 2 digits in length. Hexadecimal numbers use 16 digits (0-9 and A-F). For example, to express 30 in hexadecimal, we would use the following code:

    int hex_num = 0X1E;

    Printing the number will output 30 as expected. To hold an integer with a value of 501 in hexadecimal, we would write the following:

    int hex_num1 = 0x1F5;

  • To express in octal format, we start the int with a zero and must have at least 2 digits. Octal numbers have 8 digits. For example, to express 15 in octal, we would do the following:

    int oct_num = 017;

    Trying to print the preceding variable will output 15. To represent 501 in octal, we would do the following:

    int oct_num1 = 0765;

  • To express in binary format, we start the int with 0b or 0B, that is, a zero followed by b or B. The case doesn't matter. For example, to hold the value 100 in binary, we would do the following:

    int bin_num = 0b1100100;

  • To hold the number 999 in binary, we would do the following:

    int bin_num1 = 0B1111100111;

As a summary of the aforementioned four formats of representing integers, all the following variables hold the same value of 117:

int num = 117;

int hex_num = 0x75;

int oct_num = 0165;

int bin_num = 0b1110101;

long Data Type

long is a 64 bit equivalent of an int. They hold numbers in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Numbers of long type are called long literal and are denoted by an L at the end. For example, to declare a long of value 200, we would do the following:

long long_num = 200L;

To declare a long of value 8, we would do the following:

long long_num = 8L;

Since integers are 32-bit and hence lie within the range of long, we can convert an int into a long.

主站蜘蛛池模板: 饶河县| 诏安县| SHOW| 江山市| 曲水县| 东丽区| 大洼县| 双城市| 竹北市| 来宾市| 屏南县| 阿合奇县| 聂拉木县| 简阳市| 东乡县| 拉萨市| 伊春市| 建始县| 老河口市| 凉城县| 塔河县| 象州县| 万宁市| 黔西| 饶平县| 永城市| 长顺县| 武冈市| 阳原县| 元谋县| 鄄城县| 江山市| 宁海县| 台东县| 揭阳市| 孙吴县| 徐闻县| 汉沽区| 闻喜县| 上虞市| 金华市|