• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

about string

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shilp..

The new one is String object or a String litral???
 
Sundar Murthi
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my understanding it is an object..

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

Can someone confirm this
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic