473,657 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=valu e; }
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];
add(x,y,y);
}

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 ArrayIndexOutOf Bounds 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 95879
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*******@hotm ail.com> wrote in message
news:2e******** *************** ***@posting.goo gle.com...
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=valu e; }
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];
add(x,y,y);
}

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 ArrayIndexOutOf Bounds 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,b 1,c1);
a=a1.doubleValu e();
b=b1.doubleValu e();
c=c1.doubleValu e();
.....

have a look at java.lang.Doubl e 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:

void add(double[] a) {
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;
}

void add(Triple t) {
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=valu e; }
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];
add(x,y,y);
}

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 ArrayIndexOutOf Bounds 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.eu net.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.Doubl e 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6550
by: Noah Spitzer-Williams | last post by:
Hello guys, I'm itinerating through my array using pointers in this fashion: image is unsigned char image do { cout << "image byte is: " << *image << endl;
138
5214
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
4
13782
by: mVosa | last post by:
I have this piece of code in a funtion which is trying to retrieve the memory address of values that are stored in a 2D array, using pointers...Could I get help or tips on how to do this please :)...I'm new at this programming.. void displayADD(float temp) { int m,d; float *ptr; ptr=&temp; *ptr=temp;}
6
2934
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
1
2452
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
2
11490
by: vidyavathi | last post by:
hi all, can u guys help me out ......... how do i find if a gn string is palindrome or not using pointers in C?? regards vidya
10
4020
by: My Paradise | last post by:
I have got a problem using Arrays,Functions and Pointers together.Please tell me hoe to pass a 2 D Array to a C function using Pointers.When I compile such a Program,I get an error saying "Function needs a prototype".If I give the protype ,then also it gives an error like,"Cannot convert int into int* "or something like that.If needed I can post the particular program with which i had the problem.Please help me out !
69
3304
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
2
6968
by: gchidam | last post by:
please help me out in matrix multiplication using pointers in C++ and give me brief description about usage of pointers
4
2339
by: touches | last post by:
I am using an 2d array to search for the key 32 using pointers.. Now whenever i try to compile i get an linker error saying undefined symbol find_key(int *far,int,int).. I know the error is with the use of pointers and the way i am passing them... Can anyone help me out #include<stdio.h> #include<conio.h> #include<stdlib.h> #define N 4 void find_key(int *,int,int); void main(){
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.