-2

We cannot create an empty char like this char ch = '';, and this is understood because there is no empty character in ASCII. There are NULL characters and white characters but those are not empty characters so char ch = '\0'; is possible but not char ch = '';.

Now, we can create a empty string in Java, and string is nothing but an array of characters, so it makes me wonder how an empty string would be stored?


EDIT:

I agree with everybody that in Java String is not array of characters but in the end to store a string there would be character coming into picture otherwise I will wonder how strings are stored, I mean in the end each character in the string will be stored as a BYTE, now how a empty string will be stored?

pjj
  • 2,015
  • 1
  • 17
  • 42

3 Answers3

5

A Java String is not an array of characters (nor is it a single char, which is an integral primitive type). It is an Object type, and includes a length.

JLS-10.9. An Array of Characters is Not a String says (in part)

In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).

A String object is immutable, that is, its contents never change, while an array of char has mutable elements.

I mean in the end each character in the string will be stored as a BYTE, now how a empty string will be stored?

No, each character is 2 bytes. The Java Tutorials: Primitive Data Types says

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

In the case of an empty String there aren't any characters; and an empty array has length 0.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I know that but under the hoods there is an character array. – pjj May 08 '16 at 22:45
  • 2
    C strings are not Java strings... C strings are not Java strings. I think a lot of people don't realize that languages are very different under the hood, even when they have the same data structures. Even integers aren't always the same. The memory implementations are very different and Java strings abstract a little bit further than char arrays/C strings. Java is also a little bit more open to syntactic sugar. (I have no idea who decided to defensively downvote your answer, nor why) – Aaron3468 May 08 '16 at 22:46
  • 1
    @pjj A `char` is not a `char[]`. A `char` is a primitive integral type. A `char[]` is an `Object` type. – Elliott Frisch May 08 '16 at 22:46
4

String is certainly backed by a char[] (a field known as value), but that does not under any circumstance imply that a String is exactly equivalent to a char[]. They're two different objects.

Now, with that out of the way, let's reason about what we're expecting with a String of length zero. This is how we determine length:

/**
 * Returns the length of this string.
 * The length is equal to the number of <a href="Character.html#unicode">Unicode
 * code units</a> in the string.
 *
 * @return  the length of the sequence of characters represented by this
 *          object.
 */
public int length() {
    return value.length;
}

The big thing here to note is that if we're creating an empty string, the length of our backing array has to be zero. So, the way that an empty String is created is by providing an empty value array at instantiation.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks, you are making most sense to me and clearing my doubt. But still wondering about your this statement - "*So, the way that an empty String is created is by providing us an empty value array.*" .. I am not getting this entirely, could you please elaborate more .. – pjj May 08 '16 at 22:55
  • @pjj: I've slightly reworded that portion. – Makoto May 08 '16 at 22:56
  • 2
    @Aaron3468: [No, really, it's a `char[]`.](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/lang/String.java?av=f#114) – Makoto May 08 '16 at 23:06
  • @Makoto But there is no guarantee that it will remain a `char[]` (or that a different JVM doesn't use another type). – Elliott Frisch May 09 '16 at 00:08
  • @ElliottFrisch: We can cross that bridge when we find another widely-used implementation of String on another JVM that does this sort of thing. In specific terms, for the official Java language, its implementation is a `String`. I also don't see it changing anytime soon, but that's a fair point. – Makoto May 09 '16 at 00:09
0

As a String object that refers to an empty char[]. Yes, arrays can have a length of 0, because they are objects that know the number of elements, and not just a fancy pointer like in C or C++.

For instance, you can do:

char[] array = new char[0];
assert array != null;
assert array.length == 0;
assert "".equals(new String(array));
meriton
  • 68,356
  • 14
  • 108
  • 175