AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

no pointer in Java => my problem
Goto page 1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming
View previous topic :: View next topic  
Author Message
rowla
Guest





PostPosted: Thu Apr 15, 2004 5:41 pm    Post subject: no pointer in Java => my problem Reply with quote



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?

--
best regards, jacek
Back to top
Joona I Palaste
Guest





PostPosted: Thu Apr 15, 2004 5:43 pm    Post subject: Re: no pointer in Java => my problem Reply with quote



rowla <rowla.WYTNIJ.TO@i.to.o2.pl> scribbled the following:
Quote:
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?

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

--
/-- Joona Palaste (palaste (AT) cc (DOT) helsinki.fi) ------------- Finland --------
-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Life without ostriches is like coffee with milk."
- Mika P. Nieminen

Back to top
rowla
Guest





PostPosted: Thu Apr 15, 2004 5:52 pm    Post subject: Re: no pointer in Java => my problem Reply with quote



Quote:
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...

--
pozdrawiam,
jacek


Back to top
Joona I Palaste
Guest





PostPosted: Thu Apr 15, 2004 5:57 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

rowla <rowla.WYTNIJ.TO@i.to.o2.pl> scribbled the following:
Quote:
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...

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?

--
/-- Joona Palaste (palaste (AT) cc (DOT) helsinki.fi) ------------- Finland --------
-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"That's no raisin - it's an ALIEN!"
- Tourist in MTV's Oddities

Back to top
rowla
Guest





PostPosted: Thu Apr 15, 2004 6:15 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

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

Back to top
Joona I Palaste
Guest





PostPosted: Thu Apr 15, 2004 6:21 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

rowla <rowla.WYTNIJ.TO@i.to.o2.pl> scribbled the following:
Quote:
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.

Quote:
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".

--
/-- Joona Palaste (palaste (AT) cc (DOT) helsinki.fi) ------------- Finland --------
-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush

Back to top
Bryce (Work)
Guest





PostPosted: Thu Apr 15, 2004 7:10 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

On Thu, 15 Apr 2004 19:41:50 +0200, "rowla"
<rowla.WYTNIJ.TO@I.TO.o2.pl> wrote:

Quote:
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.

--
now with more cowbell

Back to top
Christophe Vanfleteren
Guest





PostPosted: Thu Apr 15, 2004 7:16 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

Bryce (Work) wrote:

Quote:
In short, java passes objects by value.

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

--
Kind regards,
Christophe Vanfleteren

Back to top
Dale King
Guest





PostPosted: Thu Apr 15, 2004 7:49 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

"rowla" <rowla.WYTNIJ.TO@I.TO.o2.pl> wrote

Quote:
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.

Quote:
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.

Quote:
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.


--
Dale King
Blog: http://nobleware.homedns.org/Blog



Back to top
rowla
Guest





PostPosted: Thu Apr 15, 2004 8:19 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

Quote:
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;)

Quote:
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:)

--
pozdrawiam,
jacek


Back to top
Bryce (Work)
Guest





PostPosted: Thu Apr 15, 2004 8:19 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

On Thu, 15 Apr 2004 19:16:34 GMT, Christophe Vanfleteren
<c.v4nfl3t3r3n (AT) pandora (DOT) be> wrote:

Quote:
Bryce (Work) wrote:

In short, java passes objects by value.

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.

--
now with more cowbell

Back to top
Chris Smith
Guest





PostPosted: Thu Apr 15, 2004 8:26 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

"Dale King" <kingd[at]tmicha[dot]net> wrote:
Quote:
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

Back to top
John C. Bollinger
Guest





PostPosted: Thu Apr 15, 2004 8:31 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

Joona I Palaste wrote:

Quote:
rowla <rowla.WYTNIJ.TO@i.to.o2.pl> scribbled the following:

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.

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
[email]jobollin (AT) indiana (DOT) edu[/email]


Back to top
andreia g.
Guest





PostPosted: Thu Apr 15, 2004 8:36 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

rowla wrote:
Quote:
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

Back to top
Timo Kinnunen
Guest





PostPosted: Thu Apr 15, 2004 8:53 pm    Post subject: Re: no pointer in Java => my problem Reply with quote

On Thu, 15 Apr 2004 19:41:50 +0200, rowla wrote:

Quote:
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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Language Programming All times are GMT
Goto page 1, 2, 3, 4, 5  Next
Page 1 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.