• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on String  RSS feed

 
Shaily Sharma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Question;
11. public String makinStrings() {
12. String s = ���Fred���;
13. s = s + ���47���;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6

When we invoke substring() method does it create new String object and make s to point to it? In that case 5 objects should be there.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I believe you got the wrong forum, you should be asking this in the programmer forum..

It's been a while, but I do remember that when you define a String without the "new" operator, similar String will share the same "memory object".

I remember reading the following article when doing the SCJP:
How Strings works

I'm *strongly* guessing that the substring will then not create a new object but simply point to the same memory space that was already created before. A quick look to the link above tells me it has something to do with "String literal pool".

Ask on the SCJP forum, you'll get a sure answer. May I kindly suggest you introduce your question by saying "Hi"
Regards,
Alex
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you got the wrong forum, you should be asking this in the programmer forum..


Agreed. Moving this to the Java in general, beginner forum.

Henry
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Belisle Turcot:
I'm *strongly* guessing that the substring will then not create a new object but simply point to the same memory space that was already created before.

substring does create a new String object, but it will share the internal character array. So the object itself is new, but its contents are shared.

That is, unless the substring will have the same start and end points as the the original string. If that's the case, the string itself is returned.

So:



For the SCJP exam you can assume that substring will always create a new object, as the API does not specify the object can be the same. In fact, nothing prevents future versions of Java to return a new object even if the start and end points are the same.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, I would argue that the API does specifically (and unnecessarily) state that substring() returns a new string. So in theory they're violating the API when they return the original string in the event that the substring range spans the entire length of the original string. In practice though, this really doesn't matter on any practical level, and won't be on the exam in any event.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!