为什么每当长时间和双重工作时,Java中有如此多的数字类型? [英] Why are there so many types of number in Java when long and double work every time?
问题描述
现在我一直在尝试学习Java编程,我想知道为什么我们使用的东西就像 Float
, short
和 int
当我们可以使用 Long
和 Double
?
Now I have been trying to learn Java Programming, I want to know why do we use things like Float
, short
, and int
when we could be just be using Long
and Double
?
我不明白那部分。
推荐答案
很棒的问题,特别是如果你来自一个类似 JavaScript
这样的语言,它不区分数字类型。
Great question, especially if you're coming from a language like JavaScript
which does not make a distinction between types of numbers.
Java比这些语言要严格得多,而您编写的所有内容首先被编译为所谓的字节代码,这就像汇编语言一样,但只能由Java虚拟机JVM)。因此,您必须准确指定需要多少位来表示您的数据。即使使用一个更抽象的概念,如一个 String
,仍然成为JVM可以读取的代码,正好表示它代表多少位。
Java is a bit more strict than those languages, and everything you write is first compiled to what is called byte code, which is sort of like assembly language, but it can only be read by the Java Virtual Machine (JVM). Because of this, you must specify exactly how many bits you need to represent your data. Even using a more abstract concept like a String
, that still becomes code that the JVM can read which says exactly how many bits it represents.
这是Java如何分解:
Here is how it breaks down in Java:
-
byte
= 1字节,有符号= 1个符号位和7个大小位(阅读更多维基百科< a>) -
short
= 2字节,有符号= 1符号位和15幅位 -
int
= 4字节,有符号= 1符号位和31幅位 -
long
= 8字节,有符号= 1符号位和63幅位 -
float
= 4字节,signed = 1符号位,8个指数位,23个尾数位(阅读更多维基百科) -
double
= 8字节,有符号= 1符号位,11指数位, 52尾数位 -
char
= 2字节,无符号= 16级位 -
boolean
= 1字节,无符号= 8位 -
boolean
code> boolean s = 1 nibble = 4位
byte
= 1 Byte, signed = 1 sign bit and 7 magnitude bits (Read more on Wikipedia)short
= 2 Bytes, signed = 1 sign bit and 15 magnitude bitsint
= 4 Bytes, signed = 1 sign bit and 31 magnitude bitslong
= 8 Bytes, signed = 1 sign bit and 63 magnitude bitsfloat
= 4 Bytes, signed = 1 sign bit, 8 exponent bits, 23 mantissa bits (Read more on Wikipedia)double
= 8 Bytes, signed = 1 sign bit, 11 exponent bits, 52 mantissa bitschar
= 2 Bytes, unsigned = 16 magnitude bitsboolean
= 1 Byte, unsigned = 8 bitsboolean
in an array ofboolean
s = 1 nibble = 4 bits
请注意,这些都是小写即可。这意味着它们是原语,并构成Java中所有数据的构建块。
Note that these are all lower-case. This means that they are primitives, and make up the building blocks of all data in Java.
有也是Java有时用来包装原语的这些 Title-Case 类(包装是当你使用一个类来表示类中的一个对象,比如买一个平板电脑并把它放在盒子里)关于平板电脑,但是你可以围绕它来临时表示并持有它)。这些如下:
There are also these Title-Case classes that Java sometimes uses to wrap primitives (wrapping is when you use a class to represent an object inside the class, like buying a tablet and getting it in the box. You only care about the tablet, but you get the box around it to temporarily represent and hold it). These are as follows:
-
字节
包裹/ code>
-
Short
wrapshort
-
整数
包裹int
-
Long
wraplong
-
Float
包裹float
-
Double
wrapdouble
-
字符
包裹char
-
布尔值
包裹布尔值
-
Number
作为通用号码,可以是字节
,Short
,整数
,Long
,Float
,Double
或自定义号码,如BigInteger
。你甚至可以使用这个来制作自己的号码!
Byte
wrapsbyte
Short
wrapsshort
Integer
wrapsint
Long
wrapslong
Float
wrapsfloat
Double
wrapsdouble
Character
wrapschar
Boolean
wrapsboolean
Number
acts as a generic number, and can be aByte
,Short
,Integer
,Long
,Float
,Double
, or a custom number likeBigInteger
. You can even use this to make your own numbers!
您绝对可以为所有的数字使用 double
和 long
!但是,当你处理这么多的数字(记住:从字面上来说,Java中的所有内容都归结为数字),你应该尽可能少的数据来完成任务,所以你不会用完内存
You can definitely just use double
and long
for all your numbers! But, when you're dealing with so many numbers (remember: literally everything in Java boils down to numbers), you should use the smallest amount of data possible to accomplish a task, so you don't run out of memory.
这是一种做法,即使像谷歌这样的大公司,我们已经看到当江南风格的视图柜台超过 int
的限制时,显示他们首先仅使用32位数的计数器,而不是64位数。现在他们需要它们,但是他们更新了。这是我建议你采用的做法!
This is a practice that even such big companies as Google use, as we've seen when the view counter for Gangnam Style surpassed the limit of an int
, showing that they preferred to use only a 32-bit number for the counter at first instead of a 64-bit one. Now that they need it, though, they updated. This is a practice I suggest you adopt!
这篇关于为什么每当长时间和双重工作时,Java中有如此多的数字类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!