This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
What is the difference between == and equals() in Java?
(26 answers)
Closed 4 years ago.
the following is the equals method from the String class:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value,aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
What does the comparison: 'this == anObject' at the first if statement mean?
It compares the memory addresses for both the object passed as parameter and the one you call equals from. If they are in the same memory address, they are obviously the same object.
Otherwise, it keeps checking for other ways to compare if they are actually equivalent objects.
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 3 years ago.
I am new to Java and StackOverflow but as i have read some of the answers about equals they stated :
Equals method compares two objects for their identity and if they are
identical it returns TRUE . whereas if you don't override the method Equals
it acts like ==(which returns true if 2 variables refer to the same object).
Integer x = new Integer(4);
Integer y = new Integer(4);
System.out.println(x.equals(y));
System.out.println(x == y);
If the queries above are correct why does this code print TRUE and FALSE since we are not overriding the method equals?
Because class Integer does override the equals method and it's implementation is the following:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
This question already has answers here:
Is null check needed before calling instanceof?
(8 answers)
Closed 4 years ago.
I have written a simple equals() method for a class:
#Override
public boolean equals(Object obj) {
if(obj instanceof Cl) {
Cl u = (Cl)obj;
return u.i == i;
} else {
return false;
}
}
As I know, if I want compare if the class object equals to a null object, this returns false because of the instanceof, but according to my uni teacher this is a bad implementation, as the null check is missing. Can someone confirm for me if my theory is correct or not?
I think, a null check is not required and should not be there, because in this case there will not be any compile-time error or any exception if obj is null, because of the check if (obj instaceof C1).
This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 6 years ago.
I need to write a new method that checks if certain values are of the type String or not.
I have two objects and I want to be able to check if these two objects are strings, if they are to then return true, and false otherwise.
I did start off with the following method:
public boolean stringTest()
{
boolean aString;
}
But could not get anything to work after that, any help would be greatly appreciated!
You can make use of instanceof and rewrite your method
public boolean stringTest(Object any)
{
return any instanceof String;
}
then
stringTest(townName); // true
stringTest(new Integer()); // false
Use instanceof:
public boolean isString(Object o) {
return o instanceof String;
}
This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When we do
String a=new String("mac");
String b=new String("mac");
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
condition 1 fails and condition 2 is true since b and a are two different objects
But when we do
String a="mac";
String b="mac";
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
Both the conditions are true. Why didn't java create a new object for second case. If java creates a new object only when we use new() then if we give different values to both the strings then what happens internally in java?
When you declare like below, Java create String literals in the String constant pool, and both references a and b will refer that String object "mac" in the pool
String a="mac";
String b="mac";
So, both == and .equals() returning true.
But, when you create String object by using new operator, String objects are created in the heap like other regular objects in java.
So == operator will return false, since both references referring two different object in the heap.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I need write a method that searches an object array of Club Members for a specific member and returns true if the member is found. This is what I have now.
public boolean isMember (String name){
boolean found = false;
int arrayIndex = 0;
while(arrayIndex < members.length && !found){
if(members[arrayIndex] == name){
found = true;
}
arrayIndex++;
}
return found;
}
In java, strings can only be compared with the .equals method, not with ==.
You can change your if condition like this:
if(members[arrayIndex].equals(name))
Try this
found = Arrays.asList(members).contains(name)