equals

Java null String equals result

 ̄綄美尐妖づ 提交于 2019-12-04 00:29:02
Please help me how does the string.equals in java work with null value? Is there some problem with exceptions? Three cases: boolean result1,result2, result3; //1st case String string1 = null; String string2 = null; result = string1.equals(string2); //2nd case String string1 = "something"; String string2 = null; result2 = string1.equals(string2); //3rd case String string1 = null; String string2 = "something"; result3 = string1.equals(string2); What the values of results are? I expect this values: result1 is true; result2 is false; result3 is false; You cannot use the dereference (dot, '.')

How should we really be implenting Equals and GetHashCode for NHibernate entities

自古美人都是妖i 提交于 2019-12-04 00:27:07
问题 There are many questions and answers and articles to this question available but in my opinion there seems to be no real clear/correct answer For me Ayende has the best generic implementation so far that I've seen : http://ayende.com/blog/2500/generic-entity-equality ....But it is from 2007 .... Is this the 'best way' to implement these methods especially with regard to NHibernate 3.2 which contains some differences in proxy implementation to earlier versions? 回答1: Yes! You should be

Java : “xx”.equals(variable) better than variable.equals(“xx”) , TRUE?

落花浮王杯 提交于 2019-12-03 22:52:11
I'm reviewing a manual of best practices and recommendation coding java I think is doubtful. Recomendation: String variable; "xx".equals(variable) // OK variable.equals("xx") //Not recomended Because prevents appearance of NullPointerException that are not controlled Is this true? Mark Byers This is a very common technique that causes the test to return false if the variable is null instead of throwing a NullPointerException . But I guess I'll be different and say that I wouldn't regard this as a recommendation that you always should follow. I definitely think it is something that all Java

Java - is there a “subclassof” like instanceof?

£可爱£侵袭症+ 提交于 2019-12-03 22:18:43
Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! instanceof can handle that just fine. Adrian With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass of Event, but not Event itself } By default instanceof checks if an object is of the class specified or a

why is that str.count('') ≠ (from str.count('A') + str.count('B') + … + str.count('Z'))

点点圈 提交于 2019-12-03 21:55:20
It (should, to me,) say True if there are only vowels in the string(phrase); otherwise says False . I don't understand why it always will return False , since (x >= x) always returns True . I thank anyone for checking the solution to this query. (str) -> bool def valid_letter_sequence(abc): valid_letters = abc.count('A') + abc.count('E') + abc.count('I') + abc.count('O') + abc.count('U') counted_letters = abc.count('') if valid_letters >= counted_letters: return True else: return False Observe: >>> 'abc'.count('') 4 Passing an empty string to count gives you one more than the length of the

Overriding hashcode and equals method in java?

删除回忆录丶 提交于 2019-12-03 21:22:45
I have the classes below: public class Sample implements java.io.Serializable{ //POJO with two fields and getters/setters private String name; private Integer id; //This POJO does not override equals() and hashCode() } public class B{ private Sample sample; //here i need override hashcode and equals() based on **sample** property. } When i tried overriding equals() and hashCode() in the B class I got the error below in Eclipse. The field type com.mypackage.Sample does not implement hashCode() and equals() - The resulting code may not work correctly. Now how can I compare two B instances

Windows Batch check if variable starts with, ends with and contains a specific string

▼魔方 西西 提交于 2019-12-03 18:01:54
问题 I'm trying to check if a variable in a batch file starts with " contains BETA somewhere and ends with ") . Is it possible? And if yes, may somebody help me with that? 回答1: @ECHO OFF SETLOCAL SET var=abc&CALL :check SET var="abc"&CALL :llcheck SET var="")&CALL :check SET var=")"&CALL :llcheck SET var=abc")"&CALL :llcheck SET var=xyzbetazyx&CALL :check SET var="xyzbetazyx"&CALL :llcheck SET var=xyzbetazyx")"&CALL :llcheck SET var=xyzbetazyx")"&CALL :check SET var="xyzbetazyx")&CALL :check GOTO

hashCode() method when equals() is based on multiple independent fields

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:26:35
问题 i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for such an equals() so that the general contract of hashCode being equal when equals returns true is preserved? public class MyClass { int id; String name; public boolean equals(Object o) { if (!(o instanceof MyClass)) return false; MyClass other = (MyClass) o; if (other.id == this.id || other.name == this.name) return

Casting in equals method

梦想的初衷 提交于 2019-12-03 17:20:43
问题 I have a question about overriding the equals method in Java. In my book, I have the following example: public class Dog{ private String name; private int age; public boolean equals(Object obj) { if(!(obj instanceof Dog)) return false; Dog other = (Dog) obj; ---> confused here if(this.name.equals(other.name) && (this.age == other.age) { return true; } else { return false; } } } I don't understand why why have to cast the reference to the Dog reference. If that reference is not of type Dog we

Java - Why can't I use charAt() to see if a char equals another?

倖福魔咒の 提交于 2019-12-03 16:21:46
I want to see if a character in my string equals a certain other char value but I do not know what the char in the string is so I have used this: if ( fieldNames.charAt(4) == "f" ) But I get the error: "Operator '==' cannot be applied to 'char', 'jav.lang.String'" But "g" == "h" seems to work and I know you can use '==' with char types. Is there another way to do this correctly? Thanks! Madhawa Priyashantha if ( fieldNames.charAt(4) == 'f' ) because "f" is a String and fieldNames.charAt(4) is a char . you should use 'f' for check char "g" == "h" works because both g and h are Strings and you