• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the result?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jim Soong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Thanks for the GREAT help!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the type of code that is helpful to verify these ideas...
 
reply
    Bookmark Topic Watch Topic
  • New Topic