Hi All
I am very confusing that what is the difference between
String and new String.
for example
String s="dreaminCode";
String s1=new String("dreaminCode");
Otherthan what will happen in memory pool.
Please advice.
String Vs new String("test")
Page 1 of 16 Replies - 4208 Views - Last Post: 16 February 2009 - 06:09 PM
Replies To: String Vs new String("test")
#2
Re: String Vs new String("test")
Posted 16 February 2009 - 11:25 AM
There is no difference if we're disregarding memory.
#3
Re: String Vs new String("test")
Posted 16 February 2009 - 11:27 AM
To my knowledge, there isn't a difference between them. One may be allocated dynamically since new is being used. But other than that, there's no difference as far as I know.
Hope this helps!
Hope this helps!
#4
Re: String Vs new String("test")
Posted 16 February 2009 - 11:32 AM
Locke, on 16 Feb, 2009 - 10:27 AM, said:
To my knowledge, there isn't a difference between them. One may be allocated dynamically since new is being used. But other than that, there's no difference as far as I know.
Hope this helps!
Hope this helps!
thanks for ur quick deed.
I read an article it mentioned that
String s="dreamincode"; - its create one String object and one reference object.
String s=new String("dreamincode"); -it's create TWO Object and one referenece object.
Is it corect. Please advice
#5
Re: String Vs new String("test")
Posted 16 February 2009 - 03:03 PM
Prefer the first the the second. Ultimately, s and s1 will contain the same value. However, to do
You have actually allocated the string "dreaminCode" twice. Once in your very declaration of it and then again in the new String you passed the parameter too.
Correct. Sort of. I don't know if "creating a reference object" is quite right. Rather, you have a reference to the object. If an object has no references, the garbage collector will kill it.
String s1=new String("dreaminCode");
You have actually allocated the string "dreaminCode" twice. Once in your very declaration of it and then again in the new String you passed the parameter too.
vibinvictoria, on 16 Feb, 2009 - 12:32 PM, said:
String s="dreamincode"; - its create one String object and one reference object.
String s=new String("dreamincode"); -it's create TWO Object and one referenece object.
Is it corect. Please advice
String s=new String("dreamincode"); -it's create TWO Object and one referenece object.
Is it corect. Please advice
Correct. Sort of. I don't know if "creating a reference object" is quite right. Rather, you have a reference to the object. If an object has no references, the garbage collector will kill it.
This post has been edited by baavgai: 16 February 2009 - 03:06 PM
#6
Re: String Vs new String("test")
Posted 16 February 2009 - 04:30 PM
vibinvictoria, on 16 Feb, 2009 - 10:19 AM, said:
Hi All
I am very confusing that what is the difference between
String and new String.
for example
String s="dreaminCode";
String s1=new String("dreaminCode");
Otherthan what will happen in memory pool.
Please advice.
I am very confusing that what is the difference between
String and new String.
for example
String s="dreaminCode";
String s1=new String("dreaminCode");
Otherthan what will happen in memory pool.
Please advice.
The 2 statements are equivalent
It is just because the class String is used so many times that Java engineering decides to create a short hand so you are not forced to type every time = new String("....");
Baavgay is almost right...
actually the compiler translate
String x = "DreamInCode";
by
String x = new String("DreamInCode");
the actual object "DreamInCode" is only instantiated once x pointing to it in both case
if you do:
String in = "In";
String str = "Dream" + in + "Code";
the actual code generated is:
String in = new String("In");
StringBufffer tmp = new StringBuffer("Dream").append(in).apend("Code");
String str = tmp.toString();
#7
Re: String Vs new String("test")
Posted 16 February 2009 - 06:09 PM
pbl, on 16 Feb, 2009 - 05:30 PM, said:
Baavgay is almost right...
actually the compiler translate
String x = "DreamInCode";
by
String x = new String("DreamInCode");
the actual object "DreamInCode" is only instantiated once x pointing to it in both case
actually the compiler translate
String x = "DreamInCode";
by
String x = new String("DreamInCode");
the actual object "DreamInCode" is only instantiated once x pointing to it in both case
I agree, to a point. You've still suffered from that "new String" call. Rather than debate the fine points of it, I'll offer words of wisdom from a far more experienced Java programmer than I. From "Effective Java", a book I highly recommend, Joshua Bloch has this to say:
Quote
As an extreme example of what not to do, consider this statement:
String s = new String("silly"); // DON'T DO THIS!
The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("silly") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly.
The improved version is simply the following:
String s = "No longer silly";
This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to contain the same string literal [The Java Language Specification, Second Edition, 3.10.5].
String s = new String("silly"); // DON'T DO THIS!
The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("silly") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly.
The improved version is simply the following:
String s = "No longer silly";
This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to contain the same string literal [The Java Language Specification, Second Edition, 3.10.5].
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|