String and Character Array in Java

10,657

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
10,657
iluvthee07
Author by

iluvthee07

Just wanna learn a lot.... :)

Updated on July 22, 2022

Comments

  • iluvthee07
    iluvthee07 almost 2 years

    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.

  • iluvthee07
    iluvthee07 over 10 years
    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?
  • chrylis -cautiouslyoptimistic-
    chrylis -cautiouslyoptimistic- over 10 years
    The Javadoc 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.
  • iluvthee07
    iluvthee07 over 10 years
    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?
  • StormeHawke
    StormeHawke over 10 years
    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 Strings 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
    StormeHawke over 10 years
    @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 Strings, I'd recommend taking a look at the String javadoc here: docs.oracle.com/javase/6/docs/api/java/lang/String.html
  • Radiodef
    Radiodef over 5 years
    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.