Comparison of basic data types between Java, C, and C ++ languages

Source: Internet
Author: User

These problems will certainly occur when we need to port the underlying layer. Specially sorted.


 

Basic Data Types of Java language

 

 

 

There are eight basic data types in Java:

Byte, short, Int, long, float, double, 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 double 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

Double 1.797693e + 308 ~ 4.90020.e-324 occupies 8 bytes

The double type has a larger storage range and higher accuracy than the float type. Therefore, the data of the float type is usually of the double type without declaration. If you want to indicate that a data is of the float type, you can add "F" to 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 character storage range is/u0000 ~ /Uffff, you must add ''when defining bytes data. For example, '1' indicates the character '1' instead of 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 = '/u0031'; the output result is still 1, because the character '1' corresponds to the Unicode encoding:/u0031

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
• Double, double
• Long double, 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 Double 8 About 16 -308 ~ 308 % F
3 Long double (1) 12 About 19 -4932 ~ 4932 % LLF

 

7. floating point Precision
Float columns include float, double, and long double.

 

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 Double 1 11 52 1023
3 Long double 1 15 64 16383

 

 

Basic Data Types of C ++ Language

C ++ basic data types include char, Int, float, double, 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 Double 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 Double 64 1.7e-308 ------ 1.7e + 308
Long double 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.

 


 

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.