-1

String class in JDK uses private final char value[]; to store constants. I wonder why it does not use private final String value.

Is there any reason related to performance or memory issues?

2
  • 2
    Are you suggesting class String { private final String value; ... }? Let that sink for a minute... Commented Oct 23, 2013 at 9:39
  • This is probably what you're looking for: stackoverflow.com/a/15368773/1974797 Commented Oct 23, 2013 at 9:43

5 Answers 5

0

String class in jdk used private final char value[] ..

Here used means 'used to define the String class'. The writer of string class needed a container to store the value of the string, & while (s)he is still in process of writing String class, how can they use it?

e.g. While assembling a desktop, you don't start with a desktop itself - you start with some basic components like a CPU, screen, mouse, keyboard etc..

Sign up to request clarification or add additional context in comments.

Comments

0

The String class clearly can't use itself to store the value. (How would that work?)

It has to use something else to represent the string. That something else in an array of char.

Comments

0

String class in JDK is used to declare the class : it appears obvious that the String class can't use itself to store a String.

Or maybe you think that String is a Java primitive ? Well, it's not !

Comments

0

By the definition of String Class itself

The String class represents character strings.

So,if you not storing characters, Where does a String came :) ?

2 Comments

i can store the constants in a String object and use charAt(index) to iterate over it
i can store the constants in a String object, again where does the String Object came ? From characters right ?
0

Think the String class as a wrapper for char[].

So the class uses char[] to store the effective value... it Can't use a String (as you said) because String doesn't exist without a place to store the characters.

In other words the chars are used for character storage as a primitive type and you can't do in a different way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.