3
    String s1 = new String("anil");
    String s2 = s1.toUpperCase();
    String s3 = s1.toLowerCase();
    System.out.println(s1 == s2);
    System.out.println(s1 == s3);

if string object created in heap then both are false.But it gives false,true.

Anil Kumar
  • 1,431
  • 3
  • 13
  • 14

5 Answers5

3
String s1 = new String("anil");

This statement creates a new object

And this ,

String s3 = s1.toLowerCase();

points the location of 1st object that is s1

And thats the reason you are getting true for second condition

Also see how java handles strings to get a clear understanding

Hope this helps!!

Santhosh
  • 8,181
  • 4
  • 29
  • 56
1

There are four String objects here:

  • the literal, created by the compiler and classloader
  • s1, created by new String()
  • s2, created by toUpperCase()
  • s3, created by toLowerCase().

No two of them are equal via the == operator.

Except that toLowerCase() may return the same object if it is already lowercase. There's nothing in the Javadoc about that, so any such behaviour in an implementation cannot be relied on.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Here S1 object will be created in heap. Its value is stored in the constant string pool.

S2 is String literal not an object. So first JVM will check whether the string is there in constant pool. If String is there constant pool it will not create new object. It will return reference of the object available.

Here the s1.toUpper will return "ANIL". "ANIL" is not in the constant pool. so new object will be created. and comparing it with s1 (using'==') give false.

Same for S3. But for S3 it wont create new object as "anil" is already there in constant pool. so will return the reference of S1. So it gives true.

Study the following link

Study this

Community
  • 1
  • 1
0

If you look at the toLowerCase() method in String class.

  1. It calls toLowerCase(Locale locale)
  2. toLowerCase(Locale locale) inturn uses Character.toLowerCase(c)
  3. Character.toLowerCase(c) in Character class has this comment -

    • @param ch the character to be converted.
    • @return the lowercase equivalent of the character, if any;
    • otherwise, the character itself.
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Case 1: String with Capital First Letter.

> String s1 = new String("Ajay")
String s2 = s1.toUpperCase()
String s3 = s1.toLowerCase()
System.out.println s1 == s2
System.out.println s1 == s3
false
false

Case 2: String with Small First Letter.

> String s1 = new String("ajay")
String s2 = s1.toUpperCase()
String s3 = s1.toLowerCase()
System.out.println s1 == s2
System.out.println s1 == s3
false
true

in Case 1, since the string has capital letter, converting to lowercase will yield a new object hence a new reference for it while in Case 2 small first letter after converting to lowercase will still point to the same object because the original object was same hence creating two references for the same object.

You can see the output from the Groovy Shell pretty clear.

ajknzhol
  • 6,322
  • 13
  • 45
  • 72