If I will write
String s=new String("XYZ");
String s1=new String("XYZ");
if(s.equals(s1)){
sop("True");
}
else{
sop("False");
}
This program will give me "True".
But When I am creating my own class suppose
class Employee{
public Employee(String name);
}
Employee e= new Employee("XYZ");
Employee e1 = neew Employee("XYZ");
if(e.equals(e1)){
sop("True");
}
else{
sop("False");
}
Then it will give the output as "False".
Can I know what is happening internally?

Answer Posted / sindhu

in the first case equals method compares the sequence of characters pointed by s and s1. as both are same it returns true. in the second case it is comparing the references i.e addresses of objects e and e1. as both are not same, it returns false.

Is This Answer Correct ?    11 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a method declaration?

510


Is integer immutable in java?

547


explain multi-threading in java?

548


How many types of exception can occur in a java program?

532


What are the 4 types of research methods?

509






What is an object class?

553


Is there is any difference between a scrollbar and a scrollpane?

558


Why collection doesn’t extend cloneable and serializable interfaces?

638


Is .net better than java?

552


Explain the difference between association, aggregation and inheritance relationships.

549


Why are data types important?

535


What is a classloader in java?

534


'A class is a template for an object' explain this statement.

544


how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? : Java thread

529


What is time complexity algorithm?

547