Home > Java > javaTutorial > Introduction to Java data types and character sets

Introduction to Java data types and character sets

不言
Release: 2018-11-16 15:45:59
forward
3055 people have browsed it

This article brings you an introduction to Java data types and character sets. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is a data type

The simple understanding is the type of data.

what? How can data have types? Isn't data just bytecode composed of 0 or 1?

Yes, in a computer, only 0 or 1 can be stored. That is to say, from a storage point of view, there is no such thing as a type. They are indeed just 0 or 1. composed of bytecode.

Why are there data types?

Because types are equivalent to programmers (people), who divide data into different types to facilitate understanding and calculation.

For example:

 int a = 0x61;
 char b = 0x61;
 float c = 0x61;
 double d = 0x61;
 System.out.println(a+" "+b+" "+c+" "+d);
Copy after login

The output result is: 97 a 97.0 97.0.

Four different types of variables are given the same bytecode 0x61, but the output is actually different! why?

Because programmers (people) have assigned a type to the hexadecimal number 0x61, or given it a meaning.

Why do we need to assign a type (meaning)? The purpose is to allow the bytecode of 0 or 1 to represent something more specific, or to map it to something that humans can understand.

Without setting the type, you can also perform various operations on the number 0x61, but. . . what is the meaning?

It is meaningless. It is of practical significance to add, subtract, multiply and divide an integer. It is of practical significance to change a character from lowercase to uppercase. However, it is meaningless to operate a binary number. Things that people cannot understand are meaningless.

#So what are data types?

It is the way people look at data, the way people understand data, and the way people specify data. This is the data type.

The reason why data has types depends on people's opinions, not on the data itself. The data itself has no type.

#Why is the output of the above program different?

Because the output is to change the data into the format that people want and display it to others.

How does the computer know what format people want? By data type!

When 0x61 is specified as int type, the computer knows that it should display the decimal number 6*16 1 = 97.

When 0x61 is specified as char type, the computer knows that it should display the character 'a' corresponding to the ascii code represented by the decimal number 6*16 1 = 97.

Although they are the same bytecode 0x61 in the computer, the computer returns different results because humans specify the type.

From a coding and decoding perspective

Encoding is the conversion of information from one form or format to another This form of process, decoding, is the inverse of encoding.

Specifically speaking, encoding is to convert things that humans understand into things that computers understand, while decoding is to convert things that computers understand into things that humans understand.

What does a computer understand? We only understand 0 and 1, what about people? almost everything.

What is the bridge for conversion between them? is the data type! Only by stipulating the type of data and stipulating how to convert things understood by humans into bytecodes understood by computers can this conversion be completed!

Example:

For the number 97, humans can understand it as a decimal number, but the computer can only understand 0 or 1, so how to make the computer understand it? Code 97. How? If you use binary encoding, 97 becomes the number 1100001. In this way, the computer can understand (can store and calculate), so how do people understand the binary code 1100001? When the computer displays the number 1100001, it binary decodes it into 97, and people can understand it. The reason why it can be converted is because it stipulates the rules for binary encoding and decoding and stipulates that it is an integer. As for the character 'a', people understand it as a lowercase letter a. How to make the computer understand it? Or coding? What code? Code the ASCII code. The ASCII code of 'a' is 1100001. In this way, the computer can understand (can store and calculate). How can the computer understand 1100001 and decode it! ASCII decoding becomes 'a', so people can understand it again! .

Different things may get the same binary code using different encoding methods, and the same binary code may be understood as different things using different decoding methods!

Essentially speaking, what is a data type?

is actually the way of encoding and decoding data! ! !

Finally, what is a character set?

is the way to encode and decode characters!

Different character sets specify the character encoding (characters are converted into binary numbers) and decoding (binary numbers are converted into characters) methods.

System.out.println("你好".getBytes("utf-8") );
System.out.println("你好".getBytes("gbk") );
Copy after login

Output:

[B@677327b6
[B@14ae5a5

You can see that different character sets (utf-8 and gbk) put the same Chinese "Hello" is encoded into a different binary code.

Of course, the above output is not 0 and 1, it is obviously not a binary code. . . That's because the computer uses ASCII code to decode the binary code for you when displaying it. . . All turned into ASCII characters. why? I don't want it to decode, but display is decoding! ! !

Of course, binary strings can be output through some techniques, but this is not the point and will not be given here.

The last one

In what field did computers first apply encoding and decoding?

Assembly language!

Computer commands are also binary codes. Turning English words in assembly language into binary codes is encoding, and turning binary codes into assembly language words is decoding!


The above is the detailed content of Introduction to Java data types and character sets. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template