I am learning Java this year as part of the AP Computer Science curriculum, and while I was reading about "Char" and "String" I could not understand why one would bother to use "Char" and only be able to store one character rather than just use "String" and be able to store much more than that. In short what's the point of "char" if it can only store a single character?

8

Best Answer


People are mentioning memory concerns, which are valid, but I don't think that's a very important reason 99% of the time. An important reason is that the Java compiler will tell you if you make a mistake so you don't have to figure it out on your own.

For example, if you only want 1 character for a variable, you can use a char to store the value and now nobody can put anything else in there without it being an error. If you used a String instead, there could be two characters in the String even though you intended that to never be possible. In fact, there could be 0 characters in the String which would be just as bad. Also, all your code that uses the String will have to say "get the first character of the String" where it could simply say, "give me the character".

An analogy (which may not make sense to you yet, unfortunately) would be, "Why would I say a Person has a Name when I could say a Person has a List of Names?" The same reasons apply. If you only want a Person to have one Name, then giving him a list of Names adds a lot maintenance overhead.

You could consider this analogy:

You need one apple. Would you prefer to have one apple in your hand, or a big box that could contain more apples, but only needs to contain the one?

The char primitive datatype is easier to work with than the String class in situations where you only need one character. It's also a lot less overhead, because the String class has a lot of extra methods and information that it needs to store to be efficient at handling string with multiple characters. Using the String class when you only need one character is basically overkill. If you want to read from a variable of both types to get the character, this is the code that would do that:

// initialization of the variableschar character = 'a';String string = "a";// writing a method that returns the characterchar getChar(){return character; // simple!}char getCharFromString(){return string.charAt(0); // complicated, could fail if there is no character}

If this code looks complicated, you can ignore it. The conclusion is that using String when you only need one character is overcomplicating things.

Basically, the String class is used when you need more than one character. You could also just create an array of chars, but then you would not have the useful methods of the String class, such as the .equals() and .length() methods.

Strings are objects. Objects always go on the dynamic storage. Storing one-character string would require at least a dozen of bytes.

chars (not Chars) are primitives. They take fixed amount of space (2 bytes). In situations when you need to process a single character, creating one-character string is a waste of resources. Moreover, when you expect to see a single character, using strings would require validation that the data passed in has exactly one character. This would be unacceptable in situations when you must be extremely fast, such as character-based input and output.

To summarize, you need a char because of

  • Memory footprint - a char is smaller than a String of one character
  • Speed of processing - creating objects carries an overhead
  • Program's maintainability - Knowing the type makes it easier for you and for the readers of your code to know what kind of data is expected to be stored in a char variable.

char take up less memory for times when you really only need one character. There are also multiple other applications for using a single character.

char is a primitive datatype while string is an object which comes at greater overhead.

A string is also made up of char, so there's that too.

Because the char takes up less memory!

Also the char is stored in memory and NOT as a reference value so theoretically its faster to access the char (You'll understand that more later)

***Note: I once had this same thought when I first started programming about why use an int when you can use a long and not have to worry about large numbers. This tells me you're on your way to be a great programmer! :)

char is a primitive type while String is a true Object. In some cases where performance is a concern it's conceivable that you would only want to use primitives.

Another case in which you would want to use char is when you're writing Java 1.0 and you're tasked with creating the String class!

public final class Stringimplements java.io.Serializable, Comparable<String>, CharSequence {/** The value is used for character storage. */private final char value[];

Everything in java can be reduced to primitive types. You can write any program with primitive types. So you need some kind of minimalist way of storing text. A char is also really just a byte, that is interpreted as a character.

Also if you want to loop though all characters in a string you would do:

char[] chArr = str.toCharArray();for(int i = 0 ; i < chArr.length ; i++){//do something with chArr[i];}

This would be much more awkward trying to substring out an exact character from the String.

Lot of answers here already. While the memory concerns are valid, you have to realize there are times when you want to directly manipulate characters. The word ladder game

where you try to turn one word into another by changing one character at a time is an example I had to do in a programming class. Having a char type lets you manipulate a singe character at a time. It also lets you assign an int to a char that maps to your local character set.

You can do thing like char c = 97; and that will print out as a. You can do things like increment a character from 97 to 122 to print out all lowercase characters. Sometimes this actually is useful.