Java中long、double、byte、char的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/415924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of long, double, byte, char in Java?
提问by Click Upvote
So I'm learning java, and I have a question. It seems that the types int
, boolean
and string
will be good for just about everything I'll ever need in terms of variables, except perhaps float
could be used when decimal numbers are needed in a number.
所以我正在学习java,我有一个问题。似乎 types int
, boolean
andstring
几乎适用于我在变量方面需要的所有东西,除非可能float
在数字中需要十进制数时使用。
My question is, are the other types such as long
, double
, byte
, char
etc ever used in normal, everyday programming? What are some practical things these could be used for? What do they exist for?
我的问题是,其他类型的如long
,double
,byte
,char
等曾经在正常的日常编程中使用?这些可以用于哪些实际用途?它们存在的目的是什么?
采纳答案by Neil Coffey
With the possible exception of "short", which arguably is a bit of a waste of space-- sometimes literally, they're all horses for courses:
除了“short”可能是个例外,这可能有点浪费空间——有时从字面上看,它们都是课程的马匹:
- Use an intwhen you don't need fractional numbers and you've no reason to use anything else; on most processors/OS configurations, this is the size of number that the machine can deal with most efficiently;
- Use a doublewhen you need fractional numbers and you've no reason to use anything else;
- Use a charwhen you want to represent a character (or possibly rare cases where you need two-byte unsigned arithmetic);
- Use a byteif either you specifically need to manipulate a signedbyte (rare!), or when you need to move around a blockof bytes;
- Use a booleanwhen you need a simple "yes/no" flag;
- Use a longfor those occasions where you need a whole number, but where the magnitude could exceed 2 billion (file sizes, time measurements in milliseconds/nanoseconds, in advanced uses for compacting several pieces of data into a single number);
- Use a floatfor those rare cases where you either (a) are storing a huge numberof them and the memory saving is worthwhile, or (b) are performing a massive number of calculations, and can afford the loss in accuracy. For most applications, "float" offers very poor precision, but operations can be twice as fast -- it's worth testing this on your processor, though, to find that it's actually the case! [*]
- Use a shortif you really need 2-byte signed arithmetic. There aren't so many cases...
- 当您不需要小数并且没有理由使用其他任何东西时,请使用int;在大多数处理器/操作系统配置中,这是机器可以最有效地处理的数字大小;
- 当您需要小数并且没有理由使用其他任何东西时,请使用双精度数;
- 当你想表示一个字符时使用char(或者可能需要两字节无符号算术的罕见情况);
- 使用一个字节,如果不是你特别需要操纵签署(!少见)字节,或者当你需要走动块字节;
- 当您需要一个简单的“是/否”标志时使用布尔值;
- 在需要整数但数量可能超过 20 亿的情况下使用long(文件大小,以毫秒/纳秒为单位的时间测量,在将多条数据压缩为单个数字的高级用途中);
- 对于那些您(a)存储大量它们并且节省内存是值得的,或者(b)正在执行大量计算并且可以承受精度损失的罕见情况,请使用浮点数。对于大多数应用程序,“float”提供的精度非常差,但操作速度可以提高两倍——不过,值得在您的处理器上测试这一点,以发现事实确实如此![*]
- 如果您确实需要 2 字节有符号算术,请使用short。没有那么多案例...
[*] For example, in Hotspot on Pentium architectures, float and double operations generally take exactly the same time, except for division.
[*] 例如,在奔腾架构的 Hotspot 中,除除法外,浮点运算和双精度运算通常花费完全相同的时间。
Don't get too bogged down in the memory usage of these types unless you reallyunderstand it. For example:
除非您真正理解这些类型的内存使用情况,否则不要太纠结于这些类型的内存使用情况。例如:
- every object size is rounded to 16 bytesin Hotspot, so an object with a single byte field will take up precisely the same space as a single object with a long or double field;
- when passing parameters to a method, every type takes up 4 or 8 bytes on the stack: you won't save anything by changing a method parameter from, say, an int to a short! (I've seen people do this...)
- Hotspot 中的每个对象大小都四舍五入为 16 字节,因此具有单字节字段的对象将与具有 long 或 double 字段的单个对象占用完全相同的空间;
- 将参数传递给方法时,每种类型都占用堆栈上的 4 或 8 个字节:将方法参数从 int 更改为 short 不会保存任何内容!(我见过有人这样做...)
Obviously, there are certain API calls (e.g. various calls for non-CPU intensive tasks that for some reason take floats) where you just have to pass it the type that it asks for...!
显然,在某些 API 调用(例如,出于某种原因需要浮点数的非 CPU 密集型任务的各种调用),您只需将其传递给它要求的类型...!
Note that String isn't a primitive type, so it doesn't really belong in this list.
请注意, String 不是原始类型,因此它并不真正属于此列表。
回答by guerda
The primitive data types are required because they are the basis of every complex collection.
原始数据类型是必需的,因为它们是每个复杂集合的基础。
long, double, byte etc. are used if you need only a small integer (or whatever), that does not waste your heap space.
如果您只需要一个小整数(或其他),则使用 long、double、byte 等,这不会浪费您的堆空间。
I know, there's enough of RAM in our times, but you should not waste it.
我知道,我们这个时代有足够的内存,但你不应该浪费它。
I need the "small ones" for database and stream operations.
我需要数据库和流操作的“小”。
回答by Kezzer
It's relative to the data you're dealing with. There's no point using a data type which reserves a large portion of memory when you're only dealing with a small amount of data. For example, a lot of data types reserve memory before they've even been used. Take arrays for example, they'll reserve a default amount (say, 256 bytes <-- an example!) even if you're only using 4 bytes of that.
它与您正在处理的数据有关。当您只处理少量数据时,使用保留大部分内存的数据类型是没有意义的。例如,许多数据类型甚至在使用之前就预留了内存。以数组为例,即使您只使用其中的 4 个字节,它们也会保留一个默认数量(例如,256 个字节 <-- 一个示例!)。
See this linkfor your answer
请参阅此链接以获取答案
回答by Romain Linsolas
You can have a look hereabout the primitive types in Java.
您可以在此处查看 Java 中的原始类型。
The main interest between these types are the memory usage. For example, intuses 32bits while byteonly uses 8bits.
这些类型之间的主要兴趣是内存使用情况。例如,int使用 32 位,而byte仅使用 8 位。
Imagine that you work on large structure (arrays, matrices...), then you will better take care of the type you are using in order to reduce the memory usage.
想象一下,您在处理大型结构(数组、矩阵...),那么您将更好地照顾您正在使用的类型以减少内存使用。
回答by Ben
I guess there are several purposes to types of that kind:
我想这种类型有几个目的:
1) They enforce restrictions on the size (and sign) of variables that can be stored in them.
1) 它们对可以存储在其中的变量的大小(和符号)进行了限制。
2) They can add a bit of clarity to code (e.g. if you use a char, then anyone reading the code knows what you plan to store in it).
2)他们可以为代码添加一些清晰度(例如,如果您使用字符,那么任何阅读代码的人都知道您打算在其中存储什么)。
3) They can save memory. if you have a large array of numbers, all of which will be unsigned and below 256, you can declare it as an array of bytes, saving some memory compared with if you declared an array of ints.
3)它们可以节省内存。如果您有大量数字,所有数字都将是无符号的且小于 256,则可以将其声明为字节数组,与声明整数数组相比,可以节省一些内存。
4) You need long if the numbers you need to store are larger than 2^32 and a double for very large floating point numbers.
4) 如果您需要存储的数字大于 2^32,则需要 long,对于非常大的浮点数则需要 double。
回答by Rolf Rander
A java int is 32 bits, while a long is 64 bits, so when you need to represent integers larger than 2^31, long is your friend. For a typical example of the use of long, see System.currentTimeMillis()
java int 是 32 位,而 long 是 64 位,所以当你需要表示大于 2^31 的整数时,long 是你的朋友。使用 long 的典型示例,请参见 System.currentTimeMillis()
A byte is 8 bits, and the smallest addressable entity on most modern hardware, so it is needed when reading binary data from a file.
一个字节是 8 位,是大多数现代硬件上最小的可寻址实体,因此从文件读取二进制数据时需要它。
A double has twice the size of a float, so you would usually use a double rather than a float, unless you have some restrictions on size or speed and a float has sufficient capacity.
double 的大小是 float 的两倍,因此您通常会使用 double 而不是 float,除非您对大小或速度有一些限制并且 float 有足够的容量。
A short is two bytes, 16 bits. In my opinion, this is the least necessary datatype, and I haven't really seen that in actual code, but again, it might be useful for reading binary file formats or doing low level network protocols. For example ip port numbers are 16 bit.
short 是两个字节,16 位。在我看来,这是最不需要的数据类型,我在实际代码中并没有真正看到它,但同样,它可能对读取二进制文件格式或执行低级网络协议很有用。例如 ip 端口号是 16 位。
Char represents a single character, which is 16 bits. This is the same size as a short, but a short is signed (-32768 to 32767) while a char is unsigned (0 to 65535). (This means that an ip port number probably is more correctly represented as a char than a short, but this seems to be outside the intended scope for chars...)
Char 表示单个字符,为 16 位。这与 short 的大小相同,但 short 是有符号的(-32768 到 32767),而 char 是无符号的(0 到 65535)。(这意味着 ip 端口号可能更正确地表示为字符而不是短,但这似乎超出了字符的预期范围......)
For the really authorative source on these details, se the java language specification.
有关这些细节的真正权威来源,请参阅java 语言规范。
回答by Ben Bowles
Integersshould be used for numbers in general.
Doublesare the basic data type used to represent decimals.
Stringscan hold essentially any data type, but it is easier to use ints and is confusing to use string except for text.
Charsare used when you only wish to hold one letter, although they are essentially only for clarity.
Shorts, longs, and floatsmay not be necessary, but if you are, for instance, creating an array of size 1,00000 which only needed to hold numbers less than 1,000, then you would want to use shorts, simply to save space.
一般来说,整数应该用于数字。
双精度数是用于表示小数的基本数据类型。
字符串基本上可以保存任何数据类型,但使用 int 更容易,并且除了文本之外使用字符串会令人困惑。
字符使用时,你只希望举行一个字母,但它们基本上仅仅是为了清楚。
空头、多头和浮点数可能不是必需的,但例如,如果您要创建一个大小为 1,00000 的数组,该数组只需要保存小于 1,000 的数字,那么您将需要使用空头,只是为了节省空间。