6 Replies - 4208 Views - Last Post: 16 February 2009 - 06:09 PM Rate Topic: -----

#1 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 16-July 08

String Vs new String("test")

Posted 16 February 2009 - 11:19 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: String Vs new String("test")

#2 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,033
  • Joined: 14-September 07

Re: String Vs new String("test")

Posted 16 February 2009 - 11:25 AM

There is no difference if we're disregarding memory.
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 516
  • View blog
  • Posts: 5,588
  • Joined: 20-March 08

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! :)
Was This Post Helpful? 0
  • +
  • -

#4 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 16-July 08

Re: String Vs new String("test")

Posted 16 February 2009 - 11:32 AM

View PostLocke, 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! :)



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
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4886
  • View blog
  • Posts: 11,280
  • Joined: 16-October 07

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
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.


View Postvibinvictoria, 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


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

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,148
  • Joined: 06-March 08

Re: String Vs new String("test")

Posted 16 February 2009 - 04:30 PM

View Postvibinvictoria, 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.

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();
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4886
  • View blog
  • Posts: 11,280
  • Joined: 16-October 07

Re: String Vs new String("test")

Posted 16 February 2009 - 06:09 PM

View Postpbl, 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


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].

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1