简体   繁体   中英

Difference between “char” and “String” in Java

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.)

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 .

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 .

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

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 .

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:

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

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

char 包含单个字符,而 string 包含多个字符。

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

Well, char (or its wrapper class Character ) means a single character, ie 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.

一个字符只包含一个字母,一个字符串有一个完整的单词或多个单词,在末尾自动插入一个转义序列,告诉编译器字符串已经在这里结束。 (0)

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 = "%%";

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

String s="a";

String s1="aaaa";

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM