Java object equals null




















Try to amend it like this: if s1! Miquel Miquel I think you are comparing two objects. You should check first if the object is null and then if it isn't null use the methods equals. If you want to compare the empty string, is better that you put first the empty string on this way: "".

Manuel Cantonero Manuel Cantonero 1 1 silver badge 5 5 bronze badges. DayaMoon DayaMoon 2 2 silver badges 7 7 bronze badges. If s1 is null and s2 isn't, you're still going to execute s1. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 1. It matters, since equals can only return false or cause a NullPointerException or something different if the overriden equals method is nonsense. When your obj will be null it will throw Null Point Exception. Erick G.

Hagstrom 4, 1 1 gold badge 21 21 silver badges 38 38 bronze badges. The issue here is that field. This breaks usual Java behaviour and is therefore confusing. It should only work for field.

I don't know why the library developers thought, that this would be good to support. Btw, your first sentence has a grammar issue and it is unclear what you mean with it. Do you mean "Here is an example where str! I think that's because the jsonObject contains the "field" key that's why field isn't null, it has a reference which contains the json.

Yes, but I wouldn't treat Null like null and would use "null" instead. But I guess they did that to avoid requiring Strings. But even with that lib, field. Well, the result can only be false or NullPointerException if equals is not overriden to something bad. So I never get confused and avoid problems with this solution: if str. Matheus Marques Matheus Marques 41 2 2 bronze badges. If str is null this will be an NPE — typoerrpr.

Additionally an empty string "" having length 0 is something entirely different than a null reference i. I have encountered this case last night. Kevin Orriss Kevin Orriss 3 3 gold badges 9 9 silver badges 23 23 bronze badges. Is it not completely redundant to add a 2nd check for null? JustinRowe It is not only redundant, it is also very wrong. Please, never do something like x. Tom, JustinRowe please see my answer above why this isn't redundant nor complete garbage stackoverflow.

Show 1 more comment. Sign up or log in Sign up using Google. Note that it is a bit confusing that you call the method compare , it has a different meaning for strings.

You should call it equals. Add a comment. Active Oldest Votes. Improve this answer. Mark Rotteveel Mark Rotteveel The Objects. I like this approach. No need to include some ApacheCommons library for things these days.

SimonBaars This isn't running a function on a null object, this is a static method that you pass two arguments, which can be null, or a reference to an object. I don't want to reinvent the wheel in every single application I write — Neuron. Show 7 more comments. Cat Cat Why not just change the String types to Objects - making it more generic? And then it is the same as what you will get if you move to Java 7.

Which java class contains method you provided? Equals is not the same as compare. Compare should be useful for sorting. However, in this paradigm both ends play different roles, so it is to be expected that order matters.

As one final point. A null pointer exception should be raised when there's an error in your code. However, Asking an object if he is nobody, shouldn't be considered a programming flaw. I think it's perfectly ok to ask an object if he isn't null.

What if you don't control the source that provides you with the object? Would you check if the object is null and only afterwards see if they are equals?

Wouldn't it be more intuitive to just compare the two and whatever the second object is the comparison will be carried out without exceptions? In all honesty, I would be pissed if an equals method within its body returns a null pointer exception on purpose. Equals is meant to be used against any sort of object, so it shouldn't be so picky on what it receives. If an equals method returned npe, the last thing on my mind would be that it did that on purpose.

Specially considering it's an unchecked exception. The NullPointerException identifies that the problem is in the object against which the equals operation is being performed. If the NullPointerException was used as you suggest and you tried the sort of pointless operation of Is the NullPointerException thrown because your comparison function is screwed or because o1 is null but you didn't realise?

An extreme example, I know, but with current behaviour I feel you can tell easily where the problem lies. In the first case o1. In the second case, it throws NullPointerException because o2 is null. One cannot call any method on a null. It may be a limitation of programming languages in general, but we have to live with it. It is also not a good idea to throw NullPointerException you are violating the contract for the equals method and making things more complex than it has to be.

There are many common situations where null is not in any way exceptional, e. Hence, doing x. As for why null. The ordering of x and y in x.

I would argue that in almost all cases this reordering can be done based on what is known about the objects beforehand e.

And since this is the way it is specified, it is a programming error to break the contract and raise an exception for a null argument to equals. And if you consider the alternative of requiring an exception to be thrown, then every implementation of equals would have to make a special case of it, and every call to equals with any potentially null object would have to check before calling.

It could have been specified differently i. I think it's about convenience and more importantly consistency - allowing nulls to be part of the comparison avoids having to do a null check and implement the semantics of that each time equals is called. Using instance methods for equality, comparison etc. When I don't need polymorphism, I sometimes create a symmetric static method with two arguments, MyObject.

This method then checks whether one or both arguments are null references. If I specifically want to exclude null references, then I create an additional method e. To show that this is the standard, see ' Objects.

Now with equals returning false else clause will get executed, but not when throwing an exception. Also null is not really equal to say "2" so it makes perfect sense to return false.

Then it is probably better to insist null. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?



0コメント

  • 1000 / 1000