The Artima Developer Community
Sponsored Link

Java Answers Forum
String manipulations again

4 replies on 1 page. Most recent reply: Jan 7, 2004 4:34 PM by Jonathon Brozny

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
suneetha

Posts: 4
Nickname: venussun
Registered: Nov, 2003

String manipulations again Posted: Jan 7, 2004 8:04 AM
Reply to this message Reply
Advertisement
hi
Thank u for ur incredible reply.That has clared all my doubts in my head.But here r some more question abt string manipulations....

if("String".trim()=="String".trim())
its true.
but
if("String ".trim()=="String ".trim())
its false
even
if("String ".trim()=="String")
its fase either
and again
if("String".replace(n,g)=="String".replace(n,g))
its false again
y so..., i dont know.Please help me.Please reply these questions

Thanking you
Suneetha


Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: String manipulations again Posted: Jan 7, 2004 8:08 AM
Reply to this message Reply
I think I just answered this in the first post you made.

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: String manipulations again Posted: Jan 7, 2004 10:19 AM
Reply to this message Reply
From the java api

"If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Returns:
A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space."

and from a post by Matt Gerrans
"But of course the only strings in the literal pool would be the strings you literally typed (well, unless you are using a code geneator). This is likely to be a small amount of memory compared to what the computer has, unless you are one hell of a typist.

By the way, one of the advantages of this literal pool is that it saves memory. In this scenario:

String s = new String("Go placidly amid the noise and haste...");
String x = new String("Go placidly amid the noise and haste...");


You have two distinct and separate copies of the same string gobling up defenselsss little memory bits by the hundreds. While in this one:

String s = "Go placidly amid the noise and haste...";
// ... Several gigabytes of painstaking hand-typed code later...
String x = "Go placidly amid the noise and haste...";


Both s and x refer to the same String, leaving vast expanses of bored memory circuits with nothing to do."



That explains why you are getting your true and false when you are. Both "String" are refering to the same String in memory so...

if("String".trim()=="String".trim())
its true. //because a reference to the same String is being returned

if("String ".trim()=="String ".trim())
its false //same String in memory but because 2 new Strings are created and references to each are returned it is false

if("String ".trim()=="String")
its false //would be false even without trim because they are different Strings in memory
if("String".replace(n,g)=="String".replace(n,g))
its false again //again same String in memory but because 2 new Strings are created and references to each are returned it is false

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: String manipulations again Posted: Jan 7, 2004 4:08 PM
Reply to this message Reply
Mr. Brozny

I disagree with the post you have quoted in your last post, which is below for your reference.

***********************************************************
String s = new String("Go placidly amid the noise and haste...");
String x = new String("Go placidly amid the noise and haste...");


You have two distinct and separate copies of the same string gobling up defenselsss little memory bits by the hundreds. While in this one:

String s = "Go placidly amid the noise and haste...";
// ... Several gigabytes of painstaking hand-typed code later...
String x = "Go placidly amid the noise and haste...";


Both s and x refer to the same String, leaving vast expanses of bored memory circuits with nothing to do."
***********************************************************

If you write:
String x = new String ( 10kb big string literal....);
String y = new String ( same 10kb big string literal goes here);
 


I agree that it would be better to write:
String x = 10kb big string literal....;
String y = 10kb big string literal....;
or 
y = x;
 


However, even if you go by first pair of statements, it doesn't create two copies of '10kb big string literal'. That is, it is not using at least 20kb of memory. Internally, it still shares the same set of characters ans there will be only one copy of '10kb big string literal'.

What we are having is: memory allocation for two string objects (which are different that saying two copies of same string literals) because of two uses of "new" operators. One String object will takes memory of two-machine words for its header and space for its instance variables.

I am not trying to defend the use of two new operators. I will always go for second option as:

String x = "Adasdasd";
String y = x;

My point is the post you had quoted was confusing and misleading (in very sophisticated way) to the beginners giving an impression that in such a cases Java will keep two true copies of the string literals using double the memory. Anyway, the person you have quoted is notorious for his verbosity on this forum!!!!

Thanks
Kishori

Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: String manipulations again Posted: Jan 7, 2004 4:34 PM
Reply to this message Reply
Kishori. Nicely put. I really didn't put too much thought about the post I quoted other then it stated what I was trying to say. That the 2 String literals with the same value would have the same reference value.

Thanks
Jonathon

Flat View: This topic has 4 replies on 1 page
Topic: hellllllllppppppp urgent Previous Topic   Next Topic Topic: JAXB - unmarshalling / marshalling xml comments

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use