create a reference
hello to all!
Well I've got this code:
public static String getName(String inString, int len){
String retStr = "";
try {
retStr = inString.substring(0, len);
int strlen = inString.length();
int l = strlen - len;
inString = inString.substring(len, l);
}catch (IndexOutOfBoundsException ex){
}
return retStr;
}
}
and I want the inString to be a reference.
How I declare e reference?
Please help
[510 byte] By [
impak] at [2007-9-26 2:44:32]

In Java all method parameters are passed by value, that is, your method is getting a copy of the original String, so changes in inString will not affect the string in the caller.
One way to solve it is encapsulating inString into another object (for example, an array), so you can change it.
Try this:
public static String getName(String[] inString, int len) {
String inString = param[0];
String retStr = "";
try {
retStr = inString.substring(0, len);
int strlen = inString.length();
int l = strlen - len;
inString = inString.substring(len, l); // CHECK THIS, IS IT WHAT YOU REALLY WANT?
param[0] = inString;
}catch (IndexOutOfBoundsException ex) {
}
return retStr;
}
I didn't mentioned, but of course you will have to call the new getName like this:
String[] sourceString = {"1234567890"};
String result = getName(sourceString, 3);
System.out.println( "result: " + result + " source: " + sourceString[0]);
You will get:
result: 123 source: 4567
Hope it helps
Consider the following code:
public static void main(String[] argv)
{
String s1 = "AAA";
method(s1);
System.out.print(s1);
}
public static void method(String s)
{
s = "BBB";
}
Are you saying that you want to do something whereby this code would print "BBB" to the screen (without the quotes)? If so, it can't be done - there is no way to pass s1 differently.
Here's what happens:
All method parameters in Java are passed by value, but that does not mean that the method gets a copy of your original String. Object variables hold references to the object in memory, so what gets passed (by value) to the method is a copy of that reference. If you manipulate the reference within the method body (by sending it messages such as calling methods or setting public variables), then you are in fact manipulating the original object. However, when you change the reference (e.g. s = "BBB"; above), you are changing only the local reference in the method (which was a copy of the original reference) to refer to a different object, and you haven't actually changed the original object at all - the original reference (s1 above) is still to the original object, and the local reference (s above) is to a new object.
Consider this code:
public static void main(String[] argv)
{
ArrayList list1 = new ArrayList();
method2(list1);
System.out.print(list1.size());
}
public static void method2(ArrayList list)
{
list.add(new Object());
}
public static void method3(ArrayList list)
{
list = new ArrayList();
list.add(new Object());
}
The above code will print "1" to the screen, since a reference to the original ArrayList is passed to method2, which thus manipulates the original ArrayList. If method3 were called instead of method2, then "0" would be printed to the screen, since the first line of method3 changes the local reference to a new object which is then manipulated rather than the original ArrayList declared in main.
Remember that Strings are immutable - you can't change the internal representation of the String - just change the variable to refer to a different String object. So, you can never alter the original String that is passed into the method.
If you really need to be able to do that, then you can use what Agustin suggested:
{
String s1 = "AAA";
String[] a1 = new String[]{s1}
method4(a1);
System.out.print(s1+","+a1[0]);
}
public static void method(String[] a)
{
a[0] = "BBB";
}
Running the above code would print "AAA,BBB" to the screen. When a1 is passed into method4, it is a reference to the array declared in main, and so changing one of its members changes the underlying array. When the method returns, a1[0] referes to the new String created in method4, but you must also be aware that the original String, s1, has not been touched - a1[0] was changed to refer to a different String, rather than the original String getting altered.
Hope this helps to clear it up.