Created
February 16, 2012 13:11
-
-
Save andreafrancia/1844741 to your computer and use it in GitHub Desktop.
Equality for strings in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Run with: | |
javac Prova.java && java Prova | |
*/ | |
class Prova { | |
public static void main(String[] args) { | |
StringBuilder o = new StringBuilder("o"); | |
System.out.println("\"pippo\" == \"pippo\" ~~> " + ( "pippo" == "pippo") ); | |
System.out.println("\"pippo\" == \"pipp\" + \"o\" ~~> " + ( "pippo" == "pipp" + "o") ); | |
System.out.println("\"pippo\" == \"pipp\" + o ~~> " + ( "pippo" == "pipp" + o) ); | |
System.out.println("\"pippo\".equals(\"pipp\" + o) ~~> " + ( "pippo".equals("pipp" + o)) ); | |
} | |
} | |
/* Results | |
"pippo" == "pippo" ~~> true | |
"pippo" == "pipp" + "o" ~~> true | |
"pippo" == "pipp" + o ~~> false | |
"pippo".equals("pipp" + o) ~~> true | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment