简体   繁体   English

如果结果必须是 int,为什么 Java 编译器允许 int/int 除法?

[英]Why does the Java compiler allow int/int division if the result must be an int?

I want to provide the actual page based on the entry index of a table's element.我想根据表格元素的条目索引提供实际页面。 I use PageRequest.of(int page, int size) .我使用PageRequest.of(int page, int size)

I have the index which is passed down to the function, and a constant PAGESIZE_SEARCH which is 50.我有传递给 function 的index和一个常量PAGESIZE_SEARCH ,它是 50。

I want to pass the current page according to the index as follows:我想根据索引传递当前页面如下:

PageRequest.of((int) index/PAGESIZE_SEARCH, PAGESIZE_SEARCH)

My IDEA (IntelliJ Ultimate 2020.1.1) greys my casting out and hinting Casting 'index' to 'int' is redundant .我的想法(IntelliJ Ultimate 2020.1.1)使我的投射变灰并暗示Casting 'index' to 'int' is redundant If I delete the cast even the compiler accepts it and I also don't get a Runtime Exception.如果我删除强制转换,即使编译器接受它,我也不会得到运行时异常。

Why is this?为什么是这样? Isn't the divsion unsafe there?那里的分区不安全吗? Any explanation would be appreciated.任何解释将不胜感激。

The division isn't unsafe;该部门并非不安全; it does exactly as it should do.它完全按照它应该做的那样做。

Dividing two integers is known as an integer division and simply returns the result of dividing the two numbers without the remainder.将两个整数相除称为 integer 除法,只返回两个数字相除的结果,没有余数。

Also, as pointed out in the comments, you're actually casting index to an int, rather than index/PAGESIZE_SEARCH - think about using brackets to be more precise: (int) (index / PAGESIZE_SEARCH) .此外,正如评论中所指出的,您实际上是将index转换为 int,而不是index/PAGESIZE_SEARCH - 考虑更精确地使用括号: (int) (index / PAGESIZE_SEARCH)

EDIT: You can read more on this topic here .编辑:您可以在此处阅读有关此主题的更多信息。

Mathematically speaking, the integers are not closed under divisison, meaning that the result of dividing two arbitrary integer like 3 and 5 may result in something that can not be represented by an integer.从数学上讲,整数在除法下不是闭合的,这意味着将两个任意 integer (如 3 和 5)相除的结果可能会导致 integer 无法表示的结果。 Each mathematical set of numbers (be it N, ℤ, ℚ, ℝ, ℂ) has certain limitations, and each of the representations of these sets in a computer has limitations as well.每个数学数字集合(无论是 N、ℤ、ℚ、ℝ、ℂ)都有一定的局限性,并且这些集合在计算机中的每个表示也有局限性。 Understanding those limitations is a somewhat important point.了解这些限制是很重要的一点。

Wrt.写。 division of two integers not being closed, you can deal with this in five different ways, mathematically speaking:两个整数的除法不被关闭,你可以用五种不同的方式处理这个问题,从数学上讲:

  • Making division a partial function.使除法部分 function。 This isn't desirable for a programming language that has to deal with arbitrary inputs.这对于必须处理任意输入的编程语言来说是不可取的。 Do you get an arithmetic exception every time you try to divide numbers for which division is undefined?每次尝试对未定义除法的数字进行除法时,是否都会出现算术异常? Not really a good approach.不是一个很好的方法。
  • Make division a function from the integer to the real numbers.将 function 从 integer 除以实数。 This is actually an approach you can use, but real numbers can not be represented exactly in computing hardware, so its only in approximate solution.这实际上是您可以使用的一种方法,但实数无法在计算硬件中精确表示,因此只能在近似解中。 Its certainly possible to implement programming languages this way, but in practice you are more likely interested in an integer result.它当然可以以这种方式实现编程语言,但实际上您更有可能对 integer 结果感兴趣。
  • Give the answer as a fractional result (Q).以分数结果 (Q) 形式给出答案。 eg 3/5 becomes ⅗, sometimes represented as the tuple (3,5).例如 3/5 变为 ⅗,有时表示为元组 (3,5)。 You can do something like this in some programming languages, eg Haskell.您可以在某些编程语言中执行类似的操作,例如 Haskell。
  • Give the answer as quotient and remainder.给出商和余数的答案。 Some programming languages do this, but Java does not support tuple types nor multiple return values, so this is out.一些编程语言会这样做,但是 Java 不支持元组类型,也不支持多个返回值,所以这个就out了。 This is sometimes called euclidean division .这有时称为欧几里得除法
  • Give only the quotient as answer, discard the remainder.只给出商作为答案,丢弃余数。 This is often called integer division , and is what Java implements.这通常称为integer 除法,是 Java 实现的。 You can recover the remainder via a related operation, the modulo operator % .您可以通过相关的操作,即模运算符%来恢复余数。

The JLS specifies how division works in §15.7.2 : JLS 在§15.7.2中指定了除法的工作方式:

Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d ⋅ q| Integer 除法向 0 舍入。也就是说,在二进制数值提升(第 5.6.2 节)之后为整数操作数 n 和 d 生成的商是 integer 值 q,其幅度尽可能大,同时满足 |d ⋅ q| ≤ |n|. ≤ |n|。 Moreover, q is positive when |n|此外,当 |n| 时 q 为正≥ |d| ≥ |d| and n and d have the same sign, but q is negative when |n|并且 n 和 d 具有相同的符号,但是当 |n| 时 q 为负≥ |d| ≥ |d| and n and d have opposite signs. n 和 d 符号相反。

There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend.有一种特殊情况不满足此规则:如果被除数是其类型可能最大量级的负 integer,除数为 -1,则 integer 发生溢出,结果等于被除数。 Despite the overflow, no exception is thrown in this case.尽管溢出,在这种情况下不会抛出异常。 On the other hand, if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.另一方面,如果 integer 除法中除数的值为 0,则引发 ArithmeticException。

Moreover, you are not actually casting the result of the division to int .此外,您实际上并没有将除法的结果转换为int You are only casting index to an int .只是index转换为int The cast is irrelevant and redundant, because integer divisions returns an int , as explained above.演员表是无关紧要的和多余的,因为 integer 部门返回一个int ,如上所述。

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

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