Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Steven Wood   on Jan 11 In Java Category.

  
Question Answered By: Rhys Evans   on Jan 11

java always pass  reference (pointer) of objects to functions
when you pass string  to function, the function  receives pointer of the
string
but when you assign new value in function body to function argument, it only
affect the local pointer, not the original  object

consider this in C:
void f1(char * s) {
s = "1234"; // you don't modify the original "hello" string

// but
strcpy(s, "1234"); // you modify the original "hello" string
}
void main() {
char * s = "hello";
f1(s);
}

in java:
target = "showForm";
is equal to : target  = new String("showForm"); // you assign new object
reference, not modifying original object

you can solve your problem  with:

public String _function( String target ) {
return "showForm";
}

//or if you need more than one outputs
public Object[] _function( String target ) {
return new Object[] { "showForm", "str2", "str3" };
}

public void  _function( StringBuffer target ) {
// cannot use String as JVM treat String like basic type
target.setLength(0);
target.append("showForm");
}

Share: 

 

This Question has 4 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Pass By Reference Or get search suggestion and latest updates.


Tagged: