# String.Empty Field

**URL:** <https://discuss.kotlinlang.org/t/string-empty-field/5335>\
**Category:** Language Design\
**Created:** [November 8, 2017, 9:18pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335 "2017-11-08T21:18:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasim-rangwala](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@kasim-rangwala](https://discuss.kotlinlang.org/u/kasim-rangwala)\
**Post date:** [November 8, 2017, 9:18pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/1 "2017-11-08T21:18:04Z")

</div>

Requesting built-in read only representation of the **empty string** similler to C# .NET has on [MSDN](https://msdn.microsoft.com/en-us/library/system.string.empty%28v=vs.110%29.aspx)  
for example:

```
public class String /* some code */ {
    companion object {
        @JvmField
        public val Empty: String = ""
    }
}

```

Uses:

```
String.Empty

```

---

<div class="post-metadata">

**Author:** ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)\
**Post date:** [November 8, 2017, 10:33pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/2 "2017-11-08T22:33:58Z")

</div>

I never understood the reason behind String.Empty and I’ve always refused to use it.  
Some people tell me it’s more readable, which is something I completely disagree with. It’s like saying `Math.plus(1, 3)` is more readable than `1 + 3`.

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [November 8, 2017, 11:02pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/3 "2017-11-08T23:02:06Z")

</div>

Why do you need it? Just use `""`. It’s like asking for Integer.ZERO constant.

---

<div class="post-metadata">

**Author:** ![nicky1038](https://avatars.discourse-cdn.com/v4/letter/n/8797f3/32.png) [@nicky1038](https://discuss.kotlinlang.org/u/nicky1038)\
**Post date:** [January 31, 2018, 12:44pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/4 "2018-01-31T12:44:33Z")

</div>

0 is zero itself but “” are not empty string itself but opening and closing quotes. In my opinion just two quotes as, for example, a function argument look a bit ugly, and it’s a case when unnamed constant should be taken out as a property.  
Of course, it’s a matter of taste, so why not to provide the choice to developers. Or at least to provide more usecases and explanations showing one variant is truly worse than another.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [January 31, 2018, 12:49pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/5 "2018-01-31T12:49:08Z")

</div>

The developers have the choice of declaring this constant in their own code if they find this more readable. No reason why this needs to be in the standard library.

---

<div class="post-metadata">

**Author:** ![kurt.seebauer](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kurt.seebauer](https://discuss.kotlinlang.org/u/kurt.seebauer)\
**Post date:** [January 31, 2018, 4:26pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/6 "2018-01-31T16:26:17Z")

</div>

I think Microsoft did it for the same reasons we have an Integer cache in the Integer class and there is the static constant Boolean.TRUE, that is for performance. There can be many empty String instances in a program if you don’t have String.EMPTY.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [January 31, 2018, 6:43pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/7 "2018-01-31T18:43:04Z")

</div>

While it’s true that there can be many instances of empty string in a program, those empty strings that come from literal `""` are usually interned by JVM. Therefore both `String.EMPTY` constant and any constant denoted by `""` literal will be represented with the same empty string instance at run time.

---

<div class="post-metadata">

**Author:** ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)\
**Post date:** [February 1, 2018, 11:35am UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/8 "2018-02-01T11:35:52Z")

</div>

In C# (and .NET generally), string literals are always interned to avoid duplication and so there can never be more than one instance of “”. If you look at the code for String.Empty it actual refers to “” in the background so, like @ilogico, I could never understand the reason why some people insisted on using String.Empty instead.

Similarly, in Kotlin, I can’t really see why:

```auto
if (s.isEmpty()) ....

```

is often considered superior to the more direct:

```auto
if (s == "") ...

```

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [February 1, 2018, 7:51pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/9 "2018-02-01T19:51:32Z")

</div>

> [@ilya.gorbunov](#):
>
> usually interned by JVM

(Side note: I was under the impression these were _always_ interned. Maybe there’s an exception for strings of unusual size, though.)

---

<div class="post-metadata">

**Author:** ![peteski](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/peteski/32/4915_2.png) [@peteski](https://discuss.kotlinlang.org/u/peteski)\
**Post date:** [March 16, 2018, 4:37pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/10 "2018-03-16T16:37:34Z")

</div>

For me, I think it’s more about making your intention clear.

If you use `""` how do we know you didn’t typo the magic string value? `String.Empty` is very explicit about what you’re trying to do.

e.g.

`return new someKindOfObject(myResult1, "");`

vs.

`return new someKindOfObject(myResult1, String.Empty);`

---

<div class="post-metadata">

**Author:** ![peteski](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/peteski/32/4915_2.png) [@peteski](https://discuss.kotlinlang.org/u/peteski)\
**Post date:** [March 16, 2018, 4:38pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/11 "2018-03-16T16:38:43Z")

</div>

I should add as part of the example:

`return new someKindOfObject(myResult1, " ");`  
`return new someKindOfObject(myResult1, "''");`  
`return new someKindOfObject(myResult1, "something was supposed to be here");`

---

<div class="post-metadata">

**Author:** ![mattmook](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mattmook/32/4637_2.png) [@mattmook](https://discuss.kotlinlang.org/u/mattmook)\
**Post date:** [March 16, 2018, 11:03pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/12 "2018-03-16T23:03:40Z")

</div>

Defining a string literal will usually result in the same String object as @ilya.gorbunov points out, however when creating new strings consider the following Java code:

```
String empty = "";
String alsoEmpty = "";
System.out.println(empty == alsoEmpty);
System.out.println(empty == new String(alsoEmpty));
System.out.println(empty == new StringBuilder().toString());
System.out.println(empty == new String());
System.out.println(empty == new String(new char[0]));

```

The result is `true, false, false, false, false`.

To me the nicety of Kotlins `isEmpty()` is the intent is obvious and, as @peteski points out a lot less likely to confuse `""` with `" "`. Also worth noting that the implementation of `isEmpty()` shows it checks the length, which is considered faster than comparing to an empty string.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 17, 2018, 12:04pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/13 "2018-03-17T12:04:15Z")

</div>

Your example is not that relevant though, because `==` in Kotlin is equivalent to javas `isEquals` method and as far as I can tell no one checks string equality using `===`. That being said, I like isEmpty() as it shows the intent.

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 2, 2019, 4:21am UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/14 "2019-08-02T04:21:21Z")

</div>

> [@kasim-rangwala](#):
>
> String.Empty

Is String merging an issue in Kotlin?

---

<div class="post-metadata">

**Author:** ![bessermt](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bessermt](https://discuss.kotlinlang.org/u/bessermt)\
**Post date:** [August 28, 2019, 10:30pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/15 "2019-08-28T22:30:23Z")

</div>

Still no comments on string merging? The question is still valid in my mind. Using a literal 0 as similar to “” is not valid to my mind. A “” may be stored in the app’s static data section and have a pointer to it while a literal 0 may be part of a CPU instruction set, so they are very different internally. If the language makes a promise to merge, then I’m good. However without that promise, I’m okay in that I can define my own constant if I want merging, but I have to do the work to make that happen. Can I get an answer please?

---

<div class="post-metadata">

**Author:** ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)\
**Post date:** [October 9, 2019, 1:03pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/16 "2019-10-09T13:03:08Z")

</div>

Here’s a case I’ve just got.

```
data class Person(val firstName: String, val middleName: String? = null, val lastName: String) {
    val fullName: String = "$firstName $middleName $lastName"
}

```

Here’s what I’m getting if middle name is _NULL_

```
John null Doe

```

What I have to do now is to add an emptyString constant which I can use in this string interpolation. It would be much simpler if language would offer this already.

I need to use _NULL_ default value here, because this is stored in database and is send over several SOAP and REST requests.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [October 9, 2019, 5:18pm UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/18 "2019-10-09T17:18:26Z")

</div>

I’m not sure what your question is? How is this relevant to this topic at hand?

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [October 10, 2019, 12:48am UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/19 "2019-10-10T00:48:26Z")

</div>

That’s what writing your own code is useful for. If you seriously actually truly really do need a `String.Empty` for whatever reason, you can just do this

`val String.Empty: String get() = ""`

It does exactly what you want and that’s the purpose of these types of things.

---

<div class="post-metadata">

**Author:** ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)\
**Post date:** [October 10, 2019, 5:05am UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/20 "2019-10-10T05:05:55Z")

</div>

I’ve resolved problem with _null_ as follow:

```
val fullName: String = "$firstName ${middleName ?: emptyString()} $lastName"

```

My point is that I have to define `emtpyString()` myself and I would prefer to have it defined in the language.

---

<div class="post-metadata">

**Author:** ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)\
**Post date:** [October 10, 2019, 5:10am UTC](https://discuss.kotlinlang.org/t/string-empty-field/5335/21 "2019-10-10T05:10:53Z")

</div>

I did exactly that, though I defined `emptyString()` function, but…  
Kotlin have `emptyList()`, `emptyMap()` etc, why not have `emptyString()` or something similar. For me that would be consistent and convenient.

[Next page](https://discuss.kotlinlang.org/t/string-empty-field/5335.md?page=2)
