Memory usage for String as formal argument

We know that String is an immutable object, So when we pass any string value to a method call then, specifically, string is passed by value.

For example consider following example

**********************************************************example code**********************************************************

Class myClass

{

String lsName = 揌ello?

verifyVariable(lsName);

//Do some processing

厖.

method2(lsName);

//Do some processing

厖.

method3(lsName);

//Do some processing

厖.

private void verifyVariable(String fsParam)

{

System.out.println(fsParam);

//Do some processing

厖.

}//End Of verifyVariable

private void method2(String fsParam2)

{

//Do some processing

厖.

}//End Of

private void method3(String fsParam3)

{

//Do some processing

厖.

}//End Of

}//End Of Class

**********************************************************End Of example code**********************************************************

Query

1.In the above example decalring a final constant for String 搇sName?will help in utilization of memory Or Not? This constant is then passed to different method call. All the formal String argument will refer to one String Object.

The reason to ask this question is because as per my understanding of Java In above example , 4 different String objects are created

One for LsName, One for fsParam, One for fsParam2 and One for fsParam3

Is this understanding correct? As per my friend only two instance of String object is created, One for lsName and One for all other methods

2. Is there any way which we can measure the actual memory utilization by writing some java program for above example?

I really appreciate if you can answer my qbove queries.

[1950 byte] By [MayurGoradiaa] at [2007-11-27 2:25:17]
# 1
> as per my understanding of Java In above example , 4 different String objects are createdNo, only a single String is created. A copy of the reference to the String is created and passed to each method.
camickra at 2007-7-12 2:33:24 > top of Java-index,Java Essentials,Java Programming...
# 2

Okay in that case do you think that JVM will create as many object as the number of call to the method? Because once the execution is return object which was created was submitted for garbage collection.

Let me explain the above statement as below (I will use the same example which I had given initially)

JVM make a call to method verifyVariable(lsName), it creates one copy of "lsName" and pass that memory address to the method. When execution is return then object copy is then submitted to garbage collection.

when JVM calls method method2(lsName), it will again create new copy for lsName and pass it to method2.

So don't you think in above fashion each time its creating a copy of object for String parameter.

MayurGoradiaa at 2007-7-12 2:33:24 > top of Java-index,Java Essentials,Java Programming...
# 3

> So don't you think in above fashion each time its creating a copy of object for String

> parameter

No. There are no copies of any strings being made when a method is invoked. Such a method is passed a reference to a String. The objects referenced by method arguments do not become eligible for garbage collection when the method returns: rather - like other objects - they become eligible when there are no references to them. This includes any references that the caller may have.

pbrockway2a at 2007-7-12 2:33:24 > top of Java-index,Java Essentials,Java Programming...