File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is the result? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the result?" Watch "What is the result?" New topic
Author

What is the result?

Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6
I'm expecting 2,3, but it gives me 1,0.
Could someone tell what is wrong?

public class Node {
public int element;
public Node next;
}
public class MergeSort {

public Node[] split(Node node){
Node[] r=new Node[2];
Node a=new Node();
Node b=new Node();

while(node!=null){
a=moveNode(a,node);
System.out.println(node.element);
node=node.next;
b=moveNode(b,node);
System.out.println(node.element);
break;
}
r[0]=a;
r[1]=b;
return r;
}

public Node moveNode(Node target,Node toAdd){

Node newNode=target;
if (toAdd!=null){
newNode=toAdd;
toAdd=toAdd.next;
newNode.next=target;
}
return newNode;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Node a1=new Node();
a1.element=1;
Node a2=new Node();
a2.element=2;
a1.next=a2;
Node a3=new Node();
a3.element=3;
a2.next=a3;
Node a4=new Node();
a4.element=4;
a3.next=a4;

MergeSort m=new MergeSort();
Node[] splited=m.split(a1);

}

}

Thanks!

Jim
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

"Jim",
Welcome to JavaRanch!

We're pleased to have you here with us, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Jim, can you explain why you're expecting output of 2 and 3? Here's what I see happening...

When split is called, the variable node is set to reference a1 (with a1.next = a2).

The method moveNode(Node target, Node toAdd) returns a reference to the second argument (toAdd) with its next variable referencing the first agrument (target).

But note that toAdd is a local variable within the method, so the assignment toAdd = toAdd.next does nothing outside of the method. In particular, moveNode(a, node) does not change the node reference to a1. So node.element is still 1, which is what's printed.

But moveNode(a,node) does have the effect of setting node.next = a, becaue although toAdd is a local variable, it's still referencing the same object, whose variables (in this case, next) can be modified.

So the assignment node = node.next sets node = a. But because a.element was never explicitly set, node.element is now equal to the default initialization value of 0.

As before, the second call to moveNode(b,node) does not change the node reference. So node is still referencing a, and node.element is 0, which is what's printed.

PS: Please use code tags to keep your formatting intact. This makes it easier for others to help...

[ August 05, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6
Mark:
Thanks for the comment!
I guess what confuses me is: pass by val.

If node is passed by val in line "a=moveNode(a,node);", first System.out should print "1", and second System.out should print "2".

As moveNode(a,node) and moveNode(b,node) has no effect on node, which is a1->a2->a3->a4

But, I couldn't figure out why it prints "1,0" instead.
Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6
Mark:

If move(Node target, Node source) does change the value of node in split, how should pass by val explained here?

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jim Soong:
... If node is passed by val in line "a=moveNode(a,node);", first System.out should print "1", and second System.out should print "2"...

Review these 2 paragraphs of my post...
But moveNode(a,node) does have the effect of setting node.next = a, becaue although toAdd is a local variable, it's still referencing the same object, whose variables (in this case, next) can be modified.

So the assignment node = node.next sets node = a. But because a.element was never explicitly set, node.element is now equal to the default initialization value of 0.

Basically, method arguments are copies of references, and these copies are local to the method.

So if I have myRef = someObject and I call myMethod(myRef), then myRef becomes a local variable within the scope of myMethod. It is a different variable than the "myRef" outside of the method. However, both of these variables are still pointing to the same object. So within the method, if I say myRef.var = 7, this will modify the object itself, and that change will be visible through all references pointing to that object.

On the other hand... Within the method, if I say myRef = someOtherObject, then this assignment is valid only within the method (because that's the scope of the local variable). I'm not changing the object -- I'm just re-assigning the local variable to point to a different object. Outside of the method, the other "myRef" is still pointing to someObject.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jim Soong:
... If move(Node target, Node source) does change the value of node in split, how should pass by val explained here? ...

Within the split method, the call to moveNode does not change the value of node. The value of node is changed within split by the line, node = node.next.
Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6
Originally posted by marc weber:

Basically, method arguments are copies of references, and these copies are local to the method.

So if I have myRef = someObject and I call myMethod(myRef), then myRef becomes a local variable within the scope of myMethod. It is a different variable than the "myRef" outside of the method. However, both of these variables are still pointing to the same object. So within the method, if I say myRef.var = 7, this will modify the object itself, and that change will be visible through all references pointing to that object.

On the other hand... Within the method, if I say myRef = someOtherObject, then this assignment is valid only within the method (because that's the scope of the local variable). I'm not changing the object -- I'm just re-assigning the local variable to point to a different object. Outside of the method, the other "myRef" is still pointing to someObject.
Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6

Where do I find these details?
Could you recommend a book or some url?

Thanks for the GREAT help!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jim Soong:
... Could you recommend a book or some url? ...

Glad I could help! I think you'll find JavaRanch to be your best resource on the internet.

As far as books, my personal recommendation is Bruce Eckel's Thinking in Java, which is available in book form or (free) online...

http://www.faqs.org/docs/think_java/TIJ3.htm

In particular, you might be interested in "Appendix A: Passing & Returning Objects."

Eckel is hoping to finish a 4th edition of the book (updated for Java 5.0) by the end of summer, but I don't know what that means in terms of an actual release date. Here is his site...

http://www.bruceeckel.com/

But the book(s) that will work for you depend on individual learning style. Another very popular book -- quite different from Eckel in terms of style -- is Head First Java by Kathy Sierra and Bert Bates. This book is often referred to on these boards as "K&B" for "Kathy & Bert."
Jim Soong
Greenhorn

Joined: Aug 05, 2005
Posts: 6
Mark:
I've read about passing by val in Java from different books, several time, and I memorized it. But your explanation is the BEST. It helps me to visualize what is happening when passing a param, and I don't need to REMEMBER the result.
Thanks again!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is the type of code that is helpful to verify these ideas...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the result?
 
Similar Threads
why variables inside interface has to final
Array
Garbage Collection :exam lab
dan's sigle topic--arrays
Multidimensional Arrays