Questions tagged [equals-operator]

173 questions
397
votes
14 answers

Can't operator == be applied to generic types in C#?

According to the documentation of the == operator in MSDN, For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true…
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
196
votes
7 answers

Java: Integer equals vs. ==

As of Java 1.5, you can pretty much interchange Integer with int in many situations. However, I found a potential defect in my code that surprised me a bit. The following code: Integer cdiCt = ...; Integer cdsCt = ...; ... if (cdiCt != null && cdsCt…
Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
121
votes
3 answers

What's the meaning of "(1,) == 1," in Python?

I'm testing the tuple structure, and I found it's strange when I use the == operator like: >>> (1,) == 1, Out: (False,) When I assign these two expressions to a variable, the result is true: >>> a = (1,) >>> b = 1, >>> a==b Out: True This…
Pythoner
  • 5,265
  • 5
  • 33
  • 49
101
votes
6 answers

Performance differences between equal (=) and IN with one literal value

How does SQL engines differ when we use equal sign and IN operator have same value? Does execution time changes? 1st one using equality check operator WHERE column_value = 'All' 2nd one using IN operator and single value WHERE column_value IN…
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
83
votes
3 answers

When is a C# value/object copied and when is its reference copied?

I keep getting the same issue over and over again where an object I want to reference is copied or where an object I want to copy is referenced. This happens when I use the = operator. For example, if I am sending the object to another form, ie:…
Mike Webb
  • 8,855
  • 18
  • 78
  • 111
76
votes
6 answers

What needs to be overridden in a struct to ensure equality operates properly?

As the title says: do I need to override the == operator? how about the .Equals() method? Anything I'm missing?
RCIX
  • 38,647
  • 50
  • 150
  • 207
32
votes
4 answers

Does == check for full equality in Booleans? - Java

So I've heard that if I compare 2 strings with == then I will only get true back if they both refer to the same object/instance. That's strings. What about Booleans?
Bluefire
  • 13,519
  • 24
  • 74
  • 118
28
votes
8 answers

What's wrong with defining operator == but not defining Equals() or GetHashCode()?

For the code below public struct Person { public int ID; public static bool operator ==(Person a, Person b) { return a.Equals(b); } public static bool operator !=(Person a, Person b) { return !a.Equals(b); } } Why does the compiler…
user541686
  • 205,094
  • 128
  • 528
  • 886
27
votes
3 answers

Equality operator overloads: Is (x!=y) == (!(x==y))?

Does the C++ standard guarantee that (x!=y) always has the same truth value as !(x==y)? I know there are many subtleties involved here: The operators == and != may be overloaded. They may be overloaded to have different return types (which only…
Marco13
  • 53,703
  • 9
  • 80
  • 159
27
votes
7 answers

Why equal operator works for Integer value until 128 number?

Why Integer == operator does not work for 128 and after Integer values? Can someone explain this situation? This is my Java environment: java version "1.6.0_37" Java(TM) SE Runtime Environment (build 1.6.0_37-b06) Java HotSpot(TM) 64-Bit Server VM…
gokhansari
  • 2,379
  • 1
  • 27
  • 33
26
votes
3 answers

C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals

I have a question about Object.Equals and Equals(object). My sample code is below: class Program { static void Main(string[] args) { var sb1 = new StringBuilder("Food"); var sb2 = new StringBuilder("Food"); …
amitabha
  • 646
  • 1
  • 9
  • 20
21
votes
2 answers

Does == actually work the same or different when comparing two primitives vs two Objects in Java?

When searching for explanations of how logical equals == works in Java the answers are always something along the lines of: For primitives it returns whether the primitives have the same value (this includes comparing a primitive to its…
user7382368
  • 293
  • 1
  • 9
21
votes
2 answers

Argument order for '==' with Nullable

The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, ==. (The type of IsInitialized is bool). Using C# 7.1 and .NET 4.7. static void A(ISupportInitialize x) { if ((x as…
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
20
votes
3 answers

Difference between == operator and Equals() method in C#?

What is the difference between == and Equals() with example? I know that == is used to compare operator and Equals() method is used to compare content of string.So i tried // first example string s1 = "a"; string s2 =…
Jui Test
  • 2,399
  • 14
  • 49
  • 76
16
votes
6 answers

Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc. giving back intuitive results as many programming languages. Whereas the normal equals operator always just returns…
fields
  • 879
  • 2
  • 9
  • 19
1
2 3
11 12