1

What's the point of using char in Java?

I know that char can only hold one character, while String can hold sentences, so what's the point of using char if it can hold only one character? String can also hold one character.

I've heard that for example, short can use much less numbers than int, however its advantage is that it uses much less memory, is there something similar with char and String?

Vegeta
  • 334
  • 2
  • 4
  • 12
  • A String is a sequence of char. So that's make a good point to use them, like when you use `equals` to compare String values. – user2336315 Jan 02 '14 at 10:18
  • You answered your own question really - for optimizing code its best to use data types applicable to that particular piece of data - it would be overkill and a waste of memory to use Strings all the time when a char type would do – Katana24 Jan 02 '14 at 10:21
  • Thanks for your answers, I think I understand now, and probably need some more programming experience too :) – Vegeta Jan 02 '14 at 10:28
  • another thing.. until recently, you weren't allowed to use "Strings with Switch statements", char could be used. and I also have a feeling that the fact "C/C++" and other languages having char as a data type also plays a major role in "keeping it" in java. – TheLostMind Jan 02 '14 at 10:29
  • possible duplicate of [Difference between "Char" and "String" at Java](http://stackoverflow.com/questions/10430043/difference-between-char-and-string-at-java) – Lakshmi Jan 02 '14 at 10:46
  • I've seen that other question thread, it explained to me the difference, however I still didn't understand why char in some cases is better than String, now I really understand that :) – Vegeta Jan 02 '14 at 10:57

6 Answers6

3

Just like there is an int and an int[] a String is a sequence of chars. There are cases where you only need a single character and not a whole sequence and in those cases you use char.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

The char data type cannot hold "one character", it can hold a 16 bit unsigned integer that can do double work as a glyph in the first 65k set of unicode glyphs. It's used to form strings (as a char[]) and is used when reading live user input, which is one letter every time the user presses a key. It's a data type that we unfortunately inherited from a period when unicode was still young, and these days the char datatype is too small to hold all "single letters" we've decided are single letters, so char will fail on some things that you would imagine is supposed to be a letter, and things will break, which is why in Strings sometimes the number of "real letters" and the number of chars/bytes used to encode those letters are different.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

In addition to @Ivaylo Strandjev's answer, it is worth mentioning that char type is mutable whereas Strings are immutable. This may be important in some security issues (you don't want passwords/hashes in your memory for ever).

Mateusz
  • 3,038
  • 4
  • 27
  • 41
0

If you need just one character in your program it's better to use Char and not String. You could use String but using Char is one of best practice and help to improve performance of your code.

Pracede
  • 4,226
  • 16
  • 65
  • 110
0

Char is a primitive data type in java that stores just one character whereas String basically is an object and can store a sequence of characters. The use of them mainly lies in what context they are used and varies for the need of the application.

Also char[] is mutable wheres String is not meaning any changes in char array is reflected in the original instance created whereas for String a new object with the modified changes is created.

Also quotoing @TheLostMind "until recently, you weren't allowed to use "Strings with Switch statements", char could be used. and I also have a feeling that the fact "C/C++" and other languages having char as a data type also plays a major role in "keeping it" in java"

You can get a better idea by going through the following:

Characters

Strings

also look at this question Difference between "char" and "String" in Java

Community
  • 1
  • 1
Lakshmi
  • 2,204
  • 3
  • 29
  • 49
0

char is a primitive data type while String is a class. If you need to store just 1 character, using a char is always better as it would consume lesser memory also using a String to store 1 character is just unrequired as every String also has a lot of methods that are kind of irrelevant for a single character. So, if it is just 1 character that you need to store, using a char is far better choice for the following reasons.

  1. String would need to create an object to store the char, while char is a primitive type and no such objects are created.
  2. Lesser memory and faster processing.

Also, char type can be used with arithmetic operators like + and -.

So, use char when you only need to store a character and don't need any String methods and use String when there is more than 1 character to deal with.

Read here for more details

Adarsh
  • 3,613
  • 2
  • 21
  • 37