简体   繁体   中英

What does a 'int' in parenthesis mean when giving a value to an int?

Here's an example I have.

public int getDelta()
    {
      long time = getTime();
      int delta = (int) (time - lastFrame);
      lastFrame = time;

    return delta;
}

In the fourth line, there's an "(int)". What does it mean?

This code is just an example I found with it; I'm not looking for a specific answer to the code I posted. What the (int) even does, generally. I've seen it done with (byte) too.

It means you're casting a long into an int. See: http://en.wikipedia.org/wiki/Type_conversion

You can't just say

int delta = (time - lastFrame);

Because time and lastFrame are longs (I'm assuming lastFrame is, at least). Therefore you must convert the type (cast) the result of that subtraction into an integer value.

When you cast with (int) , you discard any fraction number after the floating point. For example:

System.out.println((int) 15.5); // prints 15

That is cast to the primitive data type int. It will try casting the result produced by the calculation (which would be a long type in this case)into a primitive int (Integer) type.

So, if you had a result of 16.253353 from that line (as an example), the cast would return just 16, since int doesn't deal with decimal places, only whole numbers.

It's a conversion operator. It converts (time - lastFrame) to an integer.

As both time and lastFrame are long int s, there is no need for rounding.

If you had either a float or a double , the difference there might have been rounded down.

In your case it just uses a smaller data type: int , rather than long .

(typename) value or variable is called casting. It tells Java to treat the value as the type stipulated in the cast. Sometimes it's necessary because a method already written returns a type that's too big - you need an int and the method returns a long. Sometimes its used to cast a superclass to a subclass so that methods in the subclass can be called. If you cast primitives to other types make sure the size of the variable is "big" enough to hold the value or you'll get wierd overflow values. If you cast an object to a different type you have make sure the object is the correct type or you'll get a ClassCastException, for example - casting a String to a Date won't work because they're not in the same class hierarchy.

This cast are used to ensure that result will be returned in a int value. In this case, time is a long value (probably int64) to use getTime() function. But for delta you need to casto it to a int (int32) value. This cast can loose values.

Stating the data type in parenthesis makes the compiler perform an explicit cast to that type. It can be done with any type/object. For instance:

MyObject myobject = (MyObject) MyArray[0];

In your code I believe it will define your resulting variable as an int and not date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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