Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an EMPTY_STRING constant, why not?

Status
Not open for further replies.

dbleyl

Programmer
Mar 26, 2001
117
US
Sometimes you want to distinguish between an empty string and null.

Is there a constant for empty string? I know that "" is pooled, but some tools complain about non-externalized string literals (and yeah, I'm that nit-picky too).

I've seen alot of libraries that implement the same thing:

public static final java.lang.String EMPTY_STRING

I don't like this because it should be in java.lang somewhere.

How do you folks handle an empty string?


 
String s = "";

if (s.length() == 0) {

}

There is no "String.EMPTY_STRING" because is makes no sense - a String is an Object - the Object is either null, or not null. If it is not null, then it's attibutes (ie its length etc) may or not be an empty *string* or not ...

OO design means you can overcome this by implementing your own extension of String :

public class MyString extends String {
public static String EMPTY_STRING = "";
}
 
Hmmm, it makes sense to me, and I think you've made my point. I know that you can initialize String s with the literal String "". I know you can check for emptiness with s.length()==0. But if you don't want string literals in your code, how do you initialize a String to a non-null empty value without creating a bunch of duplicate empty strings? (Each call to String() creates a new char[0]). How do you initialize Strings in default constructors with String fields? If it's part of the invariant that a field be either some value or blank?

I know I can subclass it, but I don't want to, for the same reason that I don't want
a global constant or singleton. I also don't want to extend a class to a.) add static members only and b.) create a special instance of the parent class that is creatable by the parent - that's not object-oriented.

Maybe an oo way of doing what you suggest is something like:

public class EmptyStringSingleton extends String {
private static EmptyStringSingleton instance = "";
public static EmptyStringSingleton getInstance() {
return instance;
}
}

But that's super ugly. EmptyStringSingleton.getInstance();

What I need is a non-literal constant instance of a class.
And I accept the fact that it's like Collections.EMPTY_LIST, in that sometimes you just need a list with nothing in it, and you don't want to create one everytime.

Yes, maybe it's extreme that I don't want string literals in my code to the extent that I don't even want "". I suppose if you don't consider 0 a magic number, then I shouldn't consider "" a true string literal.

If a String is null the toString() value is "null", which isn't what I'm looking for. I guess what you are suggesting is that I use "" and not consider it a String literal.

But thanks for the input. Does it make no sense to you because you see an empty string as a single attribute that any String can possess?
 
Why not just use
Code:
String();
There will ever only be one such string added to the pool of unique strings privately maintained by the String class.
PS : If a String is null you cannot call the toString() method on it.
 
sedj and dbleyl : Does your code compile ?

The "String" class if final and cannot be extended.
 
HA. Forgot that.

You could always copy the sun source code and create your own *String* object !!!!
 
Doh! Don't know what I was thinking on that one - NPE for sure. And I guess you could just have a private field of
"" and delegate all your calls to that field:

public String toString() {
return EMPTY_STRING.toString(); //ha I kid. Same as //EMPTY_STRING
}

But String() will not work:

System.out.println(new String() == new String());
is false. Because any call to the default constructor
results in a new array being created of type char and size 0.
 
Of course, new String() == new String() returns false,
like new Integer(2) == new Integer(2) will,
and any other comparison.

Code:
public class Test {

  public Test() {
    System.out.println("new Integer(2) == new Integer(2) : " + (new Integer(2) == new Integer(2)) );
    System.out.println("new Integer(2)).equals(new Integer(2) : " + (new Integer(2)).equals(new Integer(2)) );
    System.out.println("new String() == new String() : " + (new String() == new String()) );
    System.out.println("new String()).equals(new String()) : " + (new String()).equals(new String()) );
    System.out.println("new String(ABC) == new String(ABC) : " + (new String("ABC") == new String("ABC")) );
  }

  static public void main(String[] args) {
    Test test = new Test();
  }

}
The output :
Code:
new Integer(2) == new Integer(2) : false
new Integer(2)).equals(new Integer(2) : true
new String() == new String() : false
new String()).equals(new String()) : true
new String(ABC) == new String(ABC) : false
But like I said :

There will ever only be one such string added to the pool of unique strings privately maintained by the String class.
 
I disagree.
System.out.println(new String() == new String());
System.out.println(new String() == "");
System.out.println(new String().intern() == "");
System.out.println(new String().intern() == new String().intern());

false
false
true
true

This site does a decent explanation:

If it's not constant or literal it's not pooled unless
you intern it.
 
Sorry dbleyl,

You are absolutely right !!!

I suppose you have to choose :
or - ""
or - new String().intern()
or - use the equals method to check for eqaulity
or - ...

holo

 
And I'm am sorry for starting a thread on such a stupid question :)

Some tools complain about literals, even "", so if you don't want warnings, and are too lazy to change the rulesets, you
end up refactoring the "" out of the code with resourceBundle or something equally strange.

I think I will just change the ruleset to exclude "" and really think about whether there is a need to use "" in the first place. Maybe check out jarkarta commons lang to see if they have one I can use :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top