Background
I am working with functions which which passes arguments as pointers. I need to convert a function which is written in C to JAVA with the same behavior as in C. I am developing for Android in Eclipse under Windows.
C Functi开发者_开发百科on Example
int testFunction( char* firstName, char* SecomdName, char* lastname, int age);
This function takes 3 pointers to char values and initializes them, so in C I can write something like the following to initialize all three pointers:
char* v1, v2, v3;
int res = testFunction(v1, v2, v3, 54);
After the call to testFunction
, the variables v1, v2, v3
will be initialized with some values.
JAVA Function Example
int testFunction( String firstName, String secondName, String lastName, int age);
This is the same function as in C but written in JAVA, however in JAVA there are no pointers which means that I can't write something like this:
String v1, v2, v3;
testFunction(v1, v2, v3, 54);
Question:
What can I do to get the same initialization behavor for my JAVA function as in the C example? Is this possible?
There's a few things you could do. You could create a bean class that contains the fields you want to create, ie something like:
public class Names {
String firstName;
String secondName;
String lastName;
int returnValue;
// getters/constructors etc ommitted...
}
Your testFunction could then instantiate a Names object and return it, ie.
Names names = testFunction(int age);
String firstName = names.getFirstName();
// etc...
An alternative, if you're dealing with Strings is to pass through StringBuilder objects, ie
int testFunction(StringBuilder firstName, StringBuilder secondName, StringBuilder lastName, int age) {
// Use StringBuilder methods such as append to create the strings eg...
firstName.append("Bob");
}
Then elsewhere in your code you could use...
StringBuilder firstNameBuilder = new StringBuilder();
StringBuilder secondNameBuilder = new StringBuilder();
StringBuilder thirdNameBuilder = new StringBuilder();
testFunction(firstNameBuilder, secondNameBuilder, thirdNameBuilder, 84);
String firstName = firstNameBuilder.toString(); // firstName now equals "Bob"
/// etc...
You would have to do something like this:
public class Person {
public String firstName;
public String secondName;
public String lastName;
}
int testFunction(Person person, int age) {
...
// fill person fields
}
Person person = new Person();
int res = testFunction(person, 54);
You have to apply the "Holder" pattern. Roughly speaking (I cannot test the code right now, but you'll get the idea), you can do something like this:
class Holder <T>
{
public T element;
public T value() { return element; }
public void setValue(T t ) { element = t; }
}
and then change the code to accept Holder<String>
items. You have the disadvantage of having to call value()
(or get the inner element
), but it's the simpler and more straightforward way that comes to my mind (it is used also in the Java to IDL mapping of CORBA to hold "out" references).
Now you could do something like:
Holder<String> name = new Holder<String>();
int result = testFunction(name, ...);
System.out.println(name.value());
and the implementation of the testFunction
is straitforward:
int testFunction(Holder<String> name, ...)
{
name.setValue("whatever");
// ...
}
You could have your testFunction(...)
return an Object that contains the three (char primitives/String Objects).
public MyObject testFunction(v1,v1,v3,54) {
//do initialization stuff...
return new MyObject(v1, v2, v3, 54);
}
public class MyObject {
MyObject(char v1, char v2, char v3, int number) {
// something...
}
}
精彩评论