1

Why does this code block return false...

char ra1= '\u30E9';
char ra2= '\u30E9';
Character RA1= ra1;
Character RA2= ra2;
System.out.println("Does Character RA1 == Character RA2? " + (RA1 == RA2)); //returns false

...when this code block returns true?

char a1= 'a';
char a2= 'a';
Character A1= a1;
Character A2= a2;
System.out.println("Does Character A1 == Character A2? " + (A1 == A2)); //returns true

They seem like they're doing the same thing to me, and both Characters hold the same char value so I don't understand why == returns false for one and true for the other

14wml
  • 4,048
  • 11
  • 49
  • 97
  • Because `Character.valueOf('a')` is [guaranteed to return a cached value](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#valueOf(char)), but `Character.valueOf('\u30E9')` is not. – Andy Turner Aug 22 '17 at 03:47
  • @AndyTurner would you mind explaining a little more what you mean (perhaps in an answer post). Because to me `'\u30E9'` and `a` both seem like valid chars – 14wml Aug 22 '17 at 14:35
  • I can't post an answer because the question is locked. But it is explained in the linked Javadoc. – Andy Turner Aug 23 '17 at 03:56

0 Answers0