10

I am a student who has just shifted from C++ to Java.

In Java what could be the main reason for defining separate data types for Strings and Char arrays? What is the difference between the two?

Since I have only studied C++, up till now I was under the impression that they are the same thing. Please clarify if possible.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
iluvthee07
  • 491
  • 1
  • 5
  • 16

6 Answers6

17

String is immutable. Char array is not. A string is implemented with a char array underneath but every time you try to modify it (like with concatenation, replace etc.) it gives you a new String object.

So, String behaves as a constant Char array but comes with certain syntactic sugar that also makes them very easier to use. For example, the addition + operator has been overloaded as a string concatenation operator as well.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Thanks for the response. I have another doubt - Can I use the String object like a char[] ? As in if I want to access a random element at any point or to store a char at any index is it possible to do so in a String? – iluvthee07 Aug 20 '13 at 15:23
  • 1
    Ravi deserves his answer to be accepted. To expand a bit on his answer, one of the most common data structures you work with when programming are `String`s because of the inherent need of computer systems to interact with human beings. The designers of Java recognized this and wrapped char[] into a special Object called String to make interacting with Strings more convenient – StormeHawke Aug 20 '13 at 15:23
  • 1
    @iluvthee07 - yes. You'll want `string.charAt(index)` for the first case. For the second, you'd need to either play around with substrings, or use `string.toCharArray()` and work with that. For more details on what you can do with `String`s, I'd recommend taking a look at the `String` javadoc here: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html – StormeHawke Aug 20 '13 at 15:27
3

In Java, String is a basic system class that essentially wraps a char[]. There are several reasons why, for most uses, having a full class is preferable to directly handling arrays:

  • Strings are immutable; once you have a reference to some String, you know it's never going to change.
  • Strings provide useful methods that a bare array couldn't, such as length(), and have clearly-defined comparison semantics.
  • You never have to deal with string termination yourself.
  • Java has a special exception for the rule of "no operator overloading" to support string concatenation (with +).

Essentially, it's good OO practice to use a class to collect the desired behavior and the data structures in the same place, and String wraps up an array of characters with the useful operations that you want to perform on a string.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Thanks for the response. I have another doubt - Can I use the String object like a char[] ? As in if I want to access a random element at any point or to store a char at any index is it possible to do so in a String? – iluvthee07 Aug 20 '13 at 15:19
  • [The Javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) is worth reading. `String` is immutable, meaning it can't be modified once created. `charAt` will provide read-only access to individual characters inside the backing array. `StringBuilder` (and the older, slower `StringBuffer`) provides read-write semantics around a `char[]`, which can be converted to a read-only `String` once you're done building it. – chrylis -cautiouslyoptimistic- Aug 20 '13 at 15:22
2

String is a class in Java and offers you methods and is also an Object.

A String-object is also immutable.

Internal the value is a char-array.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
2

There is a semantic difference. Just because data is stored the same way, this doesn't mean it's the same thing. Dates and Amounts may also have the same internal representation (long for a timestamp or fixed point amount of cash), but they're not the same. The char array could as well mean a 16-bit image.

In object orientation, it's good practice to model objects based on what they are and can, and not by how they internally store their data. This allows you to encapsulate the data (and restrict or control (observer support) access with getters/setters, or even make the internal representation immutable or poolable), and provide appropriate methods for your objects.

Peter Walser
  • 15,208
  • 4
  • 51
  • 78
1

String is immutable in Java and stored in the String pool. Once it is created it stays in the pool until garbage collected.Since, String is immutable , the logging password is as readable string.It has greater risk of producing the memory dump to find the password.

where as Char array is created in heap and you can override with some dummy values.

  • Not all Strings are stored in the pool. Only String literals, Strings created with the `intern()` method and a few other cases are stored in the pool. Also, Strings which are stored in the pool are not garbage collected. – Radiodef Aug 27 '18 at 16:57
0

The advantage to using the string object is all the methods available to it. For example:

stringExample1.equals(stringExample2);

String stringExample3 = stringExample1.replace(substring1, substring2);