equals-operator

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

徘徊边缘 提交于 2019-11-26 20:13:04
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: SomeForm myForm = new SomeForm(); SomeObject myObject = new SomeObject(); myForm.formObject = myObject; ...and then modify the object in the form, the original object does not get modified. It is as if the object was copied and not referenced. Yet, when I do this: SomeObject myObject = new SomeObject(); SomeObject anotherObject = new SomeObject();

Should you use 'isEqual' or '=='?

血红的双手。 提交于 2019-11-26 17:16:59
问题 I saw a couple of questions here on SO, with ansers including the function isEqual: instead of the standard == . So far, I have only learned to use the == , so I'm wondering what's better to use, what are the pros and cons of each? When should you use them? Thank you. 回答1: They do different things; so you need to use the appropriate one: Consider, if you will: NSString *a = @"Hello!"; NSString *b = a; NSString *c = [a mutableCopy]; if (a == b) NSLog(@"This prints"); if (b == c) NSLog(@"This

How != and == operators work on Integers in Java? [duplicate]

徘徊边缘 提交于 2019-11-26 15:29:37
This question already has an answer here: Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java? 6 answers The following code seemed really confusing to me since it provided two different outputs.The code was tested on jdk 1.7. public class NotEq { public static void main(String[] args) { ver1(); System.out.println(); ver2(); } public static void ver1() { Integer a = 128; Integer b = 128; if (a == b) { System.out.println("Equal Object"); } if (a != b) { System.out.println("Different objects"); } if (a.equals(b)) { System.out.println("Meaningfully equal."); } }

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

此生再无相见时 提交于 2019-11-26 08:09:36
问题 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: SomeForm myForm = new SomeForm(); SomeObject myObject = new SomeObject(); myForm.formObject = myObject; ...and then modify the object in the form, the original object does not get modified. It is as if the object was copied and not referenced. Yet,

How != and == operators work on Integers in Java? [duplicate]

笑着哭i 提交于 2019-11-26 04:27:13
问题 This question already has answers here : Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java? (6 answers) Closed 3 years ago . The following code seemed really confusing to me since it provided two different outputs.The code was tested on jdk 1.7. public class NotEq { public static void main(String[] args) { ver1(); System.out.println(); ver2(); } public static void ver1() { Integer a = 128; Integer b = 128; if (a == b) { System.out.println(\"Equal Object\"); }

Why equal operator works for Integer value until 128 number? [duplicate]

眉间皱痕 提交于 2019-11-26 02:58:35
问题 This question already has an answer here: Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java? 6 answers 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 (build 20.12-b01, mixed mode) Sample Code: Integer a; Integer b; a = 129; b = 129; for (int i = 0; i < 200;

Java: Integer equals vs. ==

狂风中的少年 提交于 2019-11-26 01:19:23
问题 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 != null && cdiCt != cdsCt) mismatch = true; appeared to be incorrectly setting mismatch when the values were equal, although I can\'t determine under what circumstances. I set a breakpoint in Eclipse and saw that the Integer values were both 137,

Can&#39;t operator == be applied to generic types in C#?

≯℡__Kan透↙ 提交于 2019-11-25 22:58:58
问题 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 if its two operands refer to the same object. For the string type, == compares the values of the strings. User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described