[英]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:
两个整数的除法不被关闭,你可以用五种不同的方式处理这个问题,从数学上讲:
%
.%
来恢复余数。 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.