equality

Basic Java question: String equality

我与影子孤独终老i 提交于 2019-11-29 10:51:45
public class A { static String s1 = "I am A"; public static void main(String[] args) { String s2 = "I am A"; System.out.println(s1 == s2); } } Above program outputs "true". Both are two different identifiers/objects how the output is "true" ? My understanding is that the JVM will create different reference for each object, if so how the output is true? Neil Foley Java manages a String literal pool. It reuses these literals when it can. Therefore the two objects are actually the same String object and == returns true. I believe this is called string interning == checks that the variables are

Are objects with the same id always equal when comparing them with ==?

别来无恙 提交于 2019-11-29 10:30:06
问题 If I have two objects o1 and o2, and we know that id(o1) == id(o2) returns true . Then, does it follow that o1 == o2 Or is this not always the case? The paper I'm working on says this is not the case, but in my opinion it should be true! 回答1: Not always: >>> nan = float('nan') >>> nan is nan True or formulated the same way as in the question: >>> id(nan) == id(nan) True but >>> nan == nan False NaN is a strange thing. Per definition it is not equal nor less or greater than itself. But it is

Compare equality of char[] in C

大兔子大兔子 提交于 2019-11-29 09:36:41
I have two variables: char charTime[] = "TIME"; char buf[] = "SOMETHINGELSE"; I want to check if these two are equal... using charTime == buf doesn't work. What should I use, and can someone explain why using == doesn't work? Would this action be different in C and C++? char charTime[] = "TIME"; char buf[] = "SOMETHINGELSE"; C++ and C (remove std:: for C): bool equal = (std::strcmp(charTime, buf) == 0); But the true C++ way: std::string charTime = "TIME", buf = "SOMETHINGELSE"; bool equal = (charTime == buf); Using == does not work because it tries to compare the addresses of the first

Which equality test does Ruby's Hash use when comparing keys?

天涯浪子 提交于 2019-11-29 07:22:55
I have a wrapper class around some objects that I want to use as keys in a Hash. The wrapped and unwrapper objects should map to the same key. A simple example will be this: class A attr_reader :x def initialize(inner) @inner=inner end def x; @inner.x; end def ==(other) @inner.x==other.x end end a = A.new(o) #o is just any object that allows o.x b = A.new(o) h = {a=>5} p h[a] #5 p h[b] #nil, should be 5 p h[o] #nil, should be 5 I've tried ==, ===, eq? and hash all to no avail. Hash uses key.eql? to test keys for equality. If you need to use instances of your own classes as keys in a Hash, it

Is it important to override Equals if I'm implementing IEquatable<T>?

若如初见. 提交于 2019-11-29 06:58:43
I know the importance of overriding GetHashCode when implementing custom equality checks - for which I have implemented IEquality<T> interface, and also the difference between generic and non-generic Equals as discussed here. Now is there a point to override Equals(object t) ? Wouldn't everything come under generic Equals(T t) ? public override int GetHashCode() //required for hashsets and dictionaries { return Id; } public bool Equals(T other) //IEquatable<T> here { return Id == other.Id; } public override bool Equals(object obj) //required?? { return Equals(obj as T); } Unsealed types should

how equal operator works with primitive and object type data

断了今生、忘了曾经 提交于 2019-11-29 06:12:17
I know its a very basic question but I want to be clear about the concept. I want to know how == operator works in case of primitive and object type. For example Integer a = 1; int b = 1; System.out.println(a == b) how a is compared with b , whereas a contain the ref of object that contains value 1. Can somebody clearify it to me how it works internally? In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables contains. Now the variables of primitive data types contains the value itself while the reference types

Equality of functions in Haskell

孤者浪人 提交于 2019-11-29 05:59:50
I am trying to define a function which would take a Double -> Double function and return its mathematical derivative. I have tried doing the following: der :: (Double -> Double) -> (Double -> Double) der f | f == exp = exp | otherwise = undefined but Haskell does not support == on Double -> Double values. Is what I am trying to do impossible in Haskell? Yes, what you are trying to do is impossible in Haskell, and in general: deciding whether two functions are equal for all possible inputs (without just checking every input value, if that is even possible) is equivalent to solving the Halting

Should I use a concatenation of my string fields as a hash code?

旧巷老猫 提交于 2019-11-29 05:55:53
I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } } I'm implementing equality and so I need to override the hash code. At first I was going to use the hashcode formula from EJ but then I thought: These are all string fields, can't I just just use a StringBuilder to concatenate them and return the hash code from that string? That is: var str = new

How to detect array equality in JavaScript?

大兔子大兔子 提交于 2019-11-29 04:05:38
There are two arrays in JavaScript, they are both in the following format: [{'drink':['alcohol', 'soft', 'hot']}, {'fruit':['apple', 'pear']}]; I need to detect if the two arrays are equal or not. they are considered equal if they contain the same elements in a different order. How can I make that? Check the length of both arrays Loop through the first array, compare each variable to the second array. If 1 and 2 are both the same, your array is equal. Function to compare objects/arrays: Looping through true arrays can be achieved through for(var i=0; i<array.length; i++) . Walking through the

What is the best way to check two List<T> lists for equality in C#

白昼怎懂夜的黑 提交于 2019-11-29 03:08:31
There are many ways to do this but I feel like I've missed a function or something. Obviously List == List will use Object.Equals() and return false . If every element of the list is equal and present in the same location in the opposite list then I would consider them to be equal. I'm using value types, but a correctly implemented Data object should work in the same fashion (i.e I'm not looking for a shallow copied list, only that the value of each object within is the same). I've tried searching and there are similar questions, but my question is an equality of every element, in an exact