• 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

String's no-arg Constructor

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just discovered that the String class provides a (explicit) no-arg constructor. Since String objects are immuteable, I fail to see the reason for it.
String strvar = new String(); //compiles
The above declaration creates a zero length string equivalent to ...
String strvar2 = new String("");
Just curious if anybody knows why that is.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<CODE>strvar = new String()</CODE> initializes a newly created String object so that it represents an empty character sequence (JDK 1.3.1 API). So what's the perceived problem?
Not that it has much use but you could do
strvar += "Tony is smart";
and later (though not my favorite):
strvar = "No he is not!"

Seriously, nothing to loose sleep about.
Good luck!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic