Skip to content

bad example on String reference page #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mmalade opened this issue Jan 16, 2018 · 1 comment
Closed

bad example on String reference page #636

mmalade opened this issue Jan 16, 2018 · 1 comment

Comments

@mmalade
Copy link

mmalade commented Jan 16, 2018

Issue description

When I tested the String comparison example given on the String reference page in Processing 3.3.6:

// Comparing String objects, see reference below.
String p = "potato";
if (p == "potato") {
  println("p == potato, yep.");  // This will not print
} 
// The correct way to compare two Strings
if (p.equals("potato")) {
  println("Yes, the contents of p and potato are the same.");
}

Both lines print due to String interning in Java.

URL(s) of affected page(s)

https://processing.org/reference/String.html

Proposed fix

Use a different example:

String a = "good";
String b = "bye";
String c = a + b;

println(c);

if (c == "goodbye") {
  println("The == works"); 
}

if (c.equals("goodbye")) {
  println("The .equals works"); 
}
Abhi464 added a commit to Abhi464/processing-docs that referenced this issue Jan 20, 2018
@REAS
Copy link
Member

REAS commented Jul 26, 2019

I decided to modify the example and to suggest the best approach is to use the equals() method. The direct example using == just confused the matter.

@REAS REAS closed this as completed Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants