It's not a secret anymore!
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Objects - Count" Watch "String Objects - Count" New topic

String Objects - Count

Ravi Bhatt

Joined: Aug 08, 2007
Posts: 1

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";

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 About Java - Update every Week.
Guide SCJP - tips that you need know
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

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]
I agree. Here's the link:
subject: String Objects - Count
It's not a secret anymore!