Confused about the reference in Java
up vote
1
down vote
favorite
I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.
public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
output
MyClass$Node@e6ea0c6
null
java
|
show 1 more comment
up vote
1
down vote
favorite
I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.
public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
output
MyClass$Node@e6ea0c6
null
java
1
nextdoes not point toroot.children[0], it's a copy ofroot.children[0]and therefore references the same object asroot.children[0]for as long asroot.children[0]is not changed.
– Thomas Kläger
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.
public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
output
MyClass$Node@e6ea0c6
null
java
I am confused about why the value of next is not the same as the root.children[0]? In my understanding, next points to root.children[0]. Therefore, if the value of root.children[0] is changed, next should also change.
public class MyClass {
public static void main(String args) {
Node root = new Node();
Node next = root.children[0];
root.children[0] = new Node();
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
output
MyClass$Node@e6ea0c6
null
java
java
edited Nov 7 at 8:18
Mark Jeronimus
4,76421836
4,76421836
asked Nov 7 at 7:47
LoveTW
1,46683045
1,46683045
1
nextdoes not point toroot.children[0], it's a copy ofroot.children[0]and therefore references the same object asroot.children[0]for as long asroot.children[0]is not changed.
– Thomas Kläger
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05
|
show 1 more comment
1
nextdoes not point toroot.children[0], it's a copy ofroot.children[0]and therefore references the same object asroot.children[0]for as long asroot.children[0]is not changed.
– Thomas Kläger
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05
1
1
next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.– Thomas Kläger
Nov 7 at 7:51
next does not point to root.children[0], it's a copy of root.children[0] and therefore references the same object as root.children[0] for as long as root.children[0] is not changed.– Thomas Kläger
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.
Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing
If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node
When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object
So... the output of the program is something likeNode@a153fb40and thennull, right ?
– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
add a comment |
up vote
4
down vote
Let's dissect this code line by line:
Node root = new Node();
You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.
Node next = root.children[0];
As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.
root.children[0] = new Node();
Now children[0] is being assigned a non-null value. Note that this does not change the value of next.
Sonextis still null at the end of execution, right?
– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.nextis only set once (to null), and it will stay that way.
– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between usingroot.children[0]in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
add a comment |
up vote
0
down vote
Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.
public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.
Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing
If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node
When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object
So... the output of the program is something likeNode@a153fb40and thennull, right ?
– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
add a comment |
up vote
3
down vote
accepted
Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.
Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing
If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node
When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object
So... the output of the program is something likeNode@a153fb40and thennull, right ?
– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.
Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing
If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node
When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object
Consider it like this, I'll mark the object in memory as {} and the reference as ->
So you start by next = root.children[0], at this time root.children[0] -> null, it points to nothing in memory, no object, so next -> null.
Then you do root.children[0] -> {a new Node} but next is still next -> null it doesn't point to the same object, it's not a shortcut to root.children[0], it's NOT next -> root.children[0] -> {a new Node}, next points to nothing
If you had root.children[0] -> {a new Node}, and then do next = root.children[0], then next would point next -> {a new Node}, but again if your do now root.children[0] = new Node() it will result in root.children[0] -> {a newer Node}
and next will NOT point to this newer node
When you assign a object's reference to a variable, that variable will not always point to the same address in memory, by doing new Node() you create a new object somewhere in memory and with = you tell a variable to point to that newly allocated object
answered Nov 7 at 7:53
Blagoj Atanasovski
320111
320111
So... the output of the program is something likeNode@a153fb40and thennull, right ?
– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
add a comment |
So... the output of the program is something likeNode@a153fb40and thennull, right ?
– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
So... the output of the program is something like
Node@a153fb40 and then null, right ?– vikingsteve
Nov 7 at 8:18
So... the output of the program is something like
Node@a153fb40 and then null, right ?– vikingsteve
Nov 7 at 8:18
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
Sorry, which program are you referring to?
– Blagoj Atanasovski
Nov 7 at 9:37
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
of course ´MyClass.java´. (I had not seen the output in the OP question, now when I reread it I see the output there)
– vikingsteve
Nov 7 at 10:38
add a comment |
up vote
4
down vote
Let's dissect this code line by line:
Node root = new Node();
You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.
Node next = root.children[0];
As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.
root.children[0] = new Node();
Now children[0] is being assigned a non-null value. Note that this does not change the value of next.
Sonextis still null at the end of execution, right?
– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.nextis only set once (to null), and it will stay that way.
– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between usingroot.children[0]in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
add a comment |
up vote
4
down vote
Let's dissect this code line by line:
Node root = new Node();
You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.
Node next = root.children[0];
As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.
root.children[0] = new Node();
Now children[0] is being assigned a non-null value. Note that this does not change the value of next.
Sonextis still null at the end of execution, right?
– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.nextis only set once (to null), and it will stay that way.
– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between usingroot.children[0]in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
add a comment |
up vote
4
down vote
up vote
4
down vote
Let's dissect this code line by line:
Node root = new Node();
You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.
Node next = root.children[0];
As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.
root.children[0] = new Node();
Now children[0] is being assigned a non-null value. Note that this does not change the value of next.
Let's dissect this code line by line:
Node root = new Node();
You created a new Node object. This object has a children array of length 1. Since you have not assigned anything to children yet, the array contains the single element null.
Node next = root.children[0];
As I said, children[0] is null, so next is now null. Note that in this line, you did not make it so that next always points to the same thing as children[0]. You only made next point to the same thing as children[0] is pointing to at that time.
root.children[0] = new Node();
Now children[0] is being assigned a non-null value. Note that this does not change the value of next.
answered Nov 7 at 7:55
Sweeper
60k967134
60k967134
Sonextis still null at the end of execution, right?
– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.nextis only set once (to null), and it will stay that way.
– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between usingroot.children[0]in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
add a comment |
Sonextis still null at the end of execution, right?
– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.nextis only set once (to null), and it will stay that way.
– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between usingroot.children[0]in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(
– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
So
next is still null at the end of execution, right?– vikingsteve
Nov 7 at 8:15
So
next is still null at the end of execution, right?– vikingsteve
Nov 7 at 8:15
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.
next is only set once (to null), and it will stay that way.– Sweeper
Nov 7 at 8:17
@vikingsteve Yes. The value of a variable doesn't change until the programmer changes it.
next is only set once (to null), and it will stay that way.– Sweeper
Nov 7 at 8:17
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using
root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(– LoveTW
Nov 7 at 8:18
I do not accept your answer because I think @Blagoj Atanasovski helps me understand the difference between using
root.children[0] in different scenarios. However, I still like your answer! Your answer is very inspiring, too. It's sad that I could not accept two answer :(– LoveTW
Nov 7 at 8:18
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW I respect your choice. I was just a little confused because for a second there, you didn't accept any answer, which is why I deleted my comment after you accepted the other one :).
– Sweeper
Nov 7 at 8:20
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
@LoveTW i think both answers deserve an upvote. +1 from me :)
– vikingsteve
Nov 7 at 8:40
add a comment |
up vote
0
down vote
Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.
public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
add a comment |
up vote
0
down vote
Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.
public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.
public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
Currently, root.children[0] just have reference not the object. So, You need to First add children to root node and then assign as I have changed into below code.
public class MyClass {
public static void main(String args) {
Node root = new Node();
root.children[0] = new Node();
Node next = root.children[0];
System.out.println(root.children[0]);
System.out.println(next);
}
public static class Node {
Node children = new Node[1];
}
}
answered Nov 7 at 7:58
Raheela Aslam
3076
3076
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185325%2fconfused-about-the-reference-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
nextdoes not point toroot.children[0], it's a copy ofroot.children[0]and therefore references the same object asroot.children[0]for as long asroot.children[0]is not changed.– Thomas Kläger
Nov 7 at 7:51
When you assigned root.children[0] to next, root.children[0] was null, and only later you assigned an object to root.children[0] hence both are not same
– Pushpesh Kumar Rajwanshi
Nov 7 at 7:51
you assigned the value of that moment to next, and at that point, it was null. if you want that new Node() to be referred by next, you should move the Node next = root.children[0]; to after the root.children[0] = new Node(); line
– Stultuske
Nov 7 at 7:52
This is very bad code!! Unless it is some sort of programming challenge, you should consider rewriting it. If that's the case (you would like a rewrite), Im happy to add an answer for you. Real world java code should never look like this code as it stands, however...
– vikingsteve
Nov 7 at 8:02
@vikingsteve Yes, this is an example from the coding challenge. I face the problem when trying to build a Trie structure.
– LoveTW
Nov 7 at 8:05