Yes this can get confusing so here’s a link explaining that :
Hmmm …
I need some time to think about these things …
I’ll let you know if I hve more questions …
(I can see now why some people have a “distaste” for java …)
If how reference behaves under Java was the reason why Java isn’t liked much folks would also hate JavaScript, Python and almost all modern programming languages!
Actually programmers tend to dislike Java b/c we have to explicitly declare the datatype of each single variable just like old C/C++ do!
BtW I’ve got some previous posts about Java’s reference aliases below:
Nice point here!
@ProgrammingN00b Java is relatively easy compared to C++ believe me
Well, I am just very much used to Var1 = Var2 only “copying” the value of Var2 into Var1 …
That’s exactly the assign operator =
does:
It replaces the value of the variable from the left side w/ a copy of the value from the right side.
But depending on the left variable’s datatype that value is interpreted differently.
If it’s a primitive datatype that value is interpreted as a number.
Otherwise it’s interpreted as the 1st memory address of an object’s contiguous memory block.
Yes, that is the weird part, because C or C++ does not do that …
Can you explain how C/C++ variable assigning is different than Java’s then?
Variable1 = 5;
Variable2 = 10;
Variable1 = Variable2;
//The value of Variable1 would now be 10
Variable2 = 20;
//Setting the new value of variable2 to 20
//Variable1 would still be 10 and not change as Variable2 gets changed
That’s exactly what happens in Java too! So I dunno what’s your point.
int var1 = 5, var2 = 10;
println(var1, var2); // 5, 10
var1 = var2;
println(var1, var2); // 10, 10
var2 = 20;
println(var1, var2); // 10, 20
exit();
But in C++ this would copy the “value” no matter what type the variable is!
So even if it is a custom class / object …
Or a member variable of an object / class …
By “copy” you actually mean “clone”.
That is, rather than assigning a copy of the same value from right to left C/C++ would clone an object, thus generating a new value, which is then assigned to the left variable.
In Java cloning isn’t done automatically. We’d need to code a function for it.
Usually if i have to deal with copies, ill use something which can store an object usually an arraylist. I use one for the original and a second one for all copies.
Then i iterate through the first arraylist and based on the conditions im looking for ill code an appropriate logic statement and set an index object in the second arraylist or empty the arraylist and add the desired object.
Not necessarily as direct as in other languages but it works.
Im sure there might be better ways though.
Ok, I guess I will try to create a function to “clone” values from one variable to another …
(That’s what I get for saying “this processing stuff is easier then c++”, oh well …)
That is probably some good advice, but I don’t understand enough of this yet to fully comprehend it …
I just found this article:
It esplains the differences between c++ and java.
But it appears that that function variable.clone() does not exist in processing …
Look again what’s stated on the wiki screenshot I’ve posted earlier:
// the Foo class must implement the Cloneable interface
// for this code to compile
As I’ve warned you before we’ve gotta code our own clone() function.
Here’s an example of how to implement a clone() method within a class:
I think found a better solution:
Something like “processing” but for c++!
https://openframeworks.cc/