The compiler does some optimizations in your case so that s1
and s2
are really the same object. You can work around that by using
String s1 = new String( "Hello" );
String s2 = new String( "Hello" );
Then you have two distinct objects with the same text content.
More Related Contents:
- Sort on a string that may contain a number
- Java comparison with == of two strings is false? [duplicate]
- Difference between null and empty (“”) Java String
- How do I make my string comparison case-insensitive?
- What makes reference comparison (==) work for some strings in Java?
- How to check if my string is equal to null?
- Is there a Java utility to do a deep comparison of two objects? [closed]
- String comparison and String interning in Java
- compareTo() vs. equals()
- When does HashSet ‘add’ method calls equals? [duplicate]
- What does comparison being consistent with equals mean ? What can possibly happen if my class doesn’t follow this principle?
- Why Java does not see that Integers are equal?
- String equals and == with String concatenation [duplicate]
- Comparing a string with the empty string (Java)
- When “” == s is false but “”.equals( s ) is true
- Write a function that will sort a string array using Java
- String class replace() method [closed]
- How to sort an array of objects in Java?
- What is the backslash character (\\)?
- why equals() method when we have == operator? [duplicate]
- How to capitalize the first letter of a String in Java?
- Using Enum values as String literals
- How does Java store Strings and how does substring work internally? [closed]
- Java String.indexOf and empty Strings
- Why cannot cast Integer to String in java?
- How can I find whitespace in a String?
- How to find all permutations of a given word in a given text?
- Is there a way to check if two Collections contain the same elements, independent of order?
- Java, Check if a String is a palindrome. Case insensitive
- How to convert a Java String to an ASCII byte array?