I want to know, if a String
is a collection. I have read around but am still quite confused.

- 7,399
- 5
- 58
- 106

- 553
- 1
- 4
- 21
-
4No it's not. A string is an object of Class String. It has as one of its instance members an array of chars, but it has a lot more as well. – Taylor Nov 27 '13 at 16:51
7 Answers
Strings are immutable objects representing a character sequence (CharSequence
is one of the interfaces implemented by String).
Main difference to char arrays and collections of chars: String cannot be modified, it's not possible (ignoring reflection) to add/remove/replace characters.
Internally they are represented by a char array with an offset and length (this allows to create lightweight substrings, using the same char arrays).
Example: ['F','o','o','H','e','l','l','o',' ','W','o','r','l','d'], offset=3, count=5 = "Hello"
.

- 15,208
- 4
- 51
- 78
strings are object of class String
and it's not a Collection
as well. It is implemented on top of char array. You can see it in the below code snippet:
public final class String implements
java.io.Serializable, Comparable<String>, CharSequence
{
private final char value[];

- 14,004
- 9
- 70
- 110
No, it's not a collection in that it does not implement the Collection<E>
interface.
Conceptually, it is an immutable sequence of characters (and implements the CharSequence
interface).
Internally, the String
class is likely to use an array of chars, although I am pretty sure the spec does not mandate that.

- 486,780
- 108
- 951
- 1,012
No, A string is an object of class String
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class....
A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.

- 168,305
- 31
- 280
- 331
No it's not an Array or a Collection. However there is a convenient method if you need a char array and you have a String - namely String.toCharArray(); you could use it like this
// Prints the String "Hello, World!" as a char[].
System.out.println(Arrays.toString("Hello, World!".toCharArray()));

- 198,278
- 20
- 158
- 249
String
Class is just a class which allows you to create objects of strings. Like all classes it makes easy the use of these objects using its owns methods.
An array of char is an string, but its methods are relative to arrays or the elements of the array.
So for example if you want to look for the position of a carácter in an string of a class String then you use a method called IndexOf. But if you want to find a character in an array of char, then you would have to do it manually using loops.

- 435
- 7
- 21

- 648
- 7
- 20
No, a String
is an immutable string of characters, and the class extends Object
directly and does not implement any of the Collection
interfaces. Really, that's the basic answer.
However, there's a lot going on under the covers in the runtime--there's a whole collection of cached strings held by the JVM and in its most primitive representation, yeah, a String is basically an array of characters (meaning it's a bunch of memory addresses pointing to representations of characters). Still, once you go below the definition of String
as it's defined as a class, you can keep going until you get to the point that you're just talking about organized combinations of ones and zeroes.
I'm guessing here, but I imagine you posed the question because you're studying this and an instructor said something about a String
being an array of characters. While technically correct, that's really confusing because the two concepts exists at completely different levels of abstraction.

- 2,167
- 4
- 22
- 43
-
no, I'm writing some software, and suddenly get freaked out that I don't actually know how it is set up. – juju Nov 27 '13 at 17:09
-
Ha. I know the feeling. I actually like the fact that you're asking the question just because you want to know how it works :-) – Ickster Nov 27 '13 at 17:20