The data type

1. What are data types for?

Data types are used to declare variables, and programs allocate different sizes of space according to different data types during operation.

int i = 10; Double d = 1.23; Variable I and variable D have different types and sizes.Copy the code

There are two types of data types in the Java language:

First: basic data types

Basic data types can be divided into 4 categories and 8 subcategories:

The first type: integer type

Byte,short,int,long

Second type: floating point type

Float,double (with decimal)

The third type: Boolean type

Boolean: Only two values true and false, true means true, false means false

The fourth type: character type

Char: Java specifies that character literals must be enclosed in single quotes. Belongs to the text.

Small eight kinds:

  • byte,short,int,long
  • float,double
  • boolean
  • char

Second: reference data types

The String is a reference data type.

String String is not a basic data type.

In Java, all but the basic data types are reference data types.

Reference data types will be introduced later in the object-oriented world.

3. Among the 8 basic data types

What is the difference between int short and int long?

Floating point: What is the difference between a float and a double?

Difference: the size of the space occupied is different.

What about computer storage units?

Computers can only read binary. (1001101100)…

  • 1byte = 8 bits (8 bits) -> 1byte = 8 bits

  • A bit is a 1 or a 0.

  • 1KB = 1024byte

  • 1MB = 1024KB

  • 1GB = 1024MB

  • 1TB = 1024GB

  • byte b = 2; On the computer it is represented like this: 00000010

  • short s = 2; On the computer it is expressed like this: 00000000 00000010

  • int i = 2; In the computer is expressed like this: 00000000 00000000 00000000 00000010

  • .

Number of bytes occupied by type (byte)


Binary?

Binary??

Decimal to binary

125 to binary??

How to do it: Divide by 2 and output the remainder in reverse order.

1111101

Binary to decimal

2 to the 2nd power 2 to the 1st power 2 to the 0 power 1 1 1 1 4 2 1 1*4 + 1*1 = 7 2 to the 2nd power 2 to the 1st power 2 to the 0 power 1 0 1 4 2 1 1*4 + 0*2 + 1*1 = 5Copy the code

4. The value range of the byte type?

The byte value is from -128 to 127. A total of 256 digits can be identified.

How is the maximum value of byte type calculated?

A byte is 1 byte and 8 bits. Therefore, the maximum value that a byte can store is:

01111111

Note: In computers, the leftmost bit of a binary is the sign bit, which when 0 represents a positive number.

When it’s 1, it’s negative. Therefore, the maximum value of byte type is 01111111

So is it 2 to the seventh minus 1?

Is it: 10000000 (preceded by a binary) -1

The maximum value of the byte type is 2 to the seventh power -1.

There are a few ranges to keep in mind:

  • Byte: [-128 to 127]
  • Short :[-32768 ~ 32767] can represent 65536 different digits
  • (4 bytes)int: [-2147483648 ~ 2147483647]
  • Char: [0 to 65535] indicates 65536 different digits
  • Short and char actually have the same capacity, although char can represent larger numbers.
  • Because char represents words, the file has no positive or negative distinction, so char can represent larger numbers.

For the 8 basic data types:

One byte, short, int, long, float, double, Boolean, said this type of computer up easily, because they are both Numbers. The Boolean type has only two values true and false. In fact, true and false correspond to 1 and 0 respectively in C++. 1 is true and false is 0.

Char is a bit of a hassle for computers because it corresponds to text, which is different from country to country and cannot be converted directly to binary by “natural algorithms”. What about this time?

Character encoding was born.

What is character encoding?

  • A character encoding is an artificially defined set of conversion tables.
  • A series of binary characters specified in character encodings.
  • The character code is essentially a dictionary, a field that describes the contrast between text and binary.
  • Character encodings are artificial. (It’s stipulated by a computer association.)

Character encoding involves two processes, encoding and decoding, encoding and decoding must use the same set of character encoding, otherwise there will be garbled code.

About the development of character coding?

In the beginning, computers did not support words, only scientific calculations. In fact, computers were originally developed for warfare, calculating the trajectory of missiles….

Later, with the development of computers, computers began to support characters, the first supported characters is English, English corresponding character encoding method is: ASCII code.

ASCII codes are stored in 1byte, because there are 26 letters in English. (The total number of keys on the keyboard would not exceed 256. One byte can represent 256 different cases. So English itself has an advantage in computers.

'A' --(encoded in ASCII code) -- > 01100001 01100001 --(decoded in ASCII code) -- > 'A' If the encoding and decoding are not the same, garbled characters will appear. 'b' ---> 98 'c' ---> 99... 'a' ---> 97 'A' ---> 65 'B' ---> 66 ... '0' --> 48 (this '0' is not that 0, it is a text '0') '1' --> 49Copy the code

With the development of computer languages, isO-8859-1 encoding, also known as Latin-1 encoding, was developed by the International Standards Organization, which is compatible with ASCII codes upwards. But Chinese is not supported.

Later developed to Asia, only support Chinese, Japanese, Korean….

Encoding mode of Chinese: GB2312<GBK<GB18030 (capacity relationship)

The above encoding mode is simplified Chinese.

Traditional Chinese: big5 (Taiwan uses big5.)

In Java, the Java language uses a character encoding approach to support all characters in the world

It’s called Unicode encoding. Unicode encoding unifies all languages in the world and supports all languages.

The implementation includes utF-8 UTF-16 UTF-32….

Remember:

  • ASCII (‘a’ is 97 ‘a’ is 65 ‘0’ is 48…)
  • ISO – 8859-1 (latin-1)
  • GB2312
  • GBK
  • GB18030
  • Big5
  • Unicode (UTF8 UTf16 UTf32)

Detailed explanation of the eight basic data types

  • The character type char
  • Byte short int long
  • Float double
  • The Boolean Boolean

Details on data types

Character type: char

Integer: byte short int long

byte b = 127; // Can be assigned directly

short s = 32767; // Can be assigned directly

Char // Can be assigned to a char variable without exceeding the value range of char? \

Float type: float double

Boolean: Boolean \

All in all, what rules should you follow when casting?

  • Rule 1: Of the eight basic data types, Boolean cannot be converted. The other seven types can be converted.

  • If an integer literal does not exceed byte,short, or char, it can be assigned to a byte,short, or char variable.

  • The conversion from small capacity to large capacity is called automatic type conversion. The order from small capacity to large capacity is as follows: Byte < short(char) < int < long < float < double, where both short and char take two bytes, but char can represent larger positive integers;

  • Fourth: large capacity conversion to small capacity, known as the cast, write must add “cast character”, but the runtime may appear precision loss, use caution;

  • Article 5: When the byte,short and char types are mixed, they should be converted to int type before operation; \

  • Article 6: a variety of data types mixed operation, each first into the largest capacity of the one before operation;

None of the pen-based tests exceeded the six rules above. Rote learning.

The last

Recommended to everyone a more detailed Java zero basic tutorial, the following is I have seen feel quite good, worth watching the collection.

To share it with you, click here

www.bilibili.com/video/BV1Rx…

If it helps you, thank you for your support