Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax "variable = variable = variable;" whats happening?

Okay so I'm reading some code about RedBlackTrees. And I noticed this line "v1 = v2 = v3 = v4;" and I understand something like "v1 += v2" (add v2 to the current value of v1) and "v1 = v2" (create a reference from v2 to v1) etc.

public void insert( AnyType item )
{
    current = parent = grand = header;

But I'm curious to what is happening in the memory/references with current = parent = grand = header;

http://faculty.washington.edu/moishe/javademos/REDBlack/RedBTree.java

Edit : 10:46 PM

I still have to wait 10 minutes to approve questions, sorry for the wait ladies and gents.

like image 358
classicjonesynz Avatar asked Aug 10 '26 05:08

classicjonesynz


1 Answers

The reference value of header gets assigned to each of current, parent and grand. Basically all the variables would refer to the same thing.

By the way, this practice is often frowned up on in real life programming.

Consider what would happen if you had code like this:

boolean flag = false;

if(flag = true) {
    System.out.println("true");
} else {
    System.out.println("false");
}

The output would be true here as what's really happening here is an assignment rather than a comparison.

like image 194
adarshr Avatar answered Aug 11 '26 20:08

adarshr