0
Please help me out with equals () function.
I cannot understand what the equals () function means. It would be nice if someone could answer my question.
8 Respostas
+ 2
Equals() is a method in my understanding. I recently discovered its use when I needed to ask the user to enter a string and see if it is "equal" to another string value. For example,
String x = "Yes";
If(x.equals("Yes"))
System.out.println("Good");
else
System.out.println("Not Good");
The condition in the if else will see if the string x is equal to "Yes". It is often used in conditions because it returns a boolean value true or false. I hope it helps. :)
+ 1
So this is all I got, First of all I would like to say sorry if I cant answer your question but what I got is this,
The Equals() function basically compares an object to another object. I also found out that it compares class states. For example
public static void Main (String,args[]){
Student x = new Student();
Student y = new Student();
x.Skin="Tanned";
x.Age=18;
y.Skin="Tanned";
y.Age=17;
System.out.println(x.equals(y));
System.out.println(x.equals(x));
System.out.println(x.Skin.equals(y.Skin));
System.out.println(x.Age.equals(y.Age));
}
class Student{
int Age;
String Skin;
}
Output goes like this:
false
true
true
false
First result implies that even though objeft x and y originated from same class Student, they are of different object.
Second result just compares object x to itself which is the same.
Third, the state of Student x which is Skin is the same as the Skin state of Student y which is string Tanned. They are the same object.
Fourth, since tge state Age of Student x is different to the state Age of Student y, the statement return flase.
So thats what I got. Im so sorry if its still incomplete but I hope I somehow helped you in understanding the equals method.
+ 1
Thanks, you did help me in understanding it. Hats off to you. :D
+ 1
equal method is used to compare two string by comparing their ASCII code
+ 1
I see, Dipesh Kumar.
0
But I wanted to know about its use in comparing object to and classes.
Could you answer that please?
0
Hm. Well,Im pretty new to Java. But I think I little bit of research and tinkering will do the trick. If you could wait for a while, I could find an answer for you. :)
0
No problem :)