简体   繁体   English

给 int 赋值时,括号中的“int”是什么意思?

[英]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)".在第四行,有一个“(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.通常, (int) 甚至会做什么。 I've seen it done with (byte) too.我也看到它用 (byte) 完成了。

It means you're casting a long into an int.这意味着您正在将 long 转换为 int。 See: http://en.wikipedia.org/wiki/Type_conversion请参阅: 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).因为 time 和 lastFrame 很长(我假设 lastFrame 是,至少)。 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.当你施放(int)你丢弃浮点后的任何部分数量。 For example:例如:

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

That is cast to the primitive data type int.即转换为原始数据类型 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.它将尝试将计算产生的结果(在本例中为long类型)转换为原始int (Integer) 类型。

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.因此,如果该行的结果为 16.253353(作为示例),则转换将仅返回 16,因为 int 不处理小数位,只处理整数。

It's a conversion operator.它是一个转换运算符。 It converts (time - lastFrame) to an integer.它将(time - lastFrame)转换为整数。

As both time and lastFrame are long int s, there is no need for rounding.由于timelastFrame都是long int ,因此不需要四舍五入。

If you had either a float or a double , the difference there might have been rounded down.如果您有一个float或一个double ,则其中的差异可能已四舍五入。

In your case it just uses a smaller data type: int , rather than long .在您的情况下,它只使用较小的数据类型: int ,而不是long

(typename) value or variable is called casting. (typename) 值或变量称为强制转换。 It tells Java to treat the value as the type stipulated in the cast.它告诉 Java 将值视为转换中规定的类型。 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.有时这是必要的,因为已经编写的方法返回的类型太大 - 您需要一个 int 而该方法返回一个 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.如果您将一个对象转换为不同的类型,您必须确保该对象是正确的类型,否则您将收到 ClassCastException,例如 - 将 String 转换为 Date 将不起作用,因为它们不在同一个类层次结构中.

This cast are used to ensure that result will be returned in a int value.此强制转换用于确保结果将以 int 值返回。 In this case, time is a long value (probably int64) to use getTime() function.在这种情况下,时间是使用 getTime() 函数的长值(可能是 int64)。 But for delta you need to casto it to a int (int32) value.但是对于 delta,您需要将其强制转换为 int (int32) 值。 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.在您的代码中,我相信它会将您的结果变量定义为 int 而不是日期。

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

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