• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Class  RSS feed

 
Meenaakshi Bala Shanmugam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does String class have thirteen Constructors?
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not? Have a look at what they do...
 
Meenaakshi Bala Shanmugam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the reason for it..
 
Christian Dillinger
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDoc explains what each of the constructor does. As Strings are immutable you have to provide contructors for different purposes.

There are screw drivers in different ten sizes/forms inside your toolbox, why?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenaakshi Bala Shanmugam wrote:Why does String class have thirteen Constructors?

Why does ResultSet have 140-odd methods? Or List 40-odd? Or Calendar 70-odd?

Because somebody thought they were needed.

You also have to understand that classes develop over time, and String is nearly 20 years old now. It's highly likely that it didn't start out life with 13 constructors, but it's been decided that it needs them now.

Sometimes, it can lead to "specification bloat" (all the others I mentioned being good examples, IMO), but I don't think that's particularly true in the case of String.

HIH

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In good old java 1.1 days there were 11 of them.
Version 1.4 had also 11 ones including two deprecated ones.

Version 1.5 had 13 ones including two deprecated ones, because StringBuilder and code points were introduced into the API
and they found their way into String constructors as parameters.

Version 1.6 introduced two new constructors with Charsets.

Version 1.7 has no more ones.

This growth rate fortunately does not make (well the most of) us dizzy.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:In good old java 1.1 days there were 11 of them.

Ivan. Please DontWriteLongLines. It makes the thread very hard to read.

Any chance you could break up some of those enormous comment lines? Just use the 'Edit' button.

Thanks

Winston

[Edit] Done (This time).
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Any chance you could break up some of those enormous comment lines


My bad!

I know it is a weak excuse but I try to defend myself hinting on to have picked the comments from the API docs.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should use /*…*/ for multiple line comments, not // several times.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> thirteen Constructors?

Ouch! In some cultures, 13 is considered as an unlucky number.

Fortunately the String class passed this point as discussed above.

(Other cultures have 4 as the unlucky number.)

So a class should better not have 4 or 13 constructors in order to be on the safe side.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should use /*…*/ for multiple line comments, not // several times.


Ohhhhh, comment wars!!!

I use either construct depending upon context.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: . . . Ohhhhh, comment wars!!!

I use either construct depending upon context.
I take that as meaning whether you have several single‑line line end comments or one multiple‑line comment
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only the shadow knows!
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ohhhhh, comment wars!!!


For a while there was the hope that a "the lucky numbers for the constructors of a class" war was being unleashed...
But not every dream comes true.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!