The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Strings are very special objects

Posted by Ivan Porty on May 26, 2000 at 3:21 AM

> Why is the String class from Sun final?
> Wouldn't it be nice to create a class which could
> extend the String class? Or am I going about this OO
> thing the wrong way?

> Thanks, Dan

Good Day, Mr. Cranmer!

Strings are very special objects in Java, because they were initially,
by design, highly optimized. That's very important, because of String's
high importance. Everybody uses strings, in messages, JNI (const char*),
even like: String str = "string"; - that's automatic.
Also operator "+" was overloaded for Strings, but Java prohibits such
overloading! (Consider this: String str = "Hello"+","+"world"+17+true;)
If programmers would be allowed to extend Strings, that probably
would cause a lot of unpredictable behaivor, if everybody would use
separate type of Strings each other.
Plus, Strings are immutable. That means, once created, they can't be
changed, a that's clears understanding and programming.

So, that's a good design.

If you want to add some special functionality in Java's String,
consider following approach:
package com.cranmer.util;
public class StringUtil
{
public static String doSomeTrick(String s) { }
.....
}
and call that like: String my = StringUtil.doSomeTrick("Hi");

Ivan Porty.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us