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

new String("text") vs. "text"

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking at some code and I have come across several instances of code that looks like this:



Isn't that the same as writing:



Am I missing something subtle here?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, they're not exactly the same, as the new String(String) creates a second copy of the string, which is usually unnecessary and pointless. I think that most often when you see code like this, it's simply because the coder didn't know any better.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jim (and everyone else),

oops, if i get you right, you say that



creates one more object then



does.

i'd like to understand this completely. in the first case, a string "foo" is created just to be passed into the String constructor, to create a new String "foo"?

is there any case why i want to use the first notation? does anyone have an example for it?

many thanks,
jan
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I always use

String str = "what ever";


String in Java is slightly different from other Object classes.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is there any case why i want to use the first notation?


Only in obscure cases such as:
- asking a question on a Java forum
- demonstrating how pointless such an invocation is

By extending the same reasoning, you can learn that invoking any constructor is "pointless" (for an extended definition of the term), but this is a side issue.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"foo" and new String("foo") are not same as Jim said.

when you invoke bar("foo"), new string object will not be created.

the following example might make it easy to understand this..

String s1 = "foo";
String s2 = "foo";
here, s1 == s2 is true pls note that we are comparing the references

and

String s3 = new String ("foo");
String s4 = new String ("foo");

this time, s3 == s4 is false pls note that we are comparing the references



so when u create s2 no new object is created,
but in case of s3 and s4, new objects are creted.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To use the technical term, all literal Strings in a .java file are interned (short for internalized I think)

code like this :
String greeting = "hello";
String alsoGreeting = "hello";
String newGreeting = new String("hello");

effectively gets turned into something like this

String internedString = "hello";
String greeting = internedString;
String alsoGreeting = internedString;
String newGreeting = new String(internedString);

Since Strings are immutable (contents can't be altered) it is perfectly safe to do. That explains why
greeting == alsoGreeting (they are the same object)
and
greeting != newGreeting, but greeting.equals(newGreeting); (two different objects, same value)


Bottom line:
someMethod(new String("This is an example."));

is completely unnecessary, and a little bit wasteful.
someMethod("This is an example.");
is almost the same, but will use the same interned String if there is another String "This is an example." somewhere else in the list.

Does that make any sense?
 
reply
    Bookmark Topic Watch Topic
  • New Topic