0

Can we able to access the object in String pool which is does not not have any reference. Here is code :

String str ="abc";
str.toUpperCase();
System.out.println(str); // System.out.println(str.toUpperCase());

Output : abc

Here I am performing the toUpperCase() operation on str. In String pool one object will be created for this str.toUpperCase();. Can we able to access this object.? if yes how?

3
  • 1
    What language? In Java you have to use the return value str=str.toUpperCase(); This has nothing to do with string pools. Commented Oct 24, 2013 at 14:45
  • Hi holger, Thank a lot.. Even this line 'System.out.println(str.toUpperCase());' code will give the expected result. But my question is what happened to this line in memory 'str.toUpperCase();//it contains result "ABC"'... can we able to access this result without assigning to be any variable and without using system.out.println() statements. Commented Oct 25, 2013 at 6:05
  • If you don’t use the return value, that object becomes unreachable. The garbage collector will free its memory in the next gc cycle then. Commented Oct 25, 2013 at 7:23

2 Answers 2

2

This is where java documentation is used .... Refer http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#toUpperCase()

str.toUpperCase() returns a string converted to upper case, you have to assign it to another string variable or do a self assignment, so you can access it later.

Sign up to request clarification or add additional context in comments.

4 Comments

@Basilevs the question has nothing to do with interning.
@Holger, what other string pool do you think its about? Heap?
@Basilevs question is about 'accessing the object' which has the 'upper case string'. And that link has the information he would need.
@Basilevs the questioner simply doesn’t know how to use the return value of toUppercase. Otherwise his question doesn’t make any sense. Feel free to provide a better answer if you disagree. I don’t see your answer yet.
2
String str ="abc";       // 1
str.toUpperCase();       // 2
System.out.println(str); // 3

in above code

First line will create new Object of String with value "abc" and assign it to the reference variable str.

Second line will create the new Object of String because String class is immutable so original Object str will not change. but here we are not assigning the new object which is created in line 2 so it will be lost somewhere in Heap area.

That is why in line 3 printing value is "abc".

if you want to use the new Object created by str.toUpperCase() than have to assign that in a new reference variable. or alternative option is update the original String Object like this

str = str.toUpperCase();

but in above operation, the original Object str containing the value "abc" will be lost in the Heap area !!!!!

You can find details about working with String by example here.

2 Comments

@ Krishn: thank you so much .. you mean we cant access the result of 'str.toUpperCase()' without assign to same/other String variable.
yes exactly...we have to assign it to any reference variable in order to use that...else it will be an lost object somewhere in Heap area...!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.