where can i find low level implementation of string in java

hello,

I want to see how JVM implements immutable string which , unlike c, is not terminated by a null character.

so while calculating length of a string ,how does jvm know that the end of string has reached when it does not have any marker to indicate end of the string.

please reply with ur valuable suggestions.

[340 byte] By [guptaravi2ka] at [2007-10-3 1:23:08]
# 1

In c a String is an array of char terminated with the NUL character, but in java an array of char is not a String. Internally though the String class contains a private instance variable of type char array and this is used for character storage. It also maintains a count of the number of characters in the String. When you call length() on a String this count is simply returned.

YoGeea at 2007-7-14 18:20:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

thank you for your reply but..

U did not understand the problem completely.

the question is- how does jvm implement length() method when it doesn't know the end of a string ?. in order to calculate length() it has to know this fact.

does it make sense ? nitify me if u need further clarification on the problem.

guptaravi2ka at 2007-7-14 18:20:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

To calculate length it would have indeed to know it - but it is not calculating it. It just stores length explicitly inside String object.

Please take a look at src.zip file in JDK distribution and look for the file java/lang/String.java. There you can see the implementation details. Currently, the String defines following fields

/** 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

'count' variable is the length you are looking for. To understand the idea of 'offset', please look into substring implementation.

abiesa at 2007-7-14 18:20:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

hello abies,

i did my homework before posting this question here..String.java says nothing abouthow does it calculate the variable count.

I need to know where does that count variable come from?

where in JDK src or in JVM is the assignment statement... for count

there should be a statement like

count = // something ..

hope u got the point i tried to make.

looking for ur reply....

I

guptaravi2ka at 2007-7-14 18:20:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

Well, if you already look into String.java code, you can search for

this.count = ...

public String() {

[...]

count = 0;

[...]

}

public String(String original) {

int size = original.count;

[...]

}

public String(char value[]) {

int size = value.length;

[...]

this.count = size;

}

String(int offset, int count, char value[]) {

[...]

this.count = count;

}

As you can see, there is a lot of possibilities. It can come from String you are copying, it can be 0 for empty strings, it might be the length of the char array you are copying, it might be passed directly by a substring routine in String.

Please note that count is final variable, so you don't have to look anywhere else the a constructor to see how it is initialized.

abiesa at 2007-7-14 18:20:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...