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 excellent == vs equals question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "excellent == vs equals question" Watch "excellent == vs equals question" New topic
Author

excellent == vs equals question

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
Can someone please explain by this would only print , Equal B in the output ???
---------------------------------------------------
String s = new String( "Computer" );

if( s == "Computer" )
System.out.println( "Equal A" );
if( s.equals( "Computer" ) )
System.out.println( "Equal B" );
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 129
sarimraza,
if(s=="computer")
will create a new string literal computer which does not have
the same refernce value of s obtained from the statement
String s = new String("computer");
You would know that == checks the object reference value and
not the objects literal value. By reference i mean the address
to which it points to.
s == new String("computer")
if(s == "computer")
will create two different strings "computer" at different pleaces
meaning at different memory addresses.
So the address to which the object s is pointing to is not the
same address at which the new string object "computer" is created
in the line (s == "computer") which results in the comparision showing false.
Hope this helps


------------------
Regards,
V. Kishan Kumar


Regards,<BR>V. Kishan Kumar
 
 
subject: excellent == vs equals question
 
Threads others viewed
equal
String
String
String
Wats the O/P...
developer file tools