The data type

1. What’s the use of data types?

Data types are used to declare variables, and the program allocates different sizes of space according to different data types during operation.

int i = 10; Double d = 1.23; The I variable and the d variable have different types and different space sizes.

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

The first: basic data types

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

The first type: integer type


The second type: floating point

Float,double (with decimal)

Class 3: Boolean type

Boolean: Only two values true and false. True means true and false means false

Fourth class: character type

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

Small eight kinds:

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

Second: reference data types

  • The String type is a reference data type.
  • String strings do not fall under the category of basic data types.
  • Except for the basic data types, all that is left in Java are reference data types.
  • Reference data types are not touched until they are later object-oriented.

3. Of the 8 basic data types

Integer type: byte short int long

Float: What’s the difference between float and double?

Difference: Occupy different space size.

About computer storage units?

The computer can only read binary. (1001101100)…

  • 1byte = 8bit (8 bits) –> 1byte = 8bit
  • A bit is a 1 or a 0.
  • 1KB = 1024byte
  • 1MB = 1024KB
  • 1GB = 1024MB
  • 1TB = 1024GB
  • byte b = 2; In the computer, it’s represented like this: 00000010
  • short s = 2; In the computer, it would look like this: 0000000000010
  • int i = 2; In a computer, it would look like this: 00000000 00000000 00000010
  • .

The number of bytes taken by the type (byte)

About binary?


The decimal system is converted to binary

Convert 125 to binary??

Solution: Divide by 2, then print the remainder in reverse order.


Binary is converted to decimal

Two to the second, two to the first, two to the zero

1 1 1

4 2 1

4 + 12 plus 1 times 1 is 7

Two to the second, two to the first, two to the zero

1 0 1

4 2 1

4 + 02 plus 1 times 1 is 5

What is the range of byte type?

Byte is [-128 ~ 127] and can identify 256 different numbers.

How is the maximum value of byte type calculated?

The maximum number of bytes a byte can store is:


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

A value of 1 is a negative number. So the maximum byte type is: 01111111

So is it 2 to the seventh minus 1?

10000000 (front is a binary) -1

The maximum byte type is: 2 ^ 7-1.

There are a couple of ranges to keep in mind:

  • (1 byte)byte: [-128 ~ 127]
  • (2 bytes)short:[-32768 ~ 32767] can represent 65536 different numbers
  • Int: [-2147483648 ~ 2147483647]
  • Char: [0~65535] can represent 65536 different numbers
  • Short and char actually have the same volume, although char can represent a larger number.
  • Because char represents text, there is no difference between positive and negative files, so char can represent larger numbers.

5. For the eight basic data types:

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

The CHAR type is a bit of a hassle for computers to represent because CHAR corresponds to text, which varies from country to country and cannot be converted directly to binary using a “natural algorithm.” What should I do at this time?

Character encoding was born.

What is character encoding?

  • Character encoding is artificially defined by a set of conversion tables.
  • The character encoding specifies the binary of a series of characters.
  • The character encoding is essentially a dictionary, a field that describes the relationship between literal and binary.
  • The character encoding is artificial. It’s regulated by a computer society.
  • Character encoding involves two processes, encoding and decoding, encoding and decoding must use the same set of character encoding, otherwise there will be garbled.

About the development of character encoding?

In the beginning, computers did not support words, only supported 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 text, the first text is English, English corresponding character encoding is: ASCII code.

ASCII codes are stored in 1byte because there are 26 letters in the English language. (All the keys on the keyboard are no more than 256. 1byte can represent 256 different cases. So English itself has an advantage in computers.

‘A’ –(encoded with ASCII code)-> 01100001 01100001 –(decoded with ASCII code)-> ‘A’ If the encoding and decoding are not the same encoding mode, there will be a garbled code.

‘b’ —> 98

‘c’ —> 99…

‘a’ —> 97

‘A’ —> 65

‘B’ —> 66

‘0’ –> 48 (this ‘0’ is not that 0, it is the word ‘0’) ‘1’ –> 49

With the development of computer language, later International Standards Organization developed ISO-8859-1 coding, also known as Latin-1 coding, upward compatibility with ASCII code. But Chinese is not supported.

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

GB2312<GBK<GB18030 (capacity relationship)

The above encoding is in simplified Chinese.

Traditional Chinese: BIG5 (Taiwan uses Big5.)

In Java, the Java language uses a character encoding scheme in order to support all literals in the world

It’s called Unicode Encoding. Unicode encodings unify and support all scripts in the world.

Specific implementations include: UTF-8 UTF-16 UTF-32….

Keep in mind:

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

Detailed description of the eight basic data types

  • The character type char
  • Byte short int long
  • Floating-point float double
  • The Boolean Boolean

The last

Recommend to you a more detailed Java zero based tutorial, the following is what I have seen feel quite good, worth watching the collection.

To share with you, click here…

If it helps you, thank thumb up for your support