Introduction to Java Primitives – Java原语介绍

最后修改: 2018年 1月 11日

1. Overview

1.概述

The Java Programming Language features eight primitive data types.

Java编程语言有八个原始数据类型。

In this tutorial, we’ll look at what these primitives are and go over each type.

在本教程中,我们将看看这些基元是什么,并对每种类型进行介绍。

2. Primitive Data Types

2.原始数据类型

The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren’t considered objects and represent raw values.

Java中定义的八个基元是intbyteshortlongfloatdoublebooleanchar>。这些不被视为对象,代表原始值。

They’re stored directly on the stack (check out this article for more information about memory management in Java).

它们直接存储在堆栈中(查看这篇文章以了解有关Java内存管理的更多信息)。

We’ll take a look at storage size, default values and examples of how to use each type.

我们将看一下存储大小、默认值和如何使用每种类型的例子。

Let’s start with a quick reference:

让我们先来看看快速参考。

Type Size (bits) Minimum Maximum Example
byte 8 -27 27– 1 byte b = 100;
short 16 -215 215– 1 short s = 30_000;
int 32 -231 231– 1 int i = 100_000_000;
long 64 -263 263– 1 long l = 100_000_000_000_000;
float 32 -2-149 (2-2-23)·2127 float f = 1.456f;
double 64 -2-1074 (2-2-52)·21023 double f = 1.456789012345678;
char 16 0 216– 1 char c = ‘c’;
boolean 1 boolean b = true;

2.1. int

2.1. int

The first primitive data type we’re going to cover is int. Also known as an integer, int type holds a wide range of non-fractional number values.

我们要讨论的第一个原始数据类型是int。也被称为整数,int类型可容纳广泛的非小数值。

Specifically, Java stores it using 32 bits of memory. In other words, it can represent values from -2,147,483,648 (-231) to 2,147,483,647 (231-1).

具体来说,Java使用32位内存来存储它。换句话说,它可以表示从-2,147,483,648(-231)到2,147,483,647(231-1)的值。

In Java 8, it’s possible to store an unsigned integer value up to 4,294,967,295 (232-1) by using new special helper functions.

在Java 8中,通过使用新的特殊辅助函数,可以存储一个无符号整数值,最高可达4,294,967,295(232-1)。

We can simply declare an int:

我们可以简单地声明一个int

int x = 424_242;

int y;

The default value of an int declared without an assignment is 0.

没有赋值的int声明的默认值是0.

If the variable is defined in a method, we must assign a value before we can use it.

如果变量被定义在一个方法中,我们必须在使用它之前分配一个值。

We can perform all standard arithmetic operations on ints. Just be aware that decimal values will be chopped off when performing these on integers.

我们可以对ints执行所有标准的算术运算。只是要注意,在对整数进行这些操作时,十进制值会被砍掉

2.2. byte

2.2. byte

byte is a primitive data type similar to int, except it only takes up 8 bits of memory. This is why we call it a byte. Because the memory size is so small, byte can only hold the values from -128 (-27) to 127 (27 – 1).

byte是一种类似于int的原始数据类型,只是它只占用8位内存。这就是为什么我们称它为byte。由于内存大小如此之小,byte只能容纳从-128 (-27) 到127 (27 – 1)的值。

Here’s how we can create byte:

下面是我们如何创建byte

byte b = 100;

byte empty;

The default value of byte is also 0.

byte的默认值也为0.

2.3. short

2.3.

The next stop on our list of primitive data types in Java is short.

Java中原始数据类型列表的下一站是short

If we want to save memory and byte is too small, we can use the type halfway between byte and int: short.

如果我们想节省内存,而byte太小,我们可以使用byteint之间的类型。short

At 16 bits of memory, it’s half the size of int and twice the size of byte. Its range of possible values is -32,768(-215) to 32,767(215 – 1).

在16位的内存中,它是int的一半大小,是byte的两倍大小。它的可能值范围是-32,768(-215)到32,767(215-1)。

short is declared like this:

short是这样声明的。

short s = 20_020;

short s;

Also similar to the other types, the default value is 0. We can use all standard arithmetic on it as well.

与其他类型类似,默认值也是0,我们也可以对其使用所有的标准算术。

2.4. long

2.4.

Our last primitive data type related to integers is long.

我们最后一个与整数有关的原始数据类型是long

long is the big brother of int. It’s stored in 64 bits of memory, so it can hold a significantly larger set of possible values.

longint的老大哥。它存储在64位的内存中,所以它可以容纳一个大得多的可能值。

The possible values of a long are between -9,223,372,036,854,775,808 (-263) to 9,223,372,036,854,775,807 (263 – 1).

长的可能值在-9,223,372,036,854,775,808(-263)到9,223,372,036,854,775,807(263 – 1)之间。

We can simply declare one:

我们可以简单地宣布一个。

long l = 1_234_567_890;

long l;

As with other integer types, the default is also 0. We can use all arithmetic on long that works on int.

与其他整数类型一样,默认值也是0。 我们可以在long上使用所有对int有效的算术。

2.5. float

2.5.float

We represent basic fractional numbers in Java using the float type. This is a single-precision decimal number. This means that if we get past six decimal points, the number becomes less precise and more of an estimate.

我们使用float类型在Java中表示基本的小数。这是一个单精度的小数。这意味着,如果我们超过六个小数点,这个数字就会变得不那么精确,而更像是一种估计。

In most cases, we don’t care about the precision loss. But if our calculation requires absolute precision (e.g., financial operations, landing on the moon, etc.), we need to use specific types designed for this work. For more information, check out the Java class Big Decimal.

在大多数情况下,我们并不关心精度的损失。但如果我们的计算需要绝对的精度(例如,金融操作、登陆月球等),我们就需要使用为这项工作设计的特定类型。欲了解更多信息,请查看Java类Big Decimal

This type is stored in 32 bits of memory just like int. However, because of the floating decimal point, its range is much different. It can represent both positive and negative numbers. The smallest decimal is 1.40239846 x 10-45, and the largest value is 3.40282347 x 1038.

这种类型和int一样,存储在32位的内存中。但是,由于是浮动小数点,其范围有很大不同。它可以表示正数和负数。最小的小数是1.40239846 x 10-45,最大的值是3.40282347 x 1038

We declare floats the same as any other type:

我们声明floats与任何其他类型相同。

float f = 3.145f;

float f;

And the default value is 0.0 instead of 0. Also, notice we add the f designation to the end of the literal number to define a float. Otherwise, Java will throw an error because the default type of a decimal value is double.

而且默认值是0.0而不是0.另外,注意我们在字面数字的末尾添加了f的指定来定义一个浮点数。否则,Java将抛出一个错误,因为小数值的默认类型是double

We can also perform all standard arithmetic operations on floats. However, it’s important to note that we perform floating point arithmetic very differently than integer arithmetic.

我们也可以对floats进行所有标准的算术操作。然而,需要注意的是,我们执行浮点算术的方式与整数算术非常不同。

2.6. double

2.6.double

Next, we look at double. Its name comes from the fact that it’s a double-precision decimal number.

接下来,我们看一下double。它的名字来自于它是一个双精度小数的事实。

It’s stored in 64 bits of memory. This means it represents a much larger range of possible numbers than float.

它存储在64位的内存中。这意味着它代表的可能数字范围比float大得多。

Although, it does suffer from the same precision limitation as float does. The range is 4.9406564584124654 x 10-324 to 1.7976931348623157 x 10308. That range can also be positive or negative.

虽然,它的确和float一样受到精度限制。范围是4.9406564584124654 x 10-324到1.7976931348623157 x 10308。这个范围也可以是正数或负数。

Declaring double is the same as other numeric types:

声明double与其他数字类型相同。

double d = 3.13457599923384753929348D;

double d;

The default value is also 0.0 as it is with float. Similar to float, we attach the letter D to designate the literal as a double.

默认值也是0.0,就像float那样。float类似,我们附加了一个字母D来指定字面意义为双数。

2.7. boolean

2.7. boolean

The simplest primitive data type is boolean. It can contain only two values: true or false. It stores its value in a single bit.

最简单的原始数据类型是boolean。它只能包含两个值。truefalse它的值存储在一个单一的比特中。

However, for convenience, Java pads the value and stores it in a single byte.

然而,为了方便起见,Java对该值进行了填充,并将其存储在一个字节中。

Here’s how we declare boolean:

下面是我们如何声明boolean

boolean b = true;

boolean b;

Declaring it without a value defaults to false. boolean is the cornerstone of controlling our programs flow. We can use boolean operators on them (e.g., and, or, etc.).

在没有值的情况下声明它,默认为falseboolean是控制我们程序流程的基石。我们可以对其使用布尔运算符(例如,and, or,等等)。

2.8. char

2.8.char

The final primitive data type to look at is char.

最后要看的原始数据类型是char

Also called a character, char is a 16-bit integer representing a Unicode-encoded character. Its range is from 0 to 65,535. In Unicode, this represents ‘\u0000′ to ‘\uffff’.

也称为字符,char是一个16位的整数,代表一个Unicode编码的字符。其范围从0到65,535。在Unicode中,它代表‘u0000’‘uffff’

For a list of all possible Unicode values, check out sites such as Unicode Table.

对于所有可能的Unicode值的列表,请查看网站,如Unicode表

Let’s now declare a char:

现在让我们声明一个char

char c = 'a';

char c = 65;

char c;

When defining our variables, we can use any character literal, and they will get automatically transformed into their Unicode encoding for us. A character’s default value is ‘/u0000′.

在定义我们的变量时,我们可以使用任何字符字面,它们会自动转化为Unicode编码。一个字符的默认值是‘/u0000’

2.9. Overflow

2.9. 溢出

The primitive data types have size limits. But what happens if we try to store a value that’s larger than the maximum value?

原始数据类型有大小限制。但如果我们试图存储一个大于最大值的值,会发生什么?

We run into a situation called overflow.

我们遇到了一种叫做溢出的情况。

When an integer overflows, it rolls over to the minimum value and begins counting up from there.

当一个整数溢出时,它将滚动到最小值,并从那里开始向上计数。

Floating point numbers overflow by returning Infinity:

浮点数通过返回无穷大而溢出。

int i = Integer.MAX_VALUE;
int j = i + 1;
// j will roll over to -2_147_483_648

double d = Double.MAX_VALUE;
double o = d + 1;
// o will be Infinity

Underflow is the same issue except it involves storing a value smaller than the minimum value. When the numbers underflow, they return 0.0.

下溢是同样的问题,只是它涉及到存储一个小于最小值的数值。当数字下溢时,它们返回0.0。

2.10. Autoboxing

2.10.自动排版

Each primitive data type also has a full Java class implementation that can wrap it. For instance, the Integer class can wrap an int. There is sometimes a need to convert from the primitive type to its object wrapper (e.g., using them with generics).

每个原始数据类型也都有一个完整的Java类实现,可以将其包裹起来。例如,Integer类可以包装一个int。有时需要从原始类型转换到它的对象封装器(例如,用generics使用它们)。

Luckily, Java can perform this conversion for us automatically, a process called Autoboxing:

幸运的是,Java可以为我们自动进行这种转换,这个过程被称为Autoboxing

Character c = 'c';

Integer i = 1;

3. Conclusion

3.总结

In this article, we’ve covered the eight primitive data types supported in Java.

在这篇文章中,我们已经介绍了Java中支持的八种原始数据类型。

These are the building blocks used by most, if not all, Java programs out there, so it’s well worth understanding how they work.

这些是大多数(如果不是全部)Java程序所使用的构件,所以很值得了解它们的工作原理。