The following method purports to determine whether its sole argument is an odd number.Does the method work?
public static boolean isOdd(int i){
return i % 2 ==1;
}
Solution:
An Odd number can be defined as an integer that is divisible by 2 with a remainder of 1.The expression i % 2
computes the remainder when i is divided by 2, soit would semm that this program ought to work.Unfortunately, it
doesn't;it returns the wrong answer one quarter of the time;
Why one quarter? Because half of all int values are negative, and the isOdd method fails for all negative odd
values.It returns false when invoked on any negative value, whether even or odd.
This isa consequence of the definition of Java's remainder operator (%).It is defined to satisfy the following
identity for all int values a and all nonzero int values b:
(a / b) * b + (a % b)== a
In other words, if you divide a by b, multiply the result by b, and add the remainder, you rare back where you started
[JLS 15.17.3].This identity makes perfect sense, but in combination with Java's truncating integer division operator
[JLS 15.17.2],it implies that when the remainder operation returns a nonzero result, it has the same sign as its left
operand.
The is Odd method and the definition of the term odd on which it was based both assume that all remainders
are positive. Although this assumption make sense for some kinds of division [Boxing],Java's remainder operation is
perfect matched to its integer division operation, which discards the fractional part of its result.
When i is a negative odd number, i % 2 is equal to -1 rather than 1,so the isOdd method incorrectly returns
false.To prevent this sort of surprise,test that your method behave properly when passed negative, zero, and
positive values for each numerical parameters.
The problem is easy to fix. Simply compare i% 2 to 0 rather than to 1,and reverse the sense of the comparison:
public static boolean isOdd(inti){
return i % 2 != 0;
}
If you are using the isOdd method in a performance-critical setting, you would be better off using the bitwise
AND operator (&) in place of the remainder operator:
public static boolean isOdd(int i) {
return (i & 1) != 0;
}
The second version may run much faster than the first, depending on what platform and virtual machine you are
using, and is unlikely to run slower.As a general role, the divide and remainder operations are slow compared to
other arithmetic and logical operations.It's a bad idea to optimize prematurely, but in this case, the faster version is
as clear as the original, so there is no reason to prefer the original.
In summary, think about the signs of the operands and of the result whenever you use the remainder
operator.The behavior of this operator is obvious when its operands are nonnegative,but it isn't so obvious when
one or both operands are negative.
分享到:
相关推荐
:puzzle_piece: :brain: Puzzles允许您在定制的 Visual Studio Code 环境中提高编码技能并添加您自己的测试。 解决来自 Reddit 的 、 和 功能齐全的 IDE 中的问题,而不是<textarea /> (呃),并编写自定义...
本人在国外网站收集大量关于java puzzle方面资料,baidu, google都找不到资料,欢迎大家学习,以了解真正的java puzzle是什么。此相关资料是在effective java之后又一java程序员应该掌握的知识。
java-puzzlers 用 Java 编写的令人费解的游戏和实用程序
有两个证据可以证明这一点: 对于随机生成的15个难题,DFS总是会出现以下错误: java.lang.OutOfMemoryError: GC overhead limit exceeded如果我通过改组最终状态难题来创建15个拼图,那么随着改组次数的增加,DFS...
字谜 这是一个单词搜索生成器,但是在将单词添加到2D数组时遇到了麻烦。 我有一个检查方法,以查看是否可以将单词放置在2d数组中,但是由于某些原因它不起作用。 我不知道我在做什么错。 如果您发现我的错误或对改进...
该应用程序是一个简单的交换益智游戏。 在此游戏中,图像被切成相同大小的多个正方形块。 作品以随机顺序放置。... 要运行项目,请使用Puzzle.jar或Puzzle.exe。 有关完整的项目描述,请参见Wiki。
java puzzle,细小之处见真功。 有chm,doc,pdf三个版本,包括中英文版
难题 一个简单的益智游戏 您选择一个图像文件和件数,它会从中产生一个难题
Sliding Block Puzzles 是一款类似于 Rush Hour 的益智游戏,使用 Java Swing 库实现。 游戏由适合有限区域的许多棋子组成。 目标是将其中一个棋子(标记为“Z”的“目标棋子”)移动到特定位置。 通常,目标只能...
15件益智游戏 项目构想链接: : 方法: 单人游戏,系统将提示用户输入名称 将显示游戏布局,其中包括游戏面板,控件,历史记录面板,排行榜-根据完成游戏的移动次数和时间得出,而页脚面板则显示名称,时钟和移动...
This sampler contains one puzzle from each chapter of Java Puzzlers by Joshua Bloch and Neal Gafter (Addison Wesley, 2005). The book is filled with brainteas- ers about the Java programming language ...
java包装类源码mysterymaster-puzzles-java 该存储库包含com.mysterymaster.puzzles软件包的Java源代码。
1_Puzzles.java
puzzle collection. The collection's web site is at . If you've obtained the source code by downloading a .tar.gz archive from the Puzzles web site, you should find several Makefiles in the source code...
您必须修改Puzzle.java类才能使最不成功的成功。 预计时间4-8小时。 要求: 您可以使用任何编辑器(IntelliJ,Eclipse等)。 将项目导入为Maven项目。 您必须具有Maven 3或更高版本 Java 8或更高版本 您可以使用...
java智力题,本文档例举了许多在笔试中有很大可能会考核的智力题,主要涉及JAVA逻辑思维能力,以及部分算法
Links:Challenging Puzzle Game Template链接:具有挑战性的益智Unity游戏模板项目源码C# 版本1.2 支持Unity版本2020.1.10或更高 Links 是一款独特且具有挑战性的益智游戏,已准备好发布。 每个级别都会向玩家呈现...
学习编程的好书,推荐哦
puzzles:谜题
这个 repo 包含个人解决方案和各种编程练习的尝试。 包括: Yodle的JuggleFest Groupon 的欺诈检测(为 CodeSprint 2 完成,InterviewStreet 举办) Spotify 的三态内存(代码挑战) Spotify 的 Troll 问题(代码...