Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the underlying container in a Java String?

Tags:

java

string

Is it just a char array?

like image 549
Aillyn Avatar asked Sep 15 '10 05:09

Aillyn


People also ask

What is a container in Java?

Containers are the interface between a component and the low-level, platform-specific functionality that supports the component. Before it can be executed, a web, enterprise bean, or application client component must be assembled into a Java EE module and deployed into its container.

How are strings stored internally in Java?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

What are the strings in Java?

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.

What does string [] mean in Java?

String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.


1 Answers

Yes, plus some meta-data, like a start and end index (because that char array can be shared across strings, for example, when you create substrings).

Looking at the source for java.lang.String, you see the following instance 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
like image 130
Thilo Avatar answered Sep 22 '22 12:09

Thilo