• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

doubt with string abjects

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can some one explain the below code where the number of string objects created is 3.I feel the objects created should be more than 3.Please clear my doubt.

Thanks










[ February 25, 2008: Message edited by: Moieen Khatri ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At first, I couldn't compile the code until I changed the double-quotes from curly-quotes to standard ASCII straight-double-quotes.

Now, it compiles and I get an output of:

ED4
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Keeping in mind that String objects are immutable, total String objects created here - 5 ( fred, 47, fred47,ed4,ED4)
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 6 String objects created here. You have to remember that s.toString() also creates a String object.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Moieen Khatri,
The number of objects created here is 5.To find the number of unique objects created you can use the following in your code.

class test
{

public static void main(String args[])
{

System.out.println(makinStrings());
}

public static String makinStrings() {
String s = "Fred";
System.out.println(s+" "+s.hashCode());
s = s + "47";
System.out.println(s+" "+s.hashCode());
s = s.substring(2, 5);
System.out.println(s+" "+s.hashCode());
s = s.toUpperCase();
System.out.println(s+" "+s.hashCode());
s=s.toString();
System.out.println(s+" "+s.hashCode());
return s;
}

}

I have just a call to the hashCode() method every time I make a change to the String Object s.When I run the above code , I get the following output.

bash-3.00# java test
Fred 2198155
Fred47 2112428622
ed4 100213
ED4 68469
ED4 68469
ED4

This shows there are 5 unique String Objects created since the hashCode() method returns different values each time it is called.

Hope this Helps.

regards,
Raja
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are missing the string "47" and thats why your count is less by 1 !!!
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Vaibhav,
I missed out that 47.Thanks for pointing it out.

Raja
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all for making the solution so simple by using the hashcode method
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ian,
I the toString() method does not seem to create a new String object.The hashCode() method returns the same value for toUpperCase() and toString() method.
So the total number of Objects created should be five.

regards,
Raja
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Raja,Agree with you ....total number of object created should be 5.toString() is returning the same reference of that object rather than creating new object.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well There are two ways to look into this question:

1) How many objects are created when this method is compiled and invoked ?

Answer : 5 (Already explained)

2) How many Object are created when this method is invoked..that means run time?

"Fred" and "47" Objects are created at compile time, So only 3 Objects will be created at run time and 2 Objects are created at compile time( in String Constant pool).
 
reply
    Bookmark Topic Watch Topic
  • New Topic