General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independent logic into the equals method? I noticed someone mention in another question that foo.equals((String)null) returns false whereas String.compareTo((String)null) throws a NullPointerException . What m
一般问题:在Java中实现对默认equals方法的重写时,我应该关注如何简单地使用已实现的compareTo方法与将独立逻辑写入equals方法? 我注意到有人在另一个问题中提到foo.equals((String)null)返回false,而String.compareTo((String)null)抛出一个NullPointerException 。 什么使这些不一致的结果成为理想的功能 样本equals方法: @Override public boolean equals(Object obj) { if (obj != null && obj instance
As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return null in circumstances that I don't want to actually handle (see below for a specific example), as the null indicates a problem with the software. The question is, should I check the return value for null and throw
正如标题所说,我想知道关于抛出NullPointerExceptions的最佳做法。 具体来说,如果我有一个外部库函数可以在我不想实际处理的情况下返回null (参见下面的具体示例),因为null表示软件有问题。 问题是,我应该 检查null的返回值并自己抛出NullPointerException,或者 当我尝试使用这个对象时,我应该让Java为我做一些肮脏的工作吗? 第一种方法让我添加一些额外的信息,因为我得到构建NullPointerException,但第二种
We've got an OO codebase where in quite a lot of cases hashcode() and equals() simply don't work, mostly for the following reason: There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction. That's a quote from "Effective Java" by Joshua Bloch
我们有一个OO代码库,在很多情况下, hashcode()和equals()根本不起作用,主要原因如下: 除非您愿意放弃面向对象抽象的好处,否则无法扩展可实例化的类并在保留等价契约的同时添加一个值组件。 这是来自Joshua Bloch的“Effective Java”的一句话,这里有一篇关于该主题的更多内容: http://www.artima.com/lejava/articles/equality.html 我们完全可以这么做,这不是这个问题的关键。 问题是:看到在某些情况下你不能
Comparable contract specifies that e.compareTo(null) must throw NullPointerException . From the API: Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false . On the other hand, Comparator API mentions nothing about what needs to happen when comparing null . Consider the following attempt of a generi
Comparable契约指定e.compareTo(null)必须抛出NullPointerException 。 从API: 请注意, null不是任何类的实例,即使e.equals(null)返回false , e.compareTo(null)应抛出NullPointerException 。 另一方面, Comparator API在比较null时没有提及什么需要发生。 考虑以下尝试使用Comparable的泛型方法,并返回一个Comparator ,它将null作为最小元素。 static <T extends Comparable<? super T>> Comparator
There are some patterns for checking whether a parameter to a method has been given a null value. First, the classic one. It is common in self-made code and obvious to understand. public void method1(String arg) { if (arg == null) { throw new NullPointerException("arg"); } } Second, you can use an existing framework. That code looks a little nicer because it only occupies a single l
有一些模式用于检查方法的参数是否已被赋予null值。 首先是经典之作。 这在自制代码中很常见,而且很明显。 public void method1(String arg) { if (arg == null) { throw new NullPointerException("arg"); } } 其次,你可以使用现有的框架。 该代码看起来更好一些,因为它只占用一行。 缺点是它可能会调用另一种方法,这可能会导致代码运行速度稍慢,具体取决于编译器。 public void method2(String arg) { A
The API for the Java Set interface states: For example, some implementations prohibit null elements and some have restrictions on the types of their elements I am looking for a basic Set implementation that does not require ordering (as ArrayList provides for the List interface) and that does not permit null . TreeSet, HashSet, and LinkedHashSet all allow null elements. Additionally, TreeSe
Java Set接口的API指出: 例如,一些实现禁止null元素,一些实现对元素类型有限制 我正在寻找一个基本的Set实现,它不需要排序(因为ArrayList提供了List接口)并且不允许为null 。 TreeSet,HashSet和LinkedHashSet都允许空元素。 此外,TreeSet还要求元素实现Comparable。 目前似乎没有这样的基本Set 。 有谁知道为什么? 或者如果有一个存在的地方我可以找到它? [编辑]:我不想允许null ,因为在后面的代码中,
Before calling a function of an object, I need to check if the object is null, to avoid throwing a NullPointerException . What is the best way to go about this? I've considered these methods. Which one is the best programming practice for Java? // Method 1 if (foo != null) { if (foo.bar()) { etc... } } // Method 2 if (foo != null ? foo.bar() : false) { etc... } //
在调用对象的函数之前,我需要检查对象是否为空,以避免抛出NullPointerException 。 什么是最好的方式去做这件事? 我已经考虑过这些方法。 哪一个是Java的最佳编程习惯? // Method 1 if (foo != null) { if (foo.bar()) { etc... } } // Method 2 if (foo != null ? foo.bar() : false) { etc... } // Method 3 try { if (foo.bar()) { etc... } } catch (NullPointerException e
I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6. A naive approach would have me add 2/12 and 6/12 and then reduce. How can I achieve a common denominator with the least performance pena
我正在尝试在Java中使用分数。 我想要实现算术函数。 为此,我首先需要一种方法来规范函数。 我知道我不能添加1/6和1/2,直到我有一个共同的标准。 我将不得不添加1/6和3/6。 一个天真的方法会让我增加2/12和6/12,然后减少。 我如何才能达到性能最低的共同标准? 什么算法最适合这个? 版本8(感谢hstoerr): 改进包括: equals()方法现在与compareTo()方法一致 final class Fraction extends Number {
This question already has an answer here: IllegalArgumentException or NullPointerException for a null parameter? [closed] 26 answers As far as I can see the problem is not in the jar export, but in your code: In your method BufferedImageLoader.loadImage(String path) (in the file BufferedImageLoader.java at line 15) you call ImageIO.read(InputStream input) . And you pass it a null object r
这个问题在这里已经有了答案: 对于null参数,IllegalArgumentException或NullPointerException? [已完成] 26个答案 据我可以看到问题不在jar导出,但在您的代码中: 在你的方法BufferedImageLoader.loadImage(String path) (在文件BufferedImageLoader.java 15行)你调用ImageIO.read(InputStream input) 。 并且将它传递给Class.getResourceAsStream(path)返回的null对象,并将其作为ImageIO.read(InputStream input
This question already has an answer here: IllegalArgumentException or NullPointerException for a null parameter? [closed] 26 answers What is a NullPointerException, and how do I fix it? 12 answers Probably you have less than 652 lines. file.readLine() returns null when there is no more line to read. Use your debugger and check the variable 'file'. NullPointerException will be
这个问题在这里已经有了答案: 对于null参数,IllegalArgumentException或NullPointerException? [已完成] 26个答案 什么是NullPointerException,以及如何解决它? 12个答案 可能你有少于652行。 file.readLine()在没有更多行要读取时返回null。 使用你的调试器并检查变量'文件'。 当你的变量为null并且你试图从null变量调用函数时,NullPointerException将被抛出。 很有可能你打电话 file.readLine()