Results 1 to 2 of 2
- 04-17-2011, 07:13 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 52
- Rep Power
- 0
Why does change one variable also change another?
The code has 3 class as following
Java Code:import java.util.*; import java.awt.*; public class AList { public static void main(String[] args) { String s1,s2,s3; s1="a";s3="b"; s2=s1; System.out.println(s2); s2=s3; System.out.println(s1); GList g = new GList(); g.add("Luke"); } }
Java Code:public class GList<E> { private static class Node<E> { public E data; public Node<E> next; public Node(E data, Node<E> next) { this.data = data; this.next = next; } public E get() { return this.data; } } private Node<E> head; public GList() { head = new Node<E>(null, null); } public void add(E e) { Node<E> c=head; while ( c.next!=null) c=c.next; Node<E> newNode=new Node<E>(e, null); c.next=newNode; } }
After s2=s3, s2 contains "b". s1 still has "a". But after
c.next=newNode, head.next is not null and contains newNode.
Why? Thank you.Last edited by Fubarable; 04-17-2011 at 06:29 PM. Reason: Moderator Edit: Code tags added
- 04-17-2011, 04:59 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 12
Similar Threads
-
Why wont my boolean variable change?
By equal in forum New To JavaReplies: 8Last Post: 02-22-2011, 11:43 PM -
can i listen to variable change in diffrent class
By gloomy1991 in forum New To JavaReplies: 2Last Post: 01-08-2011, 02:50 AM -
How to change JDK?
By mew in forum EclipseReplies: 5Last Post: 05-21-2010, 06:21 AM -
Change in O/p
By shiv_122 in forum Advanced JavaReplies: 4Last Post: 12-22-2008, 03:37 AM -
Is it possible to change the '\n' into ' ' ...
By johnny7white in forum New To JavaReplies: 1Last Post: 11-15-2007, 03:32 PM
Bookmarks