Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Objects

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respected Sirs,

public String makeStrings()
{
String s="Fred";
s=s+"47";
s=s.substring(2,5);
s=s.toUpperCase();
return s.toString();
}
/*How many string objects will be created when this method is invoked
a)1
b)2
c)3
d)4
e)5
f)6

Thnks

Francis
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Total Five String referential objects?
 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the question providers say the answer is 3:

I think there is 6:

1) "Fred"
2) "47"
3)"Fred47"
4)"ed4"
5)"ED4"
6)"ED4" , while calling toString()

can anyone help with explabation
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching the forums for "Fred 47". This exact same question has been asked and answered here many times.

For example:
How many string object created in this code
String Test
Strings
Q from javaBeat on String
Strings Doubt
Strings
doubt with string abjects
How many object create?
...
[ July 01, 2008: Message edited by: Jesper Young ]
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read questiobnm carefully
It ask's for number of strings objects created when method is invoked i.e. at runtime.
I think there are three string objects being created when program runs. Others are created at compile time.

Regards
Sunny
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher ,

Total 3 String objects will be created .
"Fred" and "47" are literals hence will be created before invocation of this method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic