0

As I've been learning about a few different languages, I've seen that java is the only one with a "char" datatype. For example, charAt() for javascript returns a string whereas in java the method returns a character. Is there a reason for a "character" datatype and why don't other languages use them?

3
  • Java inherited the concept of having a char primitive from C/C++ like languages. Commented Jun 12, 2013 at 5:15
  • 11
    Many other languages do have a char data type. You should perhaps be asking yourself rather why Javascript doesn't. Commented Jun 12, 2013 at 5:15
  • JavaScript have similar notion of "char" as other languages with String.charCodeAt(i) returning Number type (which is only other basic type supported by JavaScript), but it is very close to most languages notion of char as 8/16bit integer... Commented Jun 12, 2013 at 5:29

3 Answers 3

1

James Gosling told me

Most of the time, if you are using a single character value, you will use the primitive char type.

For example:

char ch = 'a'; 
Sign up to request clarification or add additional context in comments.

Comments

0

I would like to say and ask myself

for char a='a'; & String a="a";

How much memory does a char variable takes ?

and

How much memory does a string literal or string object takes ?

If you figure this out , Then this answers your question

2 Comments

@TheNewIdiot me neither :P
Char is more memory efficient, but what is a use case where storing characters individually rather than a long string is going to unplug a bottleneck? EG if I store the genome as a long string, is that saving me overhead over storing three billion characters separately? I get the idea, but don't see the application
0

Char holds the single character but String can be single character or it contains multiple characters. char consume lesser memory compared to String, beacuse String is a class and char is a primitive data type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.