We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I don't understand the problem with the following code. Is that normal ? Please, have someone an answer ? Thanks !
String text1="A";
String text2="A ...";
String[] text3=split(text2,' ');
String text4=text3[0];// text4= the first part of text2 ="A"
//Printing it, I see it's the same
println(text4); // prints "A"
println(text1); // prints exactly the same
// But...
println(text4==text1); // say that it wasn't the same; why is "A"(from text1) not "A"(from text4) ??
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Java's equality operator
==
: https://Processing.org/reference/equality.htmlcompares the value stored by 2 variables against each other.
However, when dealing w/ objects, in your case 2 String instances, that value corresponds to their memory address. A.K.A. pointer or reference! @-)
Which means the memory address stored in variable text4 isn't the same as text1's. =;
They're different objects located at distinguished memory addresses! :-B
Now in order to compare the content of any object, we use its equals() operator instead: *-:)
http://docs.Oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-
https://forum.Processing.org/two/discussions/tagged?Tag=equals()
Thank you very much !