简体   繁体   English

Java不兼容类型场景

[英]Java incompatible types scenario

I'm learning Java and I've come across a scenario that has me confused. 我正在学习Java,并且遇到了使我感到困惑的场景。

Here is some code: 这是一些代码:

     long frameLength = stream.getFrameLength(); 
     int frameSize = stream.getFormat().getFrameSize(); 


    //total number of bytes = bytes per frame * total number of frames

    byte[] totalBytes = new byte[frameLength * frameSize];

The int type cannot take the value of stream.getFrameLength() as it's too big. int类型不能使用stream.getFrameLength()的值,因为它太大。 That's why we use long . 这就是为什么我们使用long的原因。

But we can't use long because we need to pass in the value of (frameLength * frameSize) into a byte[] holder which has to be an int because java needs int sized arrays. 但是我们不能使用太长,因为我们需要将(frameLength * frameSize)的值传递给byte []持有人,该持有人必须是一个int,因为java需要int大小的数组。

So, the above code will not work. 因此,以上代码将不起作用。

After some research I discovered this: 经过一番研究,我发现了这一点:

int frameLength = (int) stream.getFrameLength(); 

That seems to work, I'm confused about the extra "(int)" part added, what is this and what does it mean? 这似乎可行,我对添加的额外“(int)”部分感到困惑,这是什么意思?

How is the above different to: 以上与以下内容有何不同:

 int frameLength = stream.getFrameLength(); or
 long frameLength = stream.getFrameLength(); 

Here's the working code: 这是工作代码:

AudioInputStream stream = AudioSystem.getAudioInputStream(f);
    int frameLength = (int) stream.getFrameLength(); //This is the number of bytes per frame in the audio stream
    int frameSize = (int) stream.getFormat().getFrameSize(); //Number of frames in the audio stream.

       //total number of bytes = bytes per frame * total number of frames

    byte[] totalBytes = new byte[frameLength * frameSize];

I'm confused, I'd appreciate some help! 我很困惑,我将不胜感激!

That seems to work, I'm confused about the extra "(int)" part added, what is this and what does it mean? 这似乎可行,我对添加的额外“(int)”部分感到困惑,这是什么意思?

It casts whatever comes out of stream.getFrameLength() to an integer. 它将stream.getFrameLength()产生的所有内容强制转换为整数。 Note that if it was a long that is larger than Integer.MAX_VALUE you will get an integer overflow . 请注意,如果它的长度大于Integer.MAX_VALUE ,则将导致整数溢出

If you really need that int initialization of the byte array I would do the multiplication first: 如果您确实需要字节数组的int初始化,那么我将首先进行乘法:

long frameLength = stream.getFrameLength();
int frameSize = stream.getFormat().getFrameSize();

Then see if you won't get integer overflow: 然后看看是否不会出现整数溢出:

long length = frameSize * frameLength // its a long because that won't overflow as fast as int.
if (length > Integer.MAX_VALUE)
    throw new IllegalStateException("Frame too large. Integer overflow!")
else 
   byte[] totalBytes = new byte[frameLength * frameSize];

This assumes the overflow won't happen (since we do not handle the error). 这假定溢出不会发生(因为我们不处理错误)。 If you do encounter this, you should find another solution in stead of a byte array. 如果确实遇到这种情况,则应该找到另一个解决方案来代替字节数组。

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

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