Contechtions
Contechtions

Reputation: 85

Why does java have a primitive char?

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?

Upvotes: 0

Views: 498

Answers (3)

Anil Nivargi
Anil Nivargi

Reputation: 1717

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.

Upvotes: 0

anshulkatta
anshulkatta

Reputation: 2064

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

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

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'; 

Upvotes: 1

Related Questions