There are eight basic data types in Java:
Byte, short, Int, long, float, do le, Char, Boolean
Integer
Here, byte, short, Int, and long all represent integers, but their value ranges are different.
The value range of byte is-128 ~ 127, occupies 1 byte (to the power of-2 to the power of 2 to the power of-1)
The short value range is-32768 ~ 32767, occupies 2 bytes (the 15th power of-2 to the 15th power of 2-1)
The value range of Int Is (-2147483648 ~ 2147483647), occupies 4 bytes (the 31 Power of-2 to the 31 power of 2-1)
The value range of Long is (-9223372036854774808 ~ 9223372036854774807), occupies 8 bytes (63 power of-2 to 63 power of 2-1)
We can see that the value range of byte and short is relatively small, while the value range of Long is too large and occupies a lot of space. Basically, int can satisfy our daily calculation, int is also the most commonly used Integer type.
Generally, if an integer such as 35 appears in Java, this number is int type. If we want it to be byte type, you can add an uppercase B: 35b after the data to indicate that it is of the byte type, the same 35s indicates the short type, and the 35l indicates the long type, indicating that int can be used without adding anything, however, if you want to represent the long type, you must add "L" after the data ".
Floating Point Type
Float and do le represent float data types. The difference between them is that they have different precision.
Float 3.402823e + 38 ~ 1.401298e-45 (e + 38 represents multiplied by the power 38 of 10, similarly, the e-45 represents multiplied by the power 45 of 10) occupies 4 bytes
Do le 1.797693e + 308 ~ 4.90020.e-324 occupies 8 bytes
The do le type has a larger storage range and higher accuracy than the float type. Therefore, the data of the floating point type is usually of the do le type without declaration, if you want to indicate that a data is float type, you can add "F" after the data ".
Floating point data cannot be completely accurate, so sometimes the last few digits of the decimal point may fluctuate during computation. This is normal.
Boolean (Boolean)
This type has only two values, true and false (true and false)
Boolean T = true;
Boolean F = false;
Char (text)
The data type used to store characters. It occupies 2 bytes and adopts unicode encoding. Its first 128 bytes are encoded in ASCII format.
The storage range of characters is \~ \?, When defining data of the dense type, add ''. For example, '1' indicates the character '1' rather than the value 1,
Char c = '1 ';
Let's try to Output C to see if system. Out. println (c); the result is 1, and if we do this output, system. Out. println (C + 0 );
The result is changed to 49.
Let's define C in this way.
Char c = '\ 1'; the output result is still 1, because '1' corresponds to the Unicode encoding \ 1
Char C1 = 'h', C2 = 'E', C3 = 'l', C4 = 'l', C5 = 'O ';
System. out. print (C1); system. out. print (C2); system. out. print (C3); system. out. print (C4); sytem. out. print (C5 );
String
We have seen this definition before:
String S = "hello ";
System. Out. println (s); the combination of the preceding five statements has the same effect. What is string? String is a string. It is not a basic data type and is a class.
C Language Data Type
1. Integer Data Type
C defines five integer data types.
Integer Data Type table
Serial number |
Type name |
Description |
Bytes |
Value Range |
1 |
Signed Char |
Signed single-byte Integer type |
1 |
-128 ~ + 127 |
2 |
Short int |
Short integer |
2 |
-32768 ~ + 32767 |
3 |
Int |
Integer |
4 |
-2147438648 ~ + 2147438647 |
4 |
Long int |
Long Integer |
4 |
-2147438648 ~ + 2147438647 |
5 |
Long long int |
Long and long integer type |
8 |
-9223372036854775808 ~ +-9223372036854775807 |
2. unsigned integer type
It also has the unsigned integer type.
Unsigned integer table
Serial number |
Type name |
Bytes |
Value Range |
1 |
Unsigned char |
1 |
0 ~ 255 |
2 |
Unsign short int |
2 |
0 ~ 65535 |
3 |
Unsigned int |
4 |
0 ~ 4294967295 |
4 |
Unsigned long int |
4 |
0 ~ 4294967295 |
5 |
Unsign long int |
8 |
0 ~ 18446744073709551615 |
3. Integer constants
An integer constant is a constant used to represent an integer value. It can be divided into short INTEGER (short INT), INTEGER (INT), long integer (long INT), and long integer (long INT) four. C default INTEGER (INT ). Various types of Integer constants are in hexadecimal notation (suffixes are case insensitive)
Serial number |
Data Type |
Octal |
Decimal |
Hexadecimal |
1 |
Integer |
0112 |
74 |
0x4a |
2 |
Long Integer (l) |
010000l |
74l |
0x4al |
3 |
Long and long integer (LL) |
010000ll |
74ll |
0x4all |
4 |
Unsigned integer (u) |
0112u |
74u |
0x4au |
5 |
Unsigned long integer (UL) |
0112ul |
74ul |
0x4aul |
6 |
Unsigned long integer (ull) |
0112ull |
74ull |
0x4aull |
4. character data type
In C, there is only one type of character data, that is, char type data. Char is also called the character type directly. The Bytes occupies at least one byte of memory space. The integer stored in the char type variable can be expressed as a signed or unsigned value, depending on the compiler.
5. Character Variables
Character variables are used to store numeric values. There are two types of variable: signed and unsigned.
6. Floating Point Data Type
The C language defines three floating point data types:
? Float, single precision
? Do le, Double Precision
? Long do le, long dual-precision
Different types of floating point numbers are defined in the C standard. The length of the byte varies depending on the compiler or hardware conditions.
Floating-point bytes length, precision, order of magnitude range, and output input format table
Serial number |
Data Type |
Byte Length |
Precision |
Magnitude range |
Printf and scanf formats |
1 |
Float (f) |
4 |
7 |
-38 ~ 38 |
% F |
2 |
Do le |
8 |
About 16 |
-308 ~ 308 |
% F |
3 |
Long do le (1) |
12 |
About 19 |
-4932 ~ 4932 |
% LLF |
7. floating point Precision
Float columns include float, do le, and long do le.
8. Floating Point Storage
Floating point values are stored in the memory in the form of scientific notation. The floating point memory format consists of three parts:
1) symbol bit
The symbol bit is the highest bit. The value 1 indicates a negative number. The value 0 indicates a non-negative number.
2) exponential position
Floating Point indexes are stored in the form of supplementary codes and are the index part of scientific notation.
3) Base Digit
The base digit is the last digit of the floating point type. This digit determines the accuracy of the value.
Floating Point storage segment table
Serial number |
Data Type |
Symbol bit |
Exponential bit |
Base Digit |
Deviation Value |
1 |
Float |
1 |
8 |
23 |
127 |
2 |
Do le |
1 |
11 |
52 |
1023 |
3 |
Long do le |
1 |
15 |
64 |
16383 |
Basic Data Types of C ++ Language
C ++ basic data types include char, Int, float, do le, and void ), the following table shows the basic types and the number of digits in the memory and the range of values (ASCII code for memory storage ):
Basic Data Type
Type |
Type name |
Number of digits |
Range |
Character Type |
Char |
8 |
-128 ------ 127 |
Integer |
Int |
16 |
-32768 ------ 32767 |
Floating Point Type |
Float |
32 |
3.4e-38 ------ 3.4e + 38 |
Double Precision type |
Do le |
64 |
1.7e-308 ------ 1.7e + 308 |
No value type |
Void |
0 |
0 |
You can add modifiers before the type name (except for void). modifiers include signed, unsigned, and short) and long (long ). Signed and unsigned are used for struct or Integer type. Short and long can be used for integer type and double precision type, as shown in the following table:
Basic data types and modifiers
Type |
Type name |
Number of digits |
Range |
Character Type |
Char |
8 |
-128 ------ 127 |
Signed Char |
8 |
-128 ------ 127 |
Unsigned char |
8 |
0 ------ 255 |
Integer |
Int |
16 |
-32768 ------ 32767 |
Signed int |
16 |
-32768 ------ 32767 |
Unsigned int |
16 |
0 ------ 65535 |
Short int |
16 |
-32768 ------ 32767 |
Signed short int |
16 |
-32768 ------ 32767 |
Unsigned short int |
16 |
0 ------ 65535 |
Long int |
32 |
-2,147,483,648 ---- 2,147,483,647 |
Signed long int |
32 |
-2,147,483,648 ---- 2,147,483,647 |
Unsigned long int |
32 |
0 ---------------- 4,294,967,295 |
Floating Point Type |
Float |
32 |
3.4e-38 ------ 3.4e + 38 |
Double Precision type |
Do le |
64 |
1.7e-308 ------ 1.7e + 308 |
Long do le |
80 |
3.4e-4932 ------ 1.1e + 4932 |
Int can be omitted when int is modified with signed, unsigned, short, and long.
The Bytes type is actually a byte integer.The floating point type and double precision type indicate real numbers.
Note: different compilation systems have different storage lengths of integers. For a 16-bit compiling system, Int Is 2 bytes; for a 32-bit compiling system, Int Is 4 bytes. The sizeof operator can be used for testing. For example:
Cout <sizeof (INT) <Endl;
The output result is the number of bytes.
For example, long is 32-bit, Int Is 32-bit in 32-bit systems, but in the future 64-bit systems, Int Is 64
Application:
In network communication programs, unsigned char is often used to define a piece of memory, that is, a buffer zone. Char is signed and unsigned char is unsigned.
The Char Integer Range is-128 to 127 (0x80 _ 0x7f), while the unsigned char integer range is 0 to 255 (0 _ 0xff ).
In fact, the difference between positive and negative is because the storage format of the char type in the memory is the same as that of the int type (or similar), it is precisely because the char type value can be the same as that of the int type (including unsigned, short and so on. Of course, pay attention to the range. Char can be regarded as a single-byte integer, And the int type must have at least two bytes (in this case, the int era refers to all integer types ). Char is used when a single byte is used to represent an integer (to save memory. More importantly, ASCII characters of English letters are less than 0x80, and 0x80 and above are non-English characters and tabs (including Chinese characters and other country characters of course ). For char, the maximum value is 0x7 F (127). To indicate non-English characters, it must be greater than 0x7 F. Therefore, an unsigned
Char (its range is greater than 0x7f ).
See the following simple example:
# Include <stdio. h> # include <string. h> void main () {int ncount = 0; // char * P = "ABCD Chinese character"; char P [] = "ABCD Chinese character "; int Len = strlen (p); int num = 0; For (INT I = 0; I <Len; I ++) {If (unsigned char) P [I]> 0x7f) {I ++; num ++;} ncount ++;} printf ("string \" % s \ "length is % d bytes, there are a total of % d characters, of which % d are Chinese characters. \ N ", P, Len, ncount, num );
Left shift: <
Low-level zero-filling, high-level discard.
For the Left shift operation, the logical left shift is the same as the arithmetic left shift.
Shift right:>
Pay attention to the symbol bit issue when you move the value to the right.
For the number of unsigned values, the value of the high position is zero when the right shift is performed.
For a signed number, if this number is positive, it is the same as the unsigned number. If this number is negative, Some compilers add zero at the top (logical right shift), and some compilers add one at the top (arithmetic right shift ). Therefore, it is generally considered that if a program uses a right shift operation with a signed number, it cannot be transplanted. In addition, the right operand of all the displacement operations must be smaller than the length of the left operand. Otherwise, the result is undefined, that is, the number of digits to be moved must be smaller than the number itself. Otherwise, it is equivalent to no shift.
Supplement:
Because there is no unsigned number in Java, the> operator in Java indicates the right shift of arithmetic. If you want to perform the right shift of logic, you can use the> operator.
Communication format conversion
When Java communicates with network programs written by some windows programming languages such as C, C ++, and Delphi, sending negotiation information may be a struct,
Shift is used here. The data is usually defined as unsigned int, unsigned char, unsigned short, and so on,
Conversion between high and low bytes
In Windows, the byte order starts with a low byte. in Linux, the Unix byte order starts with a high byte. in Java, the beginning of a high byte regardless of platform changes.