+ 2
How charcters are stored in java?
Character class in java doesn't override equals method, so when I call equals on a character than a method defined in Object is called, which just compares their references. Than how is it that two characters like below equals to true when they would be having different reference values. Character a='a'; Character c='a'; In this case do a and c reference same location?
10 Respuestas
+ 3
At first: It is the sense of the equals() method to return true if two objects have the same value.
In your case: a.equals(c) returns true because 'a' == 'a'.
But a == c is false because they are two different objects.
In short:
equals() -> compares values
== -> compares references
+ 1
https://en.m.wikipedia.org/wiki/String_interning
"... Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6] ..."
+ 1
you are wrong. the equals function for the class Character is
public boolean equals(Object obj) {
if (obj instanceof Character) {
return value == ((Character)obj).charValue();
}
return false;
}
where value is a class member
private final char value;
+ 1
rajshekhar y dodamani
That's right. And this is the reason why you have to override the equals() method.
If you are interested you can have a look into this code:
https://code.sololearn.com/cK2H7CdVEq45/?ref=app
The Character class has overriden the equals() method.
public boolean equals(Object o){
return o instanceof Character && value == ((Character) o);
}
source: http://developer.classpath.org/doc/java/lang/Character-source.html
instanceof is a keyword to check the instance, in this case if Object o is from type Character.
And than it checks if both values are equal.
+ 1
Ciro Pellegrino
Which java version is your source code? I am just wondering because my source code looks different.
+ 1
Character.java in lib/src.zip by Oracle JDK 11 LTS but 15 has the same code.
0
JVM probably casts any of the Autoboxed classes (Integer, Character, Boolean...) into their native datatypes before comparing them.
I could be wrong, but that seems like the most logical thing to do.
0
Denise Roßberg but equals method in Object class looks something like this
boolean equals (Object o){
if(this==o)
return true;
return false;
}
Here it only compares thier references and not their values, so it should return false but it does not.
Is equals overridden in Character class, so that also compares values.
0
Character a='a', c='a'; //97
System.out.println( System.identityHashCode(a) );
System.out.println( System.identityHashCode(c) ); //same
System.out.println( a == c ); //true
a=128; c=128;
System.out.println( System.identityHashCode(a) );
System.out.println( System.identityHashCode(c) ); // different
System.out.println( a == c ); //false
0
No: if they had the same location, and one of the variables a or c would be deleted (e.g. because of local variables), the other would also deleted. This are 2 independence variables, whitch have randomly the same value