search
HomeJavajavaTutorialIntroduction to Java data types and character sets

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);

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") );

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How can I use Java's RMI (Remote Method Invocation) for distributed computing?How can I use Java's RMI (Remote Method Invocation) for distributed computing?Mar 11, 2025 pm 05:53 PM

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

How do I use Java's sockets API for network communication?How do I use Java's sockets API for network communication?Mar 11, 2025 pm 05:53 PM

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

How can I create custom networking protocols in Java?How can I create custom networking protocols in Java?Mar 11, 2025 pm 05:52 PM

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.