6

I know that a C string abc would internally be abc\0 in C, is it the same case with Java?

Will
  • 539
  • 2
  • 8
  • 18

7 Answers7

11

No, it's not the same in Java. There's no null terminator. Java strings are objects, not points to arrays of characters. It maintains the length along with the Unicode characters, so there's no need to look for a null terminator.

You don't have to ask here: look at the source for String.java in the src.zip that ships with your JDK. Here's the start of it:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • It should be noted that UTF-16 is used (or a very slight variation of it?) for `value[]`. Not all Unicode characters can be stuffed in a single `char`. –  Dec 16 '11 at 03:30
  • Also, just because there is a `value[]` (and a `length`) *does not* mean that there is not an additional null-terminator (okay, there isn't in the canonical JVM). For instance, the [BCL -- aka .NET/C# -- has *both*](http://stackoverflow.com/questions/6293457/why-are-c-net-strings-length-prefixed-and-null-terminated), although the terminator is never exposed to managed code directly. (On the other hand, because of this, the BCL can't create a "view" of one string into another like `String.substring` does in Java.) –  Dec 16 '11 at 03:36
  • I thought the question was about Java. What's this about C#? – duffymo Dec 16 '11 at 09:59
8

Nope, C strings are an array of chars and thus there is no length associated with them. The side affect of this decision is that to determine a string's length, one must iterate through it to find the \0, which isn't as efficient of carrying the length around.

Java strings have a char array for their chars and carry an offset length and a string length. This means determining a string's length is rather efficient.

Source.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 4
    @buruzaemon: You know even chars deserve somewhere to sit, right? – alex Dec 16 '11 at 02:25
  • 1
    +1 for your humane view on `char`s and where they can rest a while, @alex – buruzaemon Dec 16 '11 at 02:27
  • 1
    @buruzaemon: [Java string](http://i.imgur.com/jXgCP.jpg) != [C string](http://i.imgur.com/jfq6d.jpg) – alex Dec 16 '11 at 02:37
  • This is great thanks for answering! But the way, I was looking at the source page, and One of the image showing all the internals of the string created in the program, what is that IDE? – Will Dec 17 '11 at 06:05
  • @Will: It looks like the debugger in in IntelliJ IDEA. – reve_etrange Dec 18 '11 at 00:23
2

String in C language is an array of char type where as in Java it a class and it represents collection of unicode chars.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

No. Null terminators are used in C because it's easier than passing around a pointer and a size. In Java, the size is always known and so a null terminator isn't necessary. Furthermore, there are no terminating characters in Java (putting in a \0 would be part of the literal string).

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
2

Java strings are not null-terminated like C strings. This is because Java stores strings' lengths. You can retrieve the length with String.length().

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
1

Class String is implemented in Java. See OpenJDK's implementation for an example.

The OpenJDK 7 String class carries an array of type char[] to hold the string itself, as well as an offset (telling the first used position in the char[]), the length of the string, and the hash code of the string.

It also has two static fields, a version ID for serialization purposes and an ObjectStreamField[] due to special casing with respect to the serialization output stream (in OpenJDK 7 at least).

reve_etrange
  • 2,561
  • 1
  • 22
  • 36
  • The `ObjectStreamField[] serialPersistentFields` field is a `static`. A `String` doesn't "contain" one. Same for the `serialVersionUID` field. In reality, a `String` instance has 4 fields. – Stephen C Dec 16 '11 at 02:51
  • I thought the offset field was abandoned before JDK7, probably because the implementers saw that if `million` is a million-character string, having `million.substr(10,3)` hold a reference to a million-character backing store could be very wasteful. IMHO, they should have designed `substr` to create a new backing store when using less than half of the old one, but take an offset into the old one otherwise. That would limit worst-case memory usage only 2x the optimal, while facilitating some commonly-helpful usage patterns. – supercat Feb 10 '15 at 23:28
0

As far as I know, in java String is stored as an object in the heap section as a sub-class of Object. There is no need to use '\0' to specify just characters or String.

Windfell
  • 38
  • 6