aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about string 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 "about string" Watch "about string" New topic
Author

about string

Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
Hi..
What is the output of the following code



output is
arit
amit
s2=arit s3=arit
false
true


actualy s2 and s3 have same string but why the result of s2==s3 is false

tx in adv..
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
My understanding is that :

The "arit" string will remain in pool for use by s3 and s4.

But when you do

a new object is created.
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
My understanding is that :

The "arit" string will remain in pool for use by s3 and s4.

But when you do

a new object is created.

See this modified code where i made another string s5.
Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
Hi Shilp..

The new one is String object or a String litral???
Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
I understand about the comparasion with S5

but still my doubt is

what happen when s.replace('m','r');

the new one is String object??

tx for ur quick reply
Shilpi M Ag
Ranch Hand

Joined: May 07, 2004
Posts: 40
in my understanding it is an object..

String strTest = "hey"; //literal
String strTestMore = new String("hello"); //object

Can someone confirm this
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
There are a few key pieces of information that you need to understand to be able to answer this question. Some of this information is fair game for the exam and some is not.

Let's start with what is fair game for the exam. Strings are immutable. That means that a String object can never be changed. So what happens when you invoke a method such as "replace" which seems to change the String? Well, it returns a brand new String object, of course. You see that with the first couple lines of your example - even though invoked replace() on s1, you see a couple lines later that s1 has remained unchanged.

Secondly, you must understand how the "==" operator works. With primitive types, it will check to see if the values of those primitives are the same. With reference types, however, it checks to see if the two reference variable refer to the same object. Note that it doesn't check to see if the two objects are "equal," it checks to see if both variables refer to the same object. It's really checking the reference value of each variable. You can read this for some more info, if you'd like.

So now we'll look at the information that isn't on the exam. Because Strings are immutable in Java, there is an optimization that is used to save space - the String literal pool. The String literal pool is basically a table of constants. Any time a String literal is found in your code, a new entry is added to the pool and that String object is created on the heap. From that point on, if you have the same String literal in your code, that literal will reference the exact same object as the previous one, thus saving some memory space.

So, what does that mean for your code? Well, look at these lines:



The String "arit" is a String literal so this is added to the String literal pool. As s3 and s4 both refer to this String literal, they both refer to the exact same object. The variable s2 refers to a String object with the contents "arit", but it didn't get this value from a String literal so it actually references a different object on the heap than s3 and s4.

Now, in the final 2 lines, we're using the "==" operator, which is checking reference values, not the contents of the String objects. If any two reference variables reference the same object, this operator will return true. Otherwise, it will return false.

As we already saw, s3 and s4 reference the same variable so this line returns true. The variable s2, on the other hand, refers to a different String object (even though it contains the same value) so the second comparison returns false.

For an added exercise, try this and see if you can explain the results:



SCJP Tipline, etc.
Sundar Murthi
Greenhorn

Joined: Dec 03, 2003
Posts: 4
Hi Corey
I understand the purpose of intern() method and get the result as true.
It creat a new string if it is not exists in a string pool. If exists its refference is returned.

Still i have the problem

look at the following one


it returns true

and
[CODE]
String str="Hello";
(str.replace('e','e')=="Hello")
[CODE]

it returns false

How it is??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: about string