GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Objects - Count Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects - Count" Watch "String Objects - Count" New topic
Author

String Objects - Count

Ravi Bhatt
Greenhorn

Joined: Aug 08, 2007
Posts: 1
All,

Can you please tell me how many string objects are created by below code?

1. String s= "Faltu";
2. s=s+"47";
3. s=s.substring(2,5);
4. s=s.toUpperCase();


As far as i know, it is 5. one point holding "Faltu", one holding "47" and rest 3 are created as a result of line 2,3,4.

But the answer is different in one of the mock tests that i took.

i tried this.

String s= "Faltu";
System.out.println(s.hashCode());
s=s+"47";
System.out.println(s.hashCode());
s=s.substring(2,5);
System.out.println(s.hashCode());
s=s.toUpperCase();
System.out.println(s.hashCode());

this code prints a different hashcode each time. confirming creation of new object.

Am i missing something? Please help. Thanks in advance.
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
yes, to each creation of a new object jvm to give one hashcode different, when the implementation is on the part of it, has not to be that you programmer defines as must function hashcode for object X.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Perhaps this thread may help you.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
GeeCON Prague 2014
 
subject: String Objects - Count