为什么每当长时间和双重工作时,Java中有如此多的数字类型? [英] Why are there so many types of number in Java when long and double work every time?

查看:176
本文介绍了为什么每当长时间和双重工作时,Java中有如此多的数字类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我一直在尝试学习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 Byte, signed = 1 sign bit and 7 magnitude bits (Read more on Wikipedia)
  • short = 2 Bytes, signed = 1 sign bit and 15 magnitude bits
  • int = 4 Bytes, signed = 1 sign bit and 31 magnitude bits
  • long = 8 Bytes, signed = 1 sign bit and 63 magnitude bits
  • float = 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 bits
  • char = 2 Bytes, unsigned = 16 magnitude bits
  • boolean = 1 Byte, unsigned = 8 bits
  • boolean in an array of booleans = 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 wrap short

  • 整数包裹 int

  • Long wrap long

  • Float 包裹 float

  • Double wrap double

  • 字符包裹 char

  • 布尔值包裹布尔值

  • Number 作为通用号码,可以是字节 Short 整数 Long Float Double 或自定义号码,如 BigInteger 。你甚至可以使用这个来制作自己的号码!

  • Byte wraps byte
  • Short wraps short
  • Integer wraps int
  • Long wraps long
  • Float wraps float
  • Double wraps double
  • Character wraps char
  • Boolean wraps boolean
  • Number acts as a generic number, and can be a Byte, Short, Integer, Long, Float, Double, or a custom number like BigInteger. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆