variable assignment - Aliasing in Java with Arrays and Reference Types -


there many similar question on this, haven't been able find answer lists differences in aliasing, asking here.

i know simple primitive assignment statement copies values:

int x = 1; int y = x; x = 2; stdout.print(x); // prints 2 stdout.print(y); // prints 1 

then told arrays 'aliased' during assignment statements. so:

int[] x = {1, 2, 3, 4, 5}; int[] y = x; x[0] = 6; stdout.print(x[0]); // prints 6 stdout.print(y[0]); // prints 6 

however, if assign 1 of variables different array, aliasing 'disappears':

int[] x = {1, 2, 3, 4, 5}; int[] y = x; x = new int[]{1, 2, 3, 4, 5}; x[0] = 6; stdout.print(x[0]); // prints 1 stdout.print(y[0]); // prints 6 

what reason this?

then, come reference types. when using assignment statements, reference copied, not value. so:

counter c1 = new counter("ones"); counter c1.incrememnt(); // 0 -> 1 counter c2 = c1; c2.increment(); stdout.print(c1); // prints 2 stdout.print(c2); // prints 2 

but if assign c1 new counter object? happens c2; reference original counter or new counter, why?

i ask because thought reference types worked arrays. if create new counter , assign c1, both c1 , c2 point newly created counter. however, after going through exercises, created iterable stack adt seems violate assumption. code below:

import java.util.iterator;  public class mystack<item> implements iterable<item> {      private node first; // top of stack     private int n; // number of items      private class node {         item item;         node next;     }      public mystack() {}      public iterator<item> iterator() {         return new listiterator();     }      private class listiterator implements iterator<item> {          private node current = first;          public boolean hasnext() {             return current != null;         }          public item next() {             item item = current.item;             current = current.next;             return item;         }      }      public void push(item item) {         node oldfirst = first;         first = new node();         first.item = item;         first.next = oldfirst;         n++;     }      public item pop() {         if(!isempty()) {             node oldfirst = first;             first = first.next;             return oldfirst.item;         } else throw new runtimeexception("stack underflow");     }      public boolean isempty() { return size() == 0; }      public int size() { return n; }      public static void main(string[] args) {         mystack<integer> stack = new mystack<>();         stack.push(5);         stack.push(6);         stack.push(7);         stack.push(8);         stack.push(9);         for(int : stack) {             stdout.println(i);         }     }  } 

it's simple implementation, using linked list data structure items. main instance variable first, holds first node in linked list (top of stack). if in nested listiterator class, there's instance variable current assigned first. now, in push method, first reassigned newly created node. surely current variable still assigned old first node? why implementation work?

my hunch either a) don't understand how reference values passed (please explain) or b) when run through for loop in main method, implicitly calls creates new listiterator which, @ point, assigns current whatever current value of first is. if real reason, mean new iterator should created whenever method called within class? example, if create iterator explicitly, push few items stack, , reuse iterator without re-initialising - work intended?

please explain!

what reason this?

the variables x , y not arrays. array references. array object, , reassigned x refer different array y, , changed x, have different values in 2 different arrays.

what happens c2; reference original counter or new counter, why?

in example there 1 counter object created, when called new counter, there's no original, , made counter reference c2 refer same instance c1 refers to, pointing same thing.

surely current variable still assigned old first node? why implementation work?

your each loop invokes iterator() returns new listiterator instantiates it's own current points latest value of first in stack.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -