equals

Java Compare 2 integers with equals or ==?

旧城冷巷雨未停 提交于 2020-02-11 14:14:06
问题 i am very very new to Java and i would like to know how can i compare 2 integers? I know == gets the job done.. but what about equals? Can this compare 2 integers? (when i say integers i mean "int" not "Integer"). My code is: import java.lang.*; import java.util.Scanner; //i read 2 integers the first_int and second_int //Code above if(first_int.equals(second_int)){ //do smth } //Other Code but for some reason this does not work.. i mean the Netbeans gives me an error: "int cannot be

.equals() or == when comparing the class of an object

六月ゝ 毕业季﹏ 提交于 2020-01-30 05:40:41
问题 In java, when you are testing what the object's class is equal to would you use .equals() or == For example in the code below would it be: if(object.getClass() == MyClass.class) or if(object.getClass().equals(MyClass.class)) 回答1: You can use instanceof for that kind of comparision too, anyway, The Class implementation don´t override equals so comparing using == is correct, it will check the runtime class, f.e: this code will shows true: public static void main(String[] args) { Object object =

Using equals inside a generic class

我的未来我决定 提交于 2020-01-24 09:55:06
问题 I'd like my EqualTester generic class to call the overridden equals(...) method of its generic parameter, but it seems to call Object.equals instead. Here is my test code: import junit.framework.TestCase; public class EqualityInsideGenerics extends TestCase { public static class EqualTester<V> { public boolean check(V v1, V v2) { return v1.equals(v2); } } public static class K { private int i; private Object o; public K(Object o, int i) { this.o = o; this.i = i; } public boolean equals(K k) {

LINQ checking for duplicate objects (excluding ID)

扶醉桌前 提交于 2020-01-23 18:45:49
问题 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 "Car" object in C# with an overloaded constructor that I've created in my "Car" partial class. So for example: Car MyCar = new Car(17, 5); Now this nicely gives me a

equals() and operator “==” in java

99封情书 提交于 2020-01-21 01:44:25
问题 I know that equals() will compare the value of objects, the '==' operator will check if the variable point to the same memory. I do not understand how equals() compare the value of objects, for example: class Test { public Test(int x, float y) { this.x = x; this.y = y; } int x, float y; } Test test1 = new Test(1,2.0); Test test2 = new Test(1,2.0); So if I use equals() , will it compare each properties in each object? And what about if we are talking about String? using equals() and operator “

Is there a “not equal” in a linq join

杀马特。学长 韩版系。学妹 提交于 2020-01-19 02:56:25
问题 I am trying accomplish the LINQ query below but I need a "not equal" instead of equal, so that filteredEmployees has all employees from groupA minus groupB. List<Employee> groupA = getEmployeeA(); List<Employee> groupB = getEmployeeB(); var filteredEmployees = from a in groupA join b in groupB on a.Name equals b.Name select a; 回答1: You don't need a join for that: var filteredEmployees = groupA.Except(groupB); Note that this will be a sequence of unique employees - so if there are any

Why are two objects with the same values not equal? [duplicate]

时光怂恿深爱的人放手 提交于 2020-01-17 14:44:09
问题 This question already has answers here : How to determine equality for two JavaScript objects? (60 answers) Closed 5 years ago . I run the following in console why is the output false. Not asking how to compare two objects but why these two objects are not same. > a = {same:'same'} Object {same: "same"} > b = {same:'same'} Object {same: "same"} > a === b false > a == b false 回答1: Two objects are never the same even if they have the same content, as two different instances of Object is never

Why are two objects with the same values not equal? [duplicate]

我怕爱的太早我们不能终老 提交于 2020-01-17 14:43:31
问题 This question already has answers here : How to determine equality for two JavaScript objects? (60 answers) Closed 5 years ago . I run the following in console why is the output false. Not asking how to compare two objects but why these two objects are not same. > a = {same:'same'} Object {same: "same"} > b = {same:'same'} Object {same: "same"} > a === b false > a == b false 回答1: Two objects are never the same even if they have the same content, as two different instances of Object is never

Writing an equals() method for linked list object

血红的双手。 提交于 2020-01-17 04:27:11
问题 I'm having trouble with an equals(Object other) method within a class I'm building called LString . The class contains an object called LString that builds strings out of linked lists, and a few other methods. I'm working on two methods, compareTo() and equals() . The class is tested by running another file, called LStringTest , which outputs this error message: Running compareTo and equals tests (18 tests) Starting tests: .E.........E...E.E Time: 0.426 There were 4 failures: 1)

Does singleton means hashcode always return the same?

一世执手 提交于 2020-01-15 03:31:05
问题 I have two objects, o1 and o2 from the same class. If o1.hashcode() == o2.hashcode() , can I tell they are the same object? Beside o1==o2 , is there any other way to tell the singleton. 回答1: If you have a single instance of the class, the == and the equals comparison will always return true . However, the hashcode can be equal for different objects, so an equality is not guaranteed just by having equal hashcodes. Here is a nice explanation of the hashcode and equals contracts. Checking the