• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Doubt About Strings

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* "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.
===========================================
 
Vanguri Jagannadh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-----------------------------------------------
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
no wonder he is so sad, he hasn't seen this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic