Detailed article eight basic types of Java!

Original link: https://bss.csdn.net/m/topic/dev_survey2019?source_id=zx

640?wx_fmt=gif

Since the release of Java, primitive data types is an important part of the Java language, this paper describes in detail under basic types for each specific use and limitations.

640?wx_fmt=jpeg

Author |  Jeremy Grifski
Translator |  meniscus , Zebian | Guo Rui
Exhibition | CSDN (ID: CSDNnews)

The following is the translation:

A few years ago, I began writing a series of articles about Java entry, I think it is necessary to some of the content is very detailed write out a separate article. In this way, those getting started easier to understand. First, let me explain about the basic types of Java 8.
As the title, Java language itself has eight basic types. In the following sections, let us take a look at the eight basic types. I will address each of the basic types, and describes the specific use restrictions.

640?wx_fmt=png
The basic type int

First, Java is an integer 32-bit signed (i.e., including both positive and negative) integers, int represented by the keyword:
int someNumber = 10;

Of course, like all basic types, Integer has its own limitations. Since it is only 32, so that the range -2147483648 to 2147483647. This figure great thing! Of course, we can confirm the following tips in using DrJava interactive panel:

Integer.MAX_VALUE // Prints 2,147,483,647
Integer.MIN_VALUE // Prints -2,147,483,648

Naturally, for the simple calculations, int is the most common type integer. If you need a larger range of numbers, please refer to the following long.

640?wx_fmt=png

double basic types

And int different, Java is a 64-bit double precision floating point type, represented by the double keyword:
double someNumber = 110.55;

Need to be reminded that the float is actually real. In other words, the double precision floating point decimal point.

Since the double type is 64-bit integer number than it represents a lot more. Similarly, we can use the interactive panel to confirm the scope of type double:
Double.MAX_VALUE // Prints 1.7976931348623157E308
Double.MIN_VALUE // Prints 4.9E-324

It should be noted that the negative index indicates the very small number, but not a very large negative number. So here's the range is not exactly the same as integer.

一般而言,double是在Java中使用浮点数的默认选择。另一个选择是float。

640?wx_fmt=png
char基本类型

我们已经看到,Java的字符类型表示16位字符,由char关键字表示:
char someCharacter = 'f';

Java中所有的字符都用单引号表示。同时,双引号用来表示字符串。我们稍后会讨论字符串。

与往常一样,我们可以通过下面的代码找出字符的范围:
Character.MAX_VALUE // Prints '???' 
Character.MIN_VALUE // Prints ''

为了让这个范围有意义,我们可以将结果转换成整数(稍后会更多地介绍):

(int) Character.MAX_VALUE // Prints 65535
(int) Character.MIN_VALUE // Prints 0

可见,char类型是Java中唯一的无符号类型。换句话说,字符的取值范围为0到65535,每个值映射到特定的字符。如果需要创建该范围之外的字符,可以将一对字符组合起来。参见“在Java中反转字符串”(https://therenegadecoder.com/code/reverse-a-string-in-java/)这篇文章中的例子。

640?wx_fmt=png

byte基本类型

当我们讨论二进制时,我们讨论的实际上是比特的概念。而8个比特组成一个字节,字节是Java支持的基本类型之一。本质上,byte类型只不过是取值范围为-128到127的8位整数。可以猜到,字节由byte关键字表示:
byte someByte = 20;

同样,可以利用下面的代码片段来确认byte类型的取值范围:

Byte.MAX_VALUE // Prints 127
Byte.MIN_VALUE // Prints -128

根据我的经验,byte类型在读取和处理原始数据时非常有用。但是一般而言,我们不会使用它,因为取值范围太小了。

640?wx_fmt=png

short基本类型

short是另一种整数类型,但它占用的空间要比int类型更小。实际上,它的占用空间正好是int类型的一半,为16位,由short关键字表示:
short someNumber = 11;

short类型的取值范围也只有整数的一半,我们可以用下述代码确认:

Short.MAX_VALUE // Prints 32767
Short.MIN_VALUE // Prints -32768

在实际应用中,short只有65546个可能的值。在内存空间和磁盘空间受限的情况下,我们会使用byte和short。但在其他情况下,在定义整数时默认使用int更为安全。

640?wx_fmt=png

long基本类型

与short相反的是long基本类型,即长整数。该类型用来表示比int类型还要大的非常大的数。long类型是64位有符号整数,其取值范围超过了10的18次方。
通常,长整数用long关键字表示:
long someBigNumber = 1013401346173L;

The following code can see how much 64-bit value:

Long.MAX_VALUE // Prints 9,223,372,036,854,775,807
Long.MIN_VALUE // Prints -9,223,372,036,854,775,808

Perhaps, long it may be used to calculate the distance light travels in a certain period of time. About 30 million meters of light propagating in one second. If you are writing a program to track the distance the light traveled, then 7 seconds after it is beyond the scope of the class of type int, long and about 975 types can be calculated. Do not believe me? This calculation can look gist (https://gist.github.com/jrg94/820d3f0f482dd19f0170964346381df0) in.

640?wx_fmt=png

float basic types

Although we usually use 64-bit floating-point type double, but Java also supports another floating-point type, called float. Int but similar, Java use case represents a double floating point by default. In any case, we can use float to represent the 32-bit floating-point type:
float someNumber = 11.4f;

type float range as follows:

Float.MAX_VALUE // Prints 3.4028235E38
Float.MIN_VALUE // Prints 1.4E-45

Seen, 32-bit floating-point range and precision are considerably smaller. If you do not double precision, while saving half the space, you can select the type float.

640?wx_fmt=png

boolean primitive type

Finally, we discuss the boolean type. Boolean type definitions can use boolean keyword:
boolean isBool = true;

Some special Boolean type, unlike other basic types as not a numerical value they represent. In fact, MAX_VALUE and MIN_VALUE techniques previously used can not be used here. Instead, it represents the true or false, that is, true and false.

Here, I do not intend to detail Boolean type, because to do anything will involve Boolean in Java. Nevertheless, we usually do not explicitly declare the Boolean type. In contrast, many of the comparison to the code are Boolean logic.
Original: https: //dev.to/renegadecoder94/the-8-primitive-types-in-java-10cl
This article CSDN translation, please indicate the source of the source.

【END】
640?wx_fmt=jpeg

 Thermal paper  recommended 

640?wx_fmt=gif Click here to read the original participation questionnaire, Gifts to send non-stop!
640?wx_fmt=png
Your point of each "look", I seriously as a favorite

Guess you like

Origin blog.csdn.net/csdnnews/article/details/102739505