472,122 Members | 1,467 Online

# Using pointers in Java

I need to pass a 'reference' to a double value to a function which
changes that value. Each time the function is called, it needs to
change different sets of values. In C I'd simply pass references.

void add(double *a1, double *a2, double *a3){
*a1 = *a2 + *a3;
*a2 = *a1 + *a3;
}

In Java, one solution would be to create a class to represent a
mutable value:

public class MutableDouble {
private double value;
public final void set(double value){ this.value=value; }
public final double get(){ return value; }
}
But, my this is cumbersome in my code for several reasons:

1) My code contains many long mathematical equations, which become
very convoluted using this syntax, with ridiculous numbers of
parentheses! It is very important that the maths in the code is
readable, as someone else may have to work on my code. E.g.,
a = a + 1
becomes
a.set(a.get()+1)
The problem is, of course, that unlike C++, operators cannot be
defined.

2) get() and set() will presumably be slower than direct use of
values. Though I don't know enough about how the javac inlines
functions.

3) I need to define a MutableInteger, MutableLong and MutableFloat as
well.

4) The size of the compiled code will be, presumably, more bulky.

So, This is the kind of solution I am thinking of adopting:

void test(){
double[] x=new double[1], y=new double[1];
}

void add(double[] a1, double[] a2, double[] a3){
a1[0] = a2[0] + a3[0];
a2[0] = a1[0] + a3[0];
}
Using arrays of length 1 as pointers has some advantages, seems
'cheap' in terms of code and memory, and the code will look much nicer
than using get/set. But naturally there are disadvantages, e.g., as
with pointers, there's the potential for inadvertent null-pointer
exceptions (which will be ArrayIndexOutOfBounds exceptions).

Has anyone used this method?

Has anyone any comments on its effectiveness and/or problems?

What is your opinion on its methodological purity?

Sanjay
Jul 17 '05 #1
6 95735
In java any reference to a simple type is always by value and any reference
to an object is always a pointer except in cases of special objects
(String).

So if you want to change the value of a double inside a function you wrote
that double needs to be wrapped inside an object on which you can call
methods like get and set or any other methods you want to use in order to
manipulate the double.

The other hacky solution might be using static variables but that is like
using globals.

Roman.

"S Manohar" <sg*******@hotmail.com> wrote in message
I need to pass a 'reference' to a double value to a function which
changes that value. Each time the function is called, it needs to
change different sets of values. In C I'd simply pass references.

void add(double *a1, double *a2, double *a3){
*a1 = *a2 + *a3;
*a2 = *a1 + *a3;
}

In Java, one solution would be to create a class to represent a
mutable value:

public class MutableDouble {
private double value;
public final void set(double value){ this.value=value; }
public final double get(){ return value; }
}
But, my this is cumbersome in my code for several reasons:

1) My code contains many long mathematical equations, which become
very convoluted using this syntax, with ridiculous numbers of
parentheses! It is very important that the maths in the code is
readable, as someone else may have to work on my code. E.g.,
a = a + 1
becomes
a.set(a.get()+1)
The problem is, of course, that unlike C++, operators cannot be
defined.

2) get() and set() will presumably be slower than direct use of
values. Though I don't know enough about how the javac inlines
functions.

3) I need to define a MutableInteger, MutableLong and MutableFloat as
well.

4) The size of the compiled code will be, presumably, more bulky.

So, This is the kind of solution I am thinking of adopting:

void test(){
double[] x=new double[1], y=new double[1];
}

void add(double[] a1, double[] a2, double[] a3){
a1[0] = a2[0] + a3[0];
a2[0] = a1[0] + a3[0];
}
Using arrays of length 1 as pointers has some advantages, seems
'cheap' in terms of code and memory, and the code will look much nicer
than using get/set. But naturally there are disadvantages, e.g., as
with pointers, there's the potential for inadvertent null-pointer
exceptions (which will be ArrayIndexOutOfBounds exceptions).

Has anyone used this method?

Has anyone any comments on its effectiveness and/or problems?

What is your opinion on its methodological purity?

Sanjay

Jul 17 '05 #2
hi sanjay
use wrapper classes
your code should be something like

double a,b,c;
Double a1,b1,c1;
a1=new Double(a);
b1=new Double(b);
c1=new Double(c);
......
callMethod(a1,b1,c1);
a=a1.doubleValue();
b=b1.doubleValue();
c=c1.doubleValue();
.....

have a look at java.lang.Double class for documentation

regards
amey
Jul 17 '05 #3
Funny, I was just saying to someone yesterday: "Java is not a replacement
for Fortran". For the project in question we took it as understood that the
number-crunching would be performed in C.

One alternative to your "arrays of one element" proposal would be to put
all three parameters into a single array:

a[0] = a[1] + a[2];
a[1] = a[0] + a[2];
}

In both cases you'll have array bounds checking overhead, which I don't
think the JIT compiler will be able to optimise away (could be wrong there).

Or alternatively:

class Triple {
double a1, a2, a3;
}

t.a1 = t.a2 + t.a3;
t.a2 = t.a1 + t.a3;
}

That does at least look object-oriented, and should generate reasonably
efficient bytecode. Whether it really _is_ object-oriented depends on
whether t corresponds to anything recognisable in your problem domain ...

HTH

Chris

S Manohar wrote:
I need to pass a 'reference' to a double value to a function which
changes that value. Each time the function is called, it needs to
change different sets of values. In C I'd simply pass references.

void add(double *a1, double *a2, double *a3){
*a1 = *a2 + *a3;
*a2 = *a1 + *a3;
}

In Java, one solution would be to create a class to represent a
mutable value:

public class MutableDouble {
private double value;
public final void set(double value){ this.value=value; }
public final double get(){ return value; }
}
But, my this is cumbersome in my code for several reasons:

1) My code contains many long mathematical equations, which become
very convoluted using this syntax, with ridiculous numbers of
parentheses! It is very important that the maths in the code is
readable, as someone else may have to work on my code. E.g.,
a = a + 1
becomes
a.set(a.get()+1)
The problem is, of course, that unlike C++, operators cannot be
defined.

2) get() and set() will presumably be slower than direct use of
values. Though I don't know enough about how the javac inlines
functions.

3) I need to define a MutableInteger, MutableLong and MutableFloat as
well.

4) The size of the compiled code will be, presumably, more bulky.

So, This is the kind of solution I am thinking of adopting:

void test(){
double[] x=new double[1], y=new double[1];
}

void add(double[] a1, double[] a2, double[] a3){
a1[0] = a2[0] + a3[0];
a2[0] = a1[0] + a3[0];
}
Using arrays of length 1 as pointers has some advantages, seems
'cheap' in terms of code and memory, and the code will look much nicer
than using get/set. But naturally there are disadvantages, e.g., as
with pointers, there's the potential for inadvertent null-pointer
exceptions (which will be ArrayIndexOutOfBounds exceptions).

Has anyone used this method?

Has anyone any comments on its effectiveness and/or problems?

What is your opinion on its methodological purity?

Sanjay

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #4
Sanjay,

Although this would be frowned upon by purists, you could make your
wrappers have public instance variables allowing you to reference them
directly without getters/setters:

public class MDouble
{
public double d;
}

Ray

Jul 17 '05 #5
While it was 31/1/04 3:27 am throughout the UK, Amey Samant sprinkled
little black dots on a white screen, and they fell thus:

<snip>
have a look at java.lang.Double class for documentation

So, in what version of Java is Double mutable?

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #6
While it was 31/1/04 11:13 pm throughout the UK, Raymond DeCampo
sprinkled little black dots on a white screen, and they fell thus:
Sanjay,

Although this would be frowned upon by purists, you could make your
wrappers have public instance variables allowing you to reference them
directly without getters/setters:

You'd need to be an extreme purist to eschew structs.

That's before you get to those who say getter/setter methods just
unnecessarily bloat the code. Some (just ask Walter Bright) believe
that properties might as well just look like field names, even if they
don't map directly to real fields and/or involve side effects.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.