简体   繁体   English

表示数字而不用Java转换

[英]Representing numbers without casting in Java

Does a more natural way to represent those numbers without the use of casting exist in Java? Java是否存在一种更自然的方式来表示这些数字而不使用强制转换?

float tolerance = (float) 8.0e-7;
byte[] data = new byte[]{(byte) 0xFF , (byte) 0x15};

Not using casting on those instances just doesn't work at all, and it just looks weird when you are defining constants of a primitive type and you have to use casting to represent it (it is like you are saying that Java doesn't support those data types naturally which is ridiculous). 在这些实例上不使用强制转换根本不起作用,并且在定义基本类型的常量时看起来很奇怪,并且必须使用强制转换来表示它(这就像您在说Java不支持Java。这些数据类型自然是荒谬的)。

I understand that for the float number you can represent it like this: 我知道对于浮点数,您可以这样表示:

float tolerance = 0.0000008f;

but it is just almost unreadable in this form. 但是这种形式几乎是不可读的。

For float values, you can simply express within the literal that you want a float , and not a double (which requires the cast): 对于float值,您可以在文字中简单地表示需要float ,而不是double(需要强制转换):

float tolerance =  8.0e-7F;

There is no literal for bytes though (see here ). 虽然没有字节的文字(请参阅此处 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM