some value , some reference

P

Peter

Hi
When i pass a int variable into a function, if i changed the value
within that function, the int value won't change. For example:

int yyy=1234;
ddd(yyy);
System.out.println("yyy="+yyy);

private void ddd(int t){
t=3455;
}

The above code still print 1234.

But if i pass a DefaultMutableTreeNode variable into a function,
if i changed the value within the function, the variable changed too.,
for example:
 
C

Chris Smith

Peter said:
Hi
When i pass a int variable into a function, if i changed the value
within that function, the int value won't change. For example:

int yyy=1234;
ddd(yyy);
System.out.println("yyy="+yyy);

private void ddd(int t){
t=3455;
}

The above code still print 1234.

Yep. Java always passes method parameters by value.
But if i pass a DefaultMutableTreeNode variable into a function,
if i changed the value within the function, the variable changed too.,
for example:

That's not true. Since you didn't provide your "for example" code, I'm
going to make an assumption that you changed the state of the object
that the parameter pointed to instead of changing the parameter. If you
had really written code analogous to the above code, which changes the
parameter itself, it would look something like:

public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

private void ddd(DefaultMutableTreeNode t)
{
t = new DefaultMutableTreeNode(new Integer(3455));
}

And you'd get the same result.

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

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

Peter

Hi
Thanks for your reply first.

this will print "3455":

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}


BUT this will print 1234:

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t =
new DefaultMutableTreeNode(new Integer(3455));
}
}

How to explain?
thanks from Peter ([email protected])
 
S

Sudsy

Peter said:
Hi
Thanks for your reply first.

this will print "3455":

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}


BUT this will print 1234:

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t =
new DefaultMutableTreeNode(new Integer(3455));
}
}

How to explain?
thanks from Peter ([email protected])

Jeez, Peter...this is all so basic! Why are you asking Java
programmers about language fundamentals? The short answer
is that the variable t is an object reference pushed on the
stack. You assign a new value to the variable, losing the
reference to the original object. The variable gets popped
off the stack when the method returns.
Followup (if really necessary) to comp.lang.java.help
 
J

Joona I Palaste

Peter said:
BUT this will print 1234:
import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}
static private void ddd(DefaultMutableTreeNode t)
{
t =
new DefaultMutableTreeNode(new Integer(3455));
}
}
How to explain?
thanks from Peter ([email protected])

For the same reason as this will print "1234":
public class tt {
public static void main(String[] args)
{
int i = 1234;
ddd(i);
System.out.println("yyy=" + i);
}
static private void ddd(int t)
{
t = 3455;
}
}

Even though the values of reference types are references to objects,
they are still *VALUES*, and Java passes everything by value.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Shh! The maestro is decomposing!"
- Gary Larson
 
P

Peter

The following two examples pass an object into a function, please
explain why the first example "WILL" lose the reference and second
"DONT"?

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
String str="1234";
ddd(str);
System.out.println("yyy=" + str);
}

static private void ddd(String t)
{
t ="3455";
}
}


import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}

thanks
from Peter ([email protected])
 
P

Peter

if passed by value then why this exmaple will print 3455?
import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}

thanks
from Peter ([email protected])
 
B

Bjorn Abelli

if passed by value then why this exmaple will print 3455?

Because you have sent the value of the reference, not the value of the
"userObject" it contains. It's still the same object you work on within the
the method, on which you have changed the property "userObject".

In your other example, you had instead changed what *object* the reference
within the method was refering to.

// Bjorn A
import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}

thanks
from Peter ([email protected])
 
S

Sudsy

Peter said:
if passed by value then why this exmaple will print 3455?

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}


thanks
from Peter ([email protected])

I give up! You're an idiot, Peter! Please don't attempt
to pursue a career in programming. You'll only give the
rest of us a bad name.
Flames invited from both courts.
 
C

Chris Smith

Hi Peter,
this will print "3455":

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}
}


BUT this will print 1234:

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t =
new DefaultMutableTreeNode(new Integer(3455));
}
}

How to explain?

The difference is that in the first case, you're modifying the state of
an object. There are two references to that object: one called 'yyy'
from main, and one called 't' from ddd. It doesn't matter which
reference you use... if you put a value into that object, and then ask
for it back, it all works fine.

In the second example, you aren't modifying any object, but rather
changing the value of 't' in ddd. Since the 't' variable is local to
the ddd method, that doesn't affect anything outside of the ddd method.

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

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

Michael Borgwardt

Sudsy said:
I give up! You're an idiot, Peter!

This is very much uncalled for without knowing at which stage
in his learning process he is.

It looks to me like he's only just being introduced to the
basic concepts, and chanced upon one of the more advanced
problems.

Can you honestly claim that when you first learned programming,
things like this were immediately understandable to you?

Please don't attempt
to pursue a career in programming. You'll only give the
rest of us a bad name.

You, in turn, should refrain from ever attempting to do any
teaching.
 
M

Michael Borgwardt

Peter said:
The following two examples pass an object into a function, please
explain why the first example "WILL" lose the reference and second
"DONT"?

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
String str="1234";
ddd(str);
System.out.println("yyy=" + str);
}

static private void ddd(String t)
{
t ="3455";
}
}


This method gets as parameter a copy (named t) of the reference to a String
object (with the content "1234"). It changes that reference to point to a
*different* String object (newly created with the content "3455"),
but neither the original object nor the original reference to it (named str)
are changed. Since both the new reference and the new object exit only within
the method and are lost after completing it, the method effectively does
nothing.
import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}

Here the method is given a copy (named t) of the reference to an object
of class DefaultMutableTreeNode which internally contains a reference
(the name of which we don't know) to an object of class Integer that
has the value 1234.

The method then calls a method of the tree node object which causes it
to change its internal reference to instead point to a newly-created
String object with the value "3455".

When the method ends, the reference t is lost, but the original reference
yyy still points to the same tree node object. Then its getUserObject
method is called and returns the internal reference, which now points at
the new String object. This String's contents are then displayed.

The Integer object, which is not anymore references anywhere, is lost.
 
P

Peter

Michael Borgwardt said:
Peter said:
The following two examples pass an object into a function, please
explain why the first example "WILL" lose the reference and second
"DONT"?

import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
String str="1234";
ddd(str);
System.out.println("yyy=" + str);
}

static private void ddd(String t)
{
t ="3455";
}
}


This method gets as parameter a copy (named t) of the reference to a String
object (with the content "1234"). It changes that reference to point to a
*different* String object (newly created with the content "3455"),
but neither the original object nor the original reference to it (named str)
are changed. Since both the new reference and the new object exit only within
the method and are lost after completing it, the method effectively does
nothing.
import javax.swing.tree.DefaultMutableTreeNode;
public class tt{
public static void main(String[] args)
{
DefaultMutableTreeNode yyy =
new DefaultMutableTreeNode(new Integer(1234));
ddd(yyy);
System.out.println("yyy=" + yyy.getUserObject());
}

static private void ddd(DefaultMutableTreeNode t)
{
t.setUserObject("3455");
}

Here the method is given a copy (named t) of the reference to an object
of class DefaultMutableTreeNode which internally contains a reference
(the name of which we don't know) to an object of class Integer that
has the value 1234.

The method then calls a method of the tree node object which causes it
to change its internal reference to instead point to a newly-created
String object with the value "3455".

When the method ends, the reference t is lost, but the original reference
yyy still points to the same tree node object. Then its getUserObject
method is called and returns the internal reference, which now points at
the new String object. This String's contents are then displayed.

The Integer object, which is not anymore references anywhere, is lost.

Thanks for your answer.
But i think MutableTreeNode::setUserObject() function should be an
accident. Because everything passed into a function, shouldn't able to
modify the original variale through the parameter.
I think we are unable to write a function like
,MutableTreeNode::setUserObject() , right? (if not agree, please give
me an example)

thanks
from Peter ([email protected])
 
P

Peter

Thanks for your answer.
But i think MutableTreeNode::setUserObject() function should be an
accident. Because everything passed into a function, shouldn't able to
modify the original variale through the parameter.
I think we are unable to write a function like
,MutableTreeNode::setUserObject() , right? (if not agree, please give
me an example)

thanks
from Peter ([email protected])
 
P

Peter

Thanks for your answer.
But i think MutableTreeNode::setUserObject() function should be an
accident. Because everything passed into a function, shouldn't able to
modify the original variale through the parameter.
I think we are unable to write a function like
,MutableTreeNode::setUserObject() , right? (if not agree, please give
me an example)

thanks
from Peter ([email protected])
 
M

Michael Borgwardt

Peter said:
Thanks for your answer.
But i think MutableTreeNode::setUserObject() function should be an
accident. Because everything passed into a function, shouldn't able to
modify the original variale through the parameter.

It works exactly as I wrote, there is no "accident". I do not understand what
your problem with it is.
I think we are unable to write a function like
,MutableTreeNode::setUserObject() , right? (if not agree, please give
me an example)

public class MutableTreeNode{
private Object userObject;
public void setUserObject(Object o){
userObject = o;
}
public Object getUserObject(){
return userObject
}
}

This is a standard technique known as "getter and setter methods" that is
probably used to some degree in well over 70% of all Java classes written
anywhere.

You seem to have problems understanding the basic concepts of object oriented
programming, so try to find a good book on it or read this:
http://java.sun.com/docs/books/tutorial/java/concepts/index.html
BTW, the notation "MutableTreeNode::setUserObject()" is not used in Java,
we use a dot (.) instead of the double colon :):).
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top