• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many objects are created in String Pool and Object Heap in Line 10

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below Question is from Poddar's Mock Exam.



1 class stest
2 {
3public static void main(String[] args)
4{
5 String s1 = new String("amit");
6System.out.println(s1.replace('m','r'));
7System.out.println(s1);
8String s3="arit";
9String s4="arit";
10String s2 = s1.replace('m','r');
11 System.out.println(s2==s3);
12System.out.println(s3==s4);
13}
14 }
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Objects on heap : 3
Objects in pool : 2("amit","arit")
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rama,

on line 10 there is no new object created.
Because s1 does not contain an "m", the original object is returned.

Also see:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html -> replace
 
RAMA KRISHNA AALLA
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1 still holds "amit" after execution of below line as we are not assigning the result of s1.replace('m','r') to anything.

System.out.println(s1.replace('m','r'));

Thanks,
Rama Krishna Chowdary
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic