File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object & String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object & String" Watch "Object & String" New topic
Author

Object & String

Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
program is

class MyClass4
{
public static void main(String args[])
{

Object obj1 = "SCJP";

Object obj2 = new String("SCJP");

System.out.println(obj1.equals(obj2));
}
}

output for the above program is "true".

can any one explain me.
Ganesha Kumar
Ranch Hand

Joined: May 04, 2006
Posts: 56
This is because, the method called is the equal() method in the String class.
This results in equality of obj1 and obj2 as both contain the same string "SCJP".
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
equals() method always checks for "meaningfully equivalent" objects
since obj1 and obj2 both contain the same string "SCJP" these objects are meaningfully equivalent.

Hence the answer is true.
If at all you do obj1 == obj2 then you will get the answer as false.
This is because == operator checks the object references.
Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
I think Rami Reddy question basically is "why is String class equals method is called ?"

Or

How does the JVM know that String class equals needs to be called rather then Object class equals (which plainly checks for equality of reference)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
This is basic overriding principle in Java
where Base class is Object and Child is String.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
A fast question,

When we say:


"some value" is placed in the String constant pool, but what about when we say:


Is "some value" placed in the pool too ?

Thanks in advance ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
yes. We are just storing String reference in Object reference. thats it.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Thanks alot ...

Good luck ...
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
I got it. thansk all..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object & String
 
Similar Threads
Overriding
Circular references and garbage collection
Casting , why so? Any help?...More....
equals() and ==
Basic thread doubt