Java中long,double,byte,char的目的是什么? [英] What is the purpose of long, double, byte, char in Java?
问题描述
所以我正在学习java,我有一个问题。似乎类型 int
, boolean
和 string
将几乎所有我需要的变量都是好的,除非在数字中需要十进制数时可以使用 float
。
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.
我的问题是,其他类型是 long
, double
,<正常的日常编程中曾经使用code> byte , char
等?可以使用哪些实用的东西?他们存在什么?
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?
推荐答案
除了短之外,可能有点浪费空间 - 有时字面意思,他们'所有匹配课程的马匹:
With the possible exception of "short", which arguably is a bit of a waste of space-- sometimes literally, they're all horses for courses:
- 当你不需要小数时,请使用 int 没有理由使用其他任何东西;在大多数处理器/操作系统配置中,这是机器可以最有效地处理的数量大小;
- 当您需要小数时,请使用 double 没有理由使用其他任何东西;
- 当你想表示一个字符时使用 char (或者可能是需要双字节无符号算术的罕见情况) );
- 如果您特别需要操作签名字节(罕见!),或者需要时,请使用字节移动块字节;
- 当需要简单的是/否标志时,使用布尔;
- 在需要整数的情况下使用长,但幅度可能超过20亿(文件大小,时间测量,以毫秒/纳秒为单位,在高级用途中)将几个数据压缩成一个数字);
- 对于那些罕见的情况,请使用 float 要么(a)存储数量巨大,并且节省内存是值得的,或者(b)正在执行大量的计算,并且可以承担损失准确性。对于大多数应用来说,浮动提供的精度非常低,但操作速度可能是原来的两倍 - 但值得在您的处理器上进行测试,但事实并非如此! [*]
- 如果您真的需要2字节有符号算术,请使用短。没有那么多的情况......
- Use an int when 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 double when you need fractional numbers and you've no reason to use anything else;
- Use a char when you want to represent a character (or possibly rare cases where you need two-byte unsigned arithmetic);
- Use a byte if either you specifically need to manipulate a signed byte (rare!), or when you need to move around a block of bytes;
- Use a boolean when you need a simple "yes/no" flag;
- Use a long for 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 float for those rare cases where you either (a) are storing a huge number of 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 short if you really need 2-byte signed arithmetic. There aren't so many cases...
[*]例如,在Pentium架构上的Hotspot中,f loat和double操作通常需要完全相同的时间,除了除法。
[*] For example, in Hotspot on Pentium architectures, float and double operations generally take exactly the same time, except for division.
除非真的理解它,否则不要陷入这些类型的内存使用中。例如:
Don't get too bogged down in the memory usage of these types unless you really understand it. For example:
- Hotspot中的每个对象大小都舍入为16字节,因此具有单个字节的对象字段将占用与具有长字段或双字段的单个对象完全相同的空间;
- 将参数传递给方法时,每个类型占用4或8个字节stack :你不会通过将一个方法参数从int更改为short来保存任何东西! (我见过人们这样做......)
- every object size is rounded to 16 bytes in 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...)
显然,有一些API调用(例如,对非CPU的各种调用)密集的任务,由于某种原因采取浮动)你只需要传递它要求的类型...!
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...!
注意String不是基本类型,所以它并不属于这个列表。
Note that String isn't a primitive type, so it doesn't really belong in this list.
这篇关于Java中long,double,byte,char的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!