fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String strObj = new String("Hello!");
  10. String str = "Hello!";
  11. // The two string references point two strings that are equal
  12. if (strObj.equals(str)) {
  13. System.out.println("The strings are equal");
  14. }
  15. // The two string references do not point to the same object
  16. if (strObj != str) {
  17. System.out.println("The strings are not the same object");
  18. }
  19. // If we intern a string that is equal to a given literal, the result is
  20. // a string that has the same reference as the literal.
  21. String internedStr = strObj.intern();
  22. if (internedStr == str) {
  23. System.out.println("The interned string and the literal are the same object");
  24. }
  25. }
  26. }
Success #stdin #stdout 0.12s 52576KB
stdin
Standard input is empty
stdout
The strings are equal
The strings are not the same object
The interned string and the literal are the same object