*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt About Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt About Strings" Watch "Doubt About Strings" New topic
Author

Doubt About Strings

Vanguri Jagannadh
Greenhorn

Joined: Apr 05, 2005
Posts: 2
1. String s1 = "hI".toUpperCase();
2. String s2 = "HI".toUpperCase();
3. String s = "HI";

4. System.out.println(s == s1); // Prints False
5. System.out.println(s == s2); // Prints True

Can some body please explain why line 4 is printing false and
line 5 is printing true.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
1. String obj1 = new String("Hi");
2. String obj2 = new String("Hi");
3. String obj3 = "Hi";
4. String obj4 = "Hi";
5. String obj5 = "hi";


obj1 == obj2; //false, in both cases memory is allocated and two objects are being created

obj3==obj4; //true, an object is being created at line 3. obj4 is refering to same object which is getting cretaed at line no 3.

a new object is beiing created at line no 5.

obj4 == obj5 //false, obj3 & obj4 are refering to one object & obj5 is refering to different object

== operator copmares the object's reference.

hope it would be helpful.
[ April 05, 2005: Message edited by: sanjeevmehra mehra ]

thanks & regards,<br />Sanjeev.
saurabh mishra
Greenhorn

Joined: Jan 22, 2004
Posts: 3
Hi Jagannadh,
There are following two ways to create a String object:
I) String s = new String("hello");
II) String s1 = "hello";
In the first case TWO string objects are created, one on the heap and another is kept in the string pool with the value "hello".

In the second case, only one object is created and that is kept in the String pool.

So if u r call "hello".toLowerCase() it will first check whether the string "hello" is already there in the string pool. It it finds one, it will NOT create a new string. Instead of that if u do "hello".toUpperCase(), it will check whether HELLO is there in the string pool. If it is not there it will create a new string object with the value HELLO. Now this is totally a new and different object.

Hope this will help.
Saurabh.
navneet shrivastava
Ranch Hand

Joined: Jul 09, 2004
Posts: 39
* "Strings are immutable objects" meaning that everytime u modify a String object a new Object is created in VM(i guess no longer in pool), and the ols String ref. will be set t this new object.

at line 1 s1 refers to Object obj1(say), in pool.
at line 1 after calling toUpperCase s1 refers to new Object obj2(say); remember Strings are immutable.

by now s1,s2 are no longer referening to same objects in pool/VM.
BUT s2 remains unmodified in pool and same goes for s.
Thus s==s2 returns TRUE
===========================================
1. String s1 = "hI".toUpperCase(); // line 1
2. String s2 = "HI".toUpperCase(); // line 2
3. String s = "HI"; // line 3

4. System.out.println(s == s1); // Prints False
5. System.out.println(s == s2); // Prints True

Can some body please explain why line 4 is printing false and
line 5 is printing true.
===========================================


+nav
Vanguri Jagannadh
Greenhorn

Joined: Apr 05, 2005
Posts: 2
Thanks for reply.

My doubt is when String s1 = "hi".toUpperCase() gets executed

does it not create both "hi" and "HI" in string pool?

If it gets created I was expecting s == s1 to print true.

Can you please tell me where I went wrong .

--> Jagannadh.V
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
How often does it have to be repeated: NEVER EVER EVER rely on == to return true for 2 reference variables however much the objects they are referencing look similar.


42
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
I have same doubt. Here I am trying to eloborate the question.

/*Creates two Strings "hi" and "HI" and s1 is pointing to "HI"*/
1. String s1 = "hI".toUpperCase();

/*As "HI" is already created, s2 will be pointing to same "HI" to which s1 is currently pointing*/
2. String s2 = "HI".toUpperCase();

/*s will also be pointing to same "HI"*/
3. String s = "HI";

/*Should this return True because s and s1 are pointing to same memory*/
4. System.out.println(s == s1); // Prints False

/*This will obviously return true*/
5. System.out.println(s == s2); // Prints True

I am not sure where these objects are created. Either on String pool or on heap. If some one could shed light on this would be a great help.

Thank you
tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Saurabh, I would like to correct u , please have a look on my explanation.

What u have narrated:
----------------------------------------------------------------------------

There are following two ways to create a String object:
I) String s = new String("hello");
II) String s1 = "hello";
In the first case TWO string objects are created, one on the heap and another is kept in the string pool with the value "hello".

In the second case, only one object is created and that is kept in the String pool.

So if u r call "hello".toLowerCase() it will first check whether the string "hello" is already there in the string pool. It it finds one, it will NOT create a new string. Instead of that if u do "hello".toUpperCase(), it will check whether HELLO is there in the string pool. If it is not there it will create a new string object with the value HELLO. Now this is totally a new and different object.
----------------------------------------------------------------------------


Look this code:

----------------------------------------------------------------------------
String s2 = "Hi".toUpperCase();
String s = "HI";

System.out.println(s == s2); // Prints False
----------------------------------------------------------------------------

Please explain this.
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
-----------------------------------------------
String s1 = "hI".toUpperCase();
String s2 = "HI".toUpperCase();
String s = "HI";

System.out.println(s == s1); // Prints False
System.out.println(s == s2); // Prints True
-------------------------------------------

whenever we call upon built in method like toUpperCase, toLowercase, trim, replaceetc., it first look upon whether any changes to be done before on the given string.. if so it create new string in string pool if not available...if no need for any change then the method will return same string..

in the above program .....

String s1 = "hI".toUpperCase();
String s = "HI";

System.out.println(s == s1); // Prints False

because there is change in s1 and it going to point new string

but in this

String s2 = "HI".toUpperCase();
String s = "HI";

System.out.println(s == s2); // Prints True

its going to return same string...

check the source code of toUppercase in java API it first checks whether there is any modification needed for string

All plz check source code of String methods...

it more helps U
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
This is what I understood after running javap and going through the instructions.

The string contants "hI', "HI" are loaded at compile time onto a constant Pool.

Hence when the class is loaded s1 will be pointing to "hI' and s2 and s will be pointing to "HI".

The method toUpperCase() on s1 will be called at runtime, Hence a new String will be created on Heap and now s1 will be pointing to this memory which is different from s2 and s.

Even though toUpperCase() is called on s2 and as there is no change in the String, no new memory is created at runtime but same string is returned.

Hence s == s1 will always return FALSE
and s == s2 will always return TRUE.

In Pictorial Format.

At compile time

At Run time

Please let me know if my understanding is wrong.

Thank you
[ April 05, 2005: Message edited by: s penumudi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt About Strings