#Hi i am trying to call the getRelation(String name1, String name2) when the user presses 2
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
@quasi bronze
TL;DR: Comparing some types with ==, (especially String references) can have confusing results. Always use a.equals(b) for Strings.
A variable's type is either a primitive or a reference:
- Primitive values are, for example,
0or1forint,trueorfalseforboolean,'a'or'*'forchar... - Reference values are either
nullor a reference to an object. eg The value ofsinString s = "Foo"is reference to aStringobject, not the object itself.
Using == compares the values of two variables. If two reference values are the same, they refer to the same object or are both null.
Using a.equals(b) calls a method on a that compares its contents to the object referenced by b. You are asking if the two objects are alike.
Imagine you know the following three people:
janebob- Bob's identical twin brother
michael, also known asmike.
The following are examples of using == and .equals:
jane == bob- false. They are not the same personjane.equals(bob)- false. They are not alikebob == michael- false. They are not the same personbob.equals(michael)- true. They are alikemichael == mike- true. They are the same person
Strings are special in Java and, because of this, comparing references to Strings can be surprising. Though there are rare cases where it is useful and necessary to use == with String references, you should almost always be using .equals(Object) to compare the Strings of two references. For two strings a and b use a.eqauls(b) rather than a == b.
Note: a.equals(b) is intended to see if the two objects referenced by a and b are alike. How alike they must be depends on the implementation of equals for the class of a. The default implementation of equals, for a class extending Object, has the same behaviour as using ==.