The following code snippet doesn't work. Why? They are objects of the same class, and they have same value of instance variable. What is the problem?
Pen bluePen = new Pen("Faber Castel");
Pen bluePen1 = new Pen("Faber Castel");
if( bluePen.equals(bluePen1) )
System.out.println("They are equal");
The output is empty. When I did like that:
Pen bluePen = new Pen();
Pen varPen = bluePen;
if( bluePen == varPen)
System.out.println(“They are identical”);
There is no problem. The output is "They are identical".
I was trying to understand the difference between == and equal() method. I read the differences of these two thing in this website. But I'm confused on account of the following code snippet not to work properly:
Pen bluePen = new Pen("Faber Castel");
Pen bluePen1 = new Pen("Faber Castel");
if( bluePen.equals(bluePen1) )
System.out.println("They are equal");
Could anyone explain to me why the code above don't output "They are equal"?