equals

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

I am revising a manual of best practices and recommendation coding java find it doubtful. The recommendation is as follows String variable; "xx".equals(variable) // OK variable.equals("xx") //Not recomended Because prevents appearance of NullPointerException that are not controlled Is this true? ...

How to make Joomla main content and modules columns match height dynamically?

Hi I have a Joomla site with a template that has 2 columns. One for the main content and the other for modules. On different pages either the content column is shorter than the module column or vice versa. How can I ensure that the 2 columns are always of the same height so that they both meet up equally at the footer on any page? ...

bash string equality

In bash, what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b" ]] && echo equal || echo not-equal results in: equal equal not-equal not-equal ...

Equals, GetHashCode, EqualityComparers and fuzzy equality

For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and overlapping date range, how would that be done? I have tried creating an EqualityComparer like so public override bool Equals(RateItem x, RateItem y) { ...

In `equals(T value)`, must T be Object, or can it be like City, etc?

I'm trying to understand the equals() method better. All examples I've seen do something like: public class City { public boolean equals(Object other) { if (other instanceof City && other.getId().equals(this.id)) { return true; } // ... } } Must the method take on Object and not...

Is there a reason to do a type comparison this way?

I'm used to seeing old code like if (true) { ... } where it's intuitively clear that someone was being either lazy or overly cautious when making a change. I ran across this snippet today, and I'm curious whether there's a functional difference between doing type comparison this way: private static bool logField(Type t, string f...

LINQ checking for duplicate objects (excluding ID)

Hi All, I am using LINQ to SQL (SQL Server) with C#. I have a table called "Cars" which automatically becomes the LINQ class/object called "Car". All well and good. Each car has a number of fields, say CarID(primary key int), EngineID, ColourID. I have 10 existing rows in the Cars table. Using all the cool LINQ stuff, I create a new...

How do I determine if a method is a generic instance of a generic method

Hi all, I have a MethodInfo passed in to a function and I want to do the following MethodInfo containsMethod = typeof(ICollection<>).GetMethod("Contains"); if (methodInfo.Equals(containsMethod) { // do something } But this doesn't work because the methodInfo has a specific generic type. For the example does work if I knew that th...

What does == do in Ruby?

In Java, == is the strongest kind of equality (pointer equality): a == b always implies a.equals(b). However, in Ruby, == is weaker than .equals?: ruby-1.9.2-rc2 > 17 == 17.0 => true ruby-1.9.2-rc2 > 17.equal?(17.0) => false So, where can I learn more about ==? What kind of checks should I expect when I compare two objects with it?...

Why returning false ? new Person("james") == new Person("james") ?

I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ? class Program { static void Main(string[] args) { Console.WriteLine(new Person("james") == new Person("james")); Console.ReadKey(); } } class Person { private strin...

Compare two strings regardless of case size in perl

is there anyway to compare two strings regardless of case size? For Example "steve" eq "STevE" <----- these would match "SHOE" eq "shoe" You get the picture ...

linq equals override

Can somebody help me to override equals operator in C# linq? This is the problem: var temp = from t1 in table1 join t2 in table2 on t1.column1 equals t2.column2 select t1.column4; it is worth mentioning that t1.column1 and t2.column2 are actually some specific types. If anyone needs more information, please let me know. Tnx ...

equals and hashCode

Hi, I am running into a question about equals and hashCode contracts: here it is Given: class SortOf { String name; int bal; String code; short rate; public int hashCode() { return (code.length() * bal); } public boolean equals(Object o) { // insert code here } } Which of the following will fulfi...

Two .net objects that are equal don't say they are

I have a the following code: object val1 = 1; object val2 = 1; bool result1 = (val1 == val2);//Equals false bool result2 = val1.Equals(val2); //Equals true What's up with that? is the only way to fix this to go with .Equals() method? ...

Equals(item, null) or item == null

Is code that uses the static Object.Equals to check for null more robust than code that uses the == operator or regular Object.Equals? Aren't the latter two vulnerable to being overridden in such a way that checking for null doesn't work as expected (e.g. returning false when the compared value is null)? In other words, is this: if (Eq...

Transitivity relationship: What is x.equals(z) when x.equals(y) is false and y.equals(z) is true

Assuming equals() is transitive; I understand that if x and y, have a bilateral agreement of being equal, then one of them, say y, does not enter into an agreement with a third class z on its own. But if we have a situation where x.equals(y) = false (still transitive) then what should this bilateral agreement with z be? ...

How to override the (final) equals method in java enums?

Hi, I have a problem with overriding the equals method in an Enum to make it compatible with other classes. The Enum implements an interface and the idea is that all implementations of this interface can be tested for equality, regardless of their type. For Example: public interface Group { public Point[] getCoordinates(); } publ...

Comparing two lists and removing duplicates from one

I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks - both of which contain Book objects. oldBooks is allowed to contain duplicate Book objects newBooks is not allowed to contain duplicate Book objects within itself and cannot include any duplicates of Book objects in the oldBooks list. The definitio...

Demonstrating string comparison with Java

I want to demonstrate with a few line of code that in Java, that to compare two strings (String), you have to use equals() instead of the operator ==. Here is something I tried : public static void main(String Args[]) { String s1 = "Hello"; String s2 = "Hello"; if (s1 == s2) System.out.println("same strings"); else ...

Determine if Equals() is an override?

I have an instance of Type (type). How can I determine if it overrides Equals()? ...