no pointer in Java => my problem

R

rowla

I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?
 
R

rowla

Change your design. Make the method f() return an int instead of trying
to change its parameter.

I forgot to add that I want to do it without returning anything...
 
J

Joona I Palaste

I forgot to add that I want to do it without returning anything...

Why? Does your religion forbid returning values? Do you have a childhood
trauma about returning values? Did your boss tell you that if you return
even one value, you're fired? Did someone dare you to see if you can do
it without returning a value?
 
R

rowla

1) in the real program I change 3 different objects of 3 different types in
my method and all I want to do is to return one of them and change in the
method the 2 others so that the changes are seen externally.
2) what is more, I want to learn how to do a thing like that in Java. In
C/C++ there's no problem with doing so, because you have pointers..
3) if my ignorance is making you so nervous that you write so stupid
questions, than just don't read my posts..

best wishes,
jacek
 
J

Joona I Palaste

rowla said:
1) in the real program I change 3 different objects of 3 different types in
my method and all I want to do is to return one of them and change in the
method the 2 others so that the changes are seen externally.
2) what is more, I want to learn how to do a thing like that in Java. In
C/C++ there's no problem with doing so, because you have pointers..

You'll have to wrap the references up in some object and pass that along
to your method. It's the only way. If you still don't like it, feel free
to write your own language.
3) if my ignorance is making you so nervous that you write so stupid
questions, than just don't read my posts..

No, I simply wanted a better justification for not returning a value
than "I don't want to do it".
 
B

Bryce (Work)

I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

Ok... Here's the problem.
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

In short, java passes objects by value.

A swap won't work because all you are doing is changing what the value
passed in is pointing to. In effect, they both are 2 different
pointers, pointing to the same object in memory (Java doesn't use
pointers, but they are references. I'm not knowledgable enough to know
the difference...). You are changing the reference that's passed into
the function, but not the original reference.

Since its just a copied value, when the procedure ends, its gone.

You can work on the object itself though. For example, the following
might work:

private void f(Z j) {
j.i = 65;
...
}

Now, when you return from your function, j.i will print 65.

also, I know you probably were obfuscating your code so that company
info was left out, etc... But on some newsreaders, i and j look very
similar and can be confusing. Best to use descriptive words like

int myInt;

Z myZ

etc... Makes it easier to read.
 
C

Christophe Vanfleteren

Bryce said:
In short, java passes objects by value.

Just a nitpick: Java passes references to objects by value, not the objects
themselves.
 
D

Dale King

rowla said:
1) in the real program I change 3 different objects of 3 different types in
my method and all I want to do is to return one of them and change in the
method the 2 others so that the changes are seen externally.

Then you have a very bad design. You need to fix the design, not try to
figure out how to get Java to support your bad design. Usually it is the
case that you just aren't thinking from an object-oriented perspective. The
fact that you used the word function is also a good clue.
2) what is more, I want to learn how to do a thing like that in Java. In
C/C++ there's no problem with doing so, because you have pointers..

And C/C++ has goto as well. There is no reason bad designs need to carry
over to new languages.
3) if my ignorance is making you so nervous that you write so stupid
questions, than just don't read my posts..

Joona asked a very good question. The question is "why?". The question you
asked was "how?" You have to look deeper to what you are trying to
accomplish and why, and not focus on how you've done it in other languages.

To help you fix your design we have to understand what abstraction you are
trying to represent. There are dozens of ways to implement it, but we don't
know which is the right one without understanding the problem you are trying
to represent.
 
R

rowla

Then you have a very bad design. You need to fix the design, not try to
figure out how to get Java to support your bad design. Usually it is the
case that you just aren't thinking from an object-oriented perspective. The
fact that you used the word function is also a good clue. [...]
To help you fix your design we have to understand what abstraction you are
trying to represent. There are dozens of ways to implement it, but we don't
know which is the right one without understanding the problem you are trying
to represent.

The thing was that after implementing some algorithm that was working OK I
had to rewrite it to make it consistent with some coding standards. when of
"the rules" is that a method shouls be no longer than approximately 20-25
lines, because otherwise it's hard to read.
One of the methods was especially hard to divide into smaller ones and
that's why I had problems with this pointer stuff..
But thanks to Bryce my roble has gone (with the wind;)
And C/C++ has goto as well. There is no reason bad designs need to carry
over to new languages.

I'm not saying that C++ is better than Java, but I found pointers very
useful (goto are not good of course:)
 
B

Bryce (Work)

Just a nitpick: Java passes references to objects by value, not the objects
themselves.

true. That's why I said "In short"... I generally think of objects as
references anyways, so I get confused when trying to explain.
 
C

Chris Smith

Dale King said:
To help you fix your design we have to understand what abstraction you are
trying to represent. There are dozens of ways to implement it, but we don't
know which is the right one without understanding the problem you are trying
to represent.

Dale's right, but http://jinx.swiki.net/66 might at least point in the
right direction for finding a solution.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

John C. Bollinger

Joona said:
You'll have to wrap the references up in some object and pass that along
to your method. It's the only way. If you still don't like it, feel free
to write your own language.

Or change class Z so that you can modify a Z instead of creating a new
instance. This is only possible if the code for Z is under your
control, of course.

Java simply does not have pass-by-reference, only pass-by-value. People
can get confused about that when they start passing reference types
around, probably more because of the clash of terminology than for any
other reason. When you pass a reference, you are passing that reference
_by_value_, so the dummy argument in the method (initially) contains a
copy of the value of the reference. Assigning a new value to the dummy
does not affect any other variable that contains the original value.

As I understand it, in fact, C and C++ also have only pass-by-value.
They just have a fairly facile way creating multiple levels of
indirection. If you want to emulate C in this [NOT RECOMMENDED] then
you can do this:

[...]

void fooPasser(Foo foo) {
Foo[] fooPtr = new Foo[] { foo };

passByReference(fooPtr);

// now fooPtr[0] != foo
}

void passByReference(Foo[] fooPtr) {
Foo bar = new Foo();

fooPtr[0] = bar;
}

[...]

The syntax is a bit messier than the C equivalent, but the semantics are
as similar as you can get in this case.


John Bollinger
(e-mail address removed)
 
A

andreia g.

rowla said:
I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

When you create your local object "s" and assign it to "j", you are
changing "j" only on a local scope. Seeing as "s" only exists inside the
function "f", "j" returns to it's original value when you return from
the function. Hence, you get 47 instead of 65.

"j" is a reference, so you can change the "f" function to assign 65 to
the variable "i" directly or through a method, instead of trying to
change all of "j".

The best way, imho, to go about it, is to have a method that takes an
int and sets the variable "i" to that, just like when you construct the
object. If you do that, you'll get the updated value when you return,
instead of the original value.

shana
developer and otherwise computer nut
 
?

=?ISO-8859-2?Q?Daniel_Sj=F6blom?=

rowla said:
I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

Your problem is not related to pointers.

void f(Z j) { j.i = 65; }

There are no value objects in java. Z is a reference passed by value.
References are similar to C pointers, except no pointer arithmetic (the
.. operator dereferences, similar to -> in C). To simulate C style:

void f(struct Object **o) { *o = malloc(sizeof(struct Object)); }

you wrap your Object in a wrapper in java:

class Wrapper
{
Object o;
}

void f(Wrapper w) { w.o = new Object(); }

Java and C are both pass by value only. You can't change the value of a
passed pointer in a C function either (that is, it won't affect the
pointer from the view of the caller).

Anyhow, your design is probably broken. Pass by reference semantics (as
in, behaves as if passed by reference) are rarely needed.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

John said:
As I understand it, in fact, C and C++ also have only pass-by-value.

C is pass by value only, while C++ also has pass by reference. You can
say something like

void f(type*& pointerPassedByReference);
 
T

Timo Kinnunen

I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

Here's an answer to the question. What you have to realize is that in C++
the compiler provides a default implementation for few member functions,
unless you tell it otherwise. One of those functions is the assignment
operator, .operator=(). You also have to realize that in C++, = in j=s is
syntactic sugar to execute the assignment operator.

The Java compiler neither provides such default methods nor is = syntactic
sugar for them, so if you want to have the same effect, you'll have to do
it manually. In your case, the code would look something like this:

class Z {
int i;
public Z(int y) {this.i=y;}
public Z assign(Z z) {this.i=z.i; return this;}
}

and

private void f(Z j) {
Z s = new Z(65);
j.assign(s);
}

As you see, the "assignment operator" is just a normal method, which should
you help you migrate the code towards a more Java-like design.
 
M

marcus

Quotes from various posts:
"I want to learn how to do a thing like that in Java"
"I want to do it without . . . "
"I want to declare a variable of type . . . "
"I want to make a declaration . . . "

What you need to do is learn to program, not hack. You seem to be
driven by ego, which is why you are getting responses about childhood
trauma. By the nature of your questions, your approaches to your
various problems are seriously flawed -- in any language.

Advise: abandon ego and open yourself up to actually learning this
stuff, rather than hacking at it until it more-or-less works. By
learning I mean take classes and read books, not post any more
ridiculing questions about how much easier it is to hack in C++.
Then you have a very bad design. You need to fix the design, not try to
figure out how to get Java to support your bad design. Usually it is the
case that you just aren't thinking from an object-oriented perspective.
The

fact that you used the word function is also a good clue.
[...]

To help you fix your design we have to understand what abstraction you
are

trying to represent. There are dozens of ways to implement it, but we
don't

know which is the right one without understanding the problem you are
trying

to represent.


The thing was that after implementing some algorithm that was working OK I
had to rewrite it to make it consistent with some coding standards. when of
"the rules" is that a method shouls be no longer than approximately 20-25
lines, because otherwise it's hard to read.
One of the methods was especially hard to divide into smaller ones and
that's why I had problems with this pointer stuff..
But thanks to Bryce my roble has gone (with the wind;)

And C/C++ has goto as well. There is no reason bad designs need to carry
over to new languages.


I'm not saying that C++ is better than Java, but I found pointers very
useful (goto are not good of course:)
 
T

Tim Jowers

rowla said:
I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

rowla,

good question... the trick is to understand that in Java
private void f(Z j) {
does not equate to a C method of
private void f(Z *j) {
but rather more like another level of indirection
private void f(Z **j) {
j=&(new Z(65));
// or something like *(&j)=new Z(65); in the original.

In short, this does not work in Java as a C progammer would expect.
You gotta think like "j is a local variable within f()". When it is
reassigned then only the local variable is reassigned. Initially it is
a refernce to the same variable (object) as the caller as when it is
called. Later it is reassigned.

Gotta do something like.

class holder
{ Z j;
}

private void f(holder h) {
Z s = new Z(65);
h.j=s;
}


Freaking bloated, eh? Or just return the variable on the stack as
mentioned. You can use ArrayList or an implicit array to effect the
work too. The implicit array is most like C IMO:

private void f(holder Z[]) {
Z s = new Z(65);
h[0]=s;
}

Z holder[] = new Z[1];
holder[0] = new Z(47);
f(holder);

Nifty ain't it ;-)
 
B

Bjorn Abelli

I have a class:
class Z {
int i;
public Z(int y) {this.i=y;}
}

and a function
private void f(Z j) {
Z s = new Z(65);
j=s;
}

when I do:
Z j = new Z(47);
f(j);
System.out.println(j.i);

I get 47 from System.out.println(j.i);
what should I do to get 65?

class Z
{
private int i;

public Z(int y) { i = y;}
public void setI(int y) { i = y;}
public int getI() {return i;}
}

Then you can do:

Z j = new Z(47);
j.setI(65);
System.out.println(j.getI());

Think about what OO really means, then
you might get another view of it... ;-)

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top