Why does this linked list traversal work? How are the references working?Is Java “pass-by-reference” or “pass-by-value”?What is the difference between a variable, object, and reference?How does the Java 'for each' loop work?Why does Java have transient fields?How to detect a loop in a linked list?How to reference a method in javadoc?Writing linked list functionsWhy does this code using random strings print “hello world”?how to get head of the list after traversal in Linked lists?Traversal In linked listI don't understand the implementation of inserting a new node in linked listWhy I can't traverse this linked list in C?

Delete n lines skip 1 line script

How to identify whether a publisher is genuine or not?

What action is recommended if your accommodation refuses to let you leave without paying additional fees?

Is it good to engage in exceptional cases where it is permissible to do a typically forbidden action to which one has a taivah for

Why are the wings of some modern gliders tadpole shaped?

How to say "respectively" in German when listing (enumerating) things

Knights and Knaves: What does C say?

How to add the real hostname in the beginning of Linux cli command

What would happen if I build a half bath without permits?

Detail vs. filler

Verb ending in -ん with positive meaning?

How do I introduce dark themes?

Why is STARTTLS used when it can be downgraded very easily?

How does Monks' Improved Unarmored Movement work out of combat?

Duck, duck, gone!

Convert a string of digits from words to an integer

Was the ruling that prorogation was unlawful only possible because of the creation of a separate supreme court?

How to bring home documents from work?

Looking for circuit board material that can be dissolved

Beyond Futuristic Technology for an Alien Warship?

I transpose the source code, you transpose the input!

Would a horse be sufficient buffer to prevent injury when falling from a great height?

Why isn't there armor to protect from spells in the Potterverse?

French license plates



Why does this linked list traversal work? How are the references working?


Is Java “pass-by-reference” or “pass-by-value”?What is the difference between a variable, object, and reference?How does the Java 'for each' loop work?Why does Java have transient fields?How to detect a loop in a linked list?How to reference a method in javadoc?Writing linked list functionsWhy does this code using random strings print “hello world”?how to get head of the list after traversal in Linked lists?Traversal In linked listI don't understand the implementation of inserting a new node in linked listWhy I can't traverse this linked list in C?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.



Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?



Note: I know this code doesnt handle edge cases. like null nodes, etc...



public class LLnode
int value;
LLnode next;

public LLnode(int x)
this.value = x;
this.next = NULL;



/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?

front.next = front.next.next; //why DOES this physically change the LL ?

public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);

//assignments
c.next = d;
b.next = c;
a.next = b;

removeNode(a,30);




Thanks.










share|improve this question
























  • front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

    – Jon Skeet
    Mar 28 at 20:10

















0















In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.



Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?



Note: I know this code doesnt handle edge cases. like null nodes, etc...



public class LLnode
int value;
LLnode next;

public LLnode(int x)
this.value = x;
this.next = NULL;



/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?

front.next = front.next.next; //why DOES this physically change the LL ?

public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);

//assignments
c.next = d;
b.next = c;
a.next = b;

removeNode(a,30);




Thanks.










share|improve this question
























  • front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

    – Jon Skeet
    Mar 28 at 20:10













0












0








0








In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.



Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?



Note: I know this code doesnt handle edge cases. like null nodes, etc...



public class LLnode
int value;
LLnode next;

public LLnode(int x)
this.value = x;
this.next = NULL;



/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?

front.next = front.next.next; //why DOES this physically change the LL ?

public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);

//assignments
c.next = d;
b.next = c;
a.next = b;

removeNode(a,30);




Thanks.










share|improve this question














In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.



Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?



Note: I know this code doesnt handle edge cases. like null nodes, etc...



public class LLnode
int value;
LLnode next;

public LLnode(int x)
this.value = x;
this.next = NULL;



/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?

front.next = front.next.next; //why DOES this physically change the LL ?

public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);

//assignments
c.next = d;
b.next = c;
a.next = b;

removeNode(a,30);




Thanks.







java linked-list






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 20:07









shockwave88shockwave88

82 bronze badges




82 bronze badges















  • front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

    – Jon Skeet
    Mar 28 at 20:10

















  • front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

    – Jon Skeet
    Mar 28 at 20:10
















front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

– Jon Skeet
Mar 28 at 20:10





front = front.next; - that's just changing a local variable front so that the value is the current value of value.next. Compare that with front.next = front.next.next; which modifies the object referred to by the current value of front. You might find this question useful: stackoverflow.com/questions/32010172/…

– Jon Skeet
Mar 28 at 20:10












1 Answer
1






active

oldest

votes


















0
















Java is pass-by-value. front = head copies the reference value from head into front. Because of that front = front.next has no effect on the head. In the loop front is created only to point into current element, it's not used to maintain the list.



However front.next = front.next.next changes the next field in an object referenced by front. There is no reference copy for next field here, like previously front was a copy of head.






share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406044%2fwhy-does-this-linked-list-traversal-work-how-are-the-references-working%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    Java is pass-by-value. front = head copies the reference value from head into front. Because of that front = front.next has no effect on the head. In the loop front is created only to point into current element, it's not used to maintain the list.



    However front.next = front.next.next changes the next field in an object referenced by front. There is no reference copy for next field here, like previously front was a copy of head.






    share|improve this answer





























      0
















      Java is pass-by-value. front = head copies the reference value from head into front. Because of that front = front.next has no effect on the head. In the loop front is created only to point into current element, it's not used to maintain the list.



      However front.next = front.next.next changes the next field in an object referenced by front. There is no reference copy for next field here, like previously front was a copy of head.






      share|improve this answer



























        0














        0










        0









        Java is pass-by-value. front = head copies the reference value from head into front. Because of that front = front.next has no effect on the head. In the loop front is created only to point into current element, it's not used to maintain the list.



        However front.next = front.next.next changes the next field in an object referenced by front. There is no reference copy for next field here, like previously front was a copy of head.






        share|improve this answer













        Java is pass-by-value. front = head copies the reference value from head into front. Because of that front = front.next has no effect on the head. In the loop front is created only to point into current element, it's not used to maintain the list.



        However front.next = front.next.next changes the next field in an object referenced by front. There is no reference copy for next field here, like previously front was a copy of head.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 20:21









        Karol DowbeckiKarol Dowbecki

        30.3k9 gold badges43 silver badges63 bronze badges




        30.3k9 gold badges43 silver badges63 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406044%2fwhy-does-this-linked-list-traversal-work-how-are-the-references-working%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript