fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s1 = "abc";
  13. String s2 = "pqr";
  14. System.out.println((s1 + s2) == (s1 + s2)); // false
  15.  
  16. final String s3 = "abc";
  17. final String s4 = "pqr";
  18.  
  19. // true, because s3+s4 is a constant, and thus the compiler puts the
  20. // value in the constant pool.
  21. System.out.println((s3 + s4) == (s3 + s4));
  22.  
  23. // false, because the value of concat isn't a compile-time constant.
  24. System.out.println(s3.concat(s4) == s3.concat(s4));
  25. }
  26. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
false
true
false