63

I am reading a book for Java that I am trying to learn, and I have a question. I can't understand what is the difference between the variable type char and String. For example, there is a difference between int and short, the bytes at the memory and the area of numbers that they have.

But what is the difference between char and String? except that char use (') and "String" (").

PS: It is my first "real" programming language. (At school I learned a fake-language for the purpose of the programming lesson.)

MrAP
  • 223
  • 4
  • 18
Android Girl
  • 2,074
  • 3
  • 22
  • 29

14 Answers14

136

char is one character. String is zero or more characters.

char is a primitive type. String is a class.

char c = 'a';
String s = "Hi!";

Note the single quotes for char, and double quotes for String.

Dan Ciborowski - MSFT
  • 6,807
  • 10
  • 53
  • 88
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    A character constant has an equivalent integer value, whereas a single-character string constant does not have an equivalent integer value. – Mohit Saluja Jun 24 '18 at 16:46
24

char means single character. In java it is UTF-16 character. String can be thought as an array of chars.

So, imagine "Android" string. It consists of 'A', 'n', 'd', 'r', 'o', 'i' and again 'd' characters.

char is a primitive type in java and String is a class, which encapsulates array of chars.

user207421
  • 305,947
  • 44
  • 307
  • 483
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
7

In layman's term, char is a letter, while String is a collection of letter (or a word). The distinction of ' and " is important, as 'Test' is illegal in Java.

char is a primitive type, String is a class

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
2

char is a primitive type, and it can hold a single character.

String is instead a reference type, thus a full-blown object. It can hold any number of characters (internally, String objects save them in a char array).

Primitive types in Java have advantages in term of speed and memory footprint. But they are not real objects, so there are some possibilities you lose using them. They cannot be used as Generic type parameters, they could not have methods or fields, and so on.

However, every Java primitive type has a corresponding full-blown object, and the conversion between them is done automagically by the compiler (this is called autoboxing).

You can for example do:

int i=12;
Integer l=i;

The compiler takes care of converting the int to a Integer.

user207421
  • 305,947
  • 44
  • 307
  • 483
Andrea Parodi
  • 5,534
  • 27
  • 46
  • You can also compare a *char* whith *int* : in ASCII table number give you the symbol to print. – cl-r May 03 '12 at 12:09
1

char have any but one character (letters, numbers,...)

char example = 'x';

string can have zero characters or as many as you want

String example = "Here you can have anything";
Martin54
  • 1,349
  • 2
  • 13
  • 34
0

A char holds a single character, while a string holds lots of characters.

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
Matthias
  • 3,458
  • 4
  • 27
  • 46
0

Char is a single alphabet where as String is a sequence of characters. Char is primitive datatype where as String is a class.

Tanuj Verma
  • 388
  • 2
  • 7
0

char is a primitive type, and it can hold a single character. String is instead a reference type, thus a full-blown object.

Sandeep Khot
  • 301
  • 3
  • 5
-1

Well, char (or its wrapper class Character) means a single character, i.e. you can't write 'ab' whereas String is a text consisting of a number of characters and you can think of a string a an array of characters (in fact the String class has a member char[] value).

You could work with plain char arrays but that's quite tedious and thus the String class is there to provide a convenient way for working with texts.

Thomas
  • 87,414
  • 12
  • 119
  • 157
-1

A char simply contains a single alphabet and a string has a full word or number of words woth having a escape sequence inserted in the end automatically to tell the compiler that string has been ended here.(0)

-1

A char consists of a single character and should be specified in single quotes. It can contain an alphabetic, numerical or even special character. below are a few examples:

char a = '4';
char b = '$';
char c = 'B';

A String defines a line can be used which is specified in double quotes. Below are a few examples:

String a = "Hello World";
String b = "1234";
String c = "%%";
Svend Hansen
  • 3,277
  • 3
  • 31
  • 50
-2

In string we can store multiple char. e.g. char ch='a';

String s="a";

String s1="aaaa";

  • What is wrong with this answer? Why this answer is negatively voted by 2 people? Can anybody explain? Is this information wrong? –  Mar 11 '22 at 12:52
-3

In terms of ASCII values you can say char is a single ASCII value ranging from 0-255. Whereas String is a collection of ASCII values. Try this code to learn better.

        char c='a';
        String s="a b c d e f g hijkl";
        int i=c;
        System.out.println(i);
        for(int count=0;count<s.length();count++){
            int temp=s.charAt(count);
            System.out.print(temp+" ");
        }

The output will be:

97

97 32 98 32 99 32 100 32 101 32 102 32 103 32 104 105 106 107 108

Since 97 is the ASCII value for small 'a'. 32 is the ASCII value for space. Hope this helps in-depth understanding of the concept.

  • No, you can't say that. A Java `char` is 16 bits, and a `String` is a collection of `char`. Java uses UTF-16 internally, not ASCII. This is rather basic. – user207421 Mar 13 '17 at 07:44
-5

A character is anything that you can type such as letters,digits,punctuations and spaces. Strings appears in variables.i.e they are text items in perls. A character consist of 16bits. While the lenght of a string is unlimited.

  • 'they are text items in perls' is either incorrect in a Java context, or irrelevant, or meaningless. I can't decide which. – user207421 Oct 04 '21 at 08:59