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.)
I would recommend you to read through the Java tutorial documentation hosted on Oracle's website whenever you are in doubt about anything related to Java.
You can get a clear understanding of the concepts by going through the following tutorials:
Characters
Strings
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)
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.
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.
Well,
char
(or its wrapper classCharacter
) means a single character, i.e. you can't write'ab'
whereasString
is a text consisting of a number of characters and you can think of a string a an array of characters (in fact theString
class has a memberchar[] value
).You could work with plain
char
arrays but that's quite tedious and thus theString
class is there to provide a convenient way for working with texts.In char only single character (should be in single quotes) can be used it could be alphabet or any number or even special character. below are the related examples for char
In String complete line can be used (should be in double quotes). below are the related examples for String
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.