简体   繁体   English

Java中“char”和“String”的区别

[英]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.我正在阅读一本我正在尝试学习的 Java 书籍,我有一个问题。 I can't understand what is the difference between the variable type char and String .我不明白变量类型charString之间有什么区别。 For example, there is a difference between int and short , the bytes at the memory and the area of numbers that they have.例如, intshort之间存在差异,内存中的字节以及它们拥有的数字区域。

But what is the difference between char and String ?但是charString之间有什么区别? except that char use (') and "String" (").除了char使用 (') 和 "String" (")。

PS: It is my first "real" programming language. PS:这是我的第一个“真正的”编程语言。 (At school I learned a fake-language for the purpose of the programming lesson.) (在学校我为了编程课学习了一种伪语言。)

char is one character. char是一个字符。 String is zero or more characters. String是零个或多个字符。

char is a primitive type. char是一种原始类型。 String is a class. String是一个类。

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

Note the single quotes for char , and double quotes for String .注意char的单引号和String双引号。

char means single character. char表示单个字符。 In java it is UTF-16 character.在java中它是UTF-16字符。 String can be thought as an array of chars. String可以被认为是一个字符数组。

So, imagine "Android" string.所以,想象一下“Android”字符串。 It consists of 'A', 'n', 'd', 'r', 'o', 'i' and again 'd' characters.它由'A', 'n', 'd', 'r', 'o', 'i''d'字符组成。

char is a primitive type in java and String is a class, which encapsulates array of chars . char是 java 中的原始类型, String是一个类,它封装了chars数组。

In layman's term, char is a letter, while String is a collection of letter (or a word).通俗地说, char是一个字母,而String是一个字母(或一个单词)的集合。 The distinction of ' and " is important, as 'Test' is illegal in Java. '"的区别很重要,因为'Test'在 Java 中是非法的。

char is a primitive type, String is a class char是原始类型, String是一个类

char is a primitive type, and it can hold a single character. char是一种原始类型,它可以容纳单个字符。

String is instead a reference type, thus a full-blown object. String是一个引用类型,因此是一个成熟的对象。 It can hold any number of characters (internally, String objects save them in a char array).它可以容纳任意数量的字符(在内部, String对象将它们保存在一个char数组中)。

Primitive types in Java have advantages in term of speed and memory footprint. Java 中的原始类型在速度和内存占用方面具有优势。 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).然而,每个 Java 原始类型都有一个相应的成熟对象,它们之间的转换由编译器自动完成(这称为自动装箱)。

You can for example do:例如,您可以执行以下操作:

int i=12;
Integer l=i;

The compiler takes care of converting the int to a Integer .编译器负责将int转换为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.当您对与 Java 相关的任何内容有疑问时,我建议您通读 Oracle 网站上托管的 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 是单个字母表,而 String 是一个字符序列。 Char is primitive datatype where as String is a class. Char 是原始数据类型,而 String 是一个类。

char have any but one character (letters, numbers,...) char 只有一个字符(字母、数字……)

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. char 是一种原始类型,它可以容纳单个字符。 String is instead a reference type, thus a full-blown object. String 是一个引用类型,因此是一个成熟的对象。

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 ).好吧, char (或其包装类Character )表示单个字符,即您不能写'ab'String是由多个字符组成的文本,您可以将字符串视为字符数组(实际上String类有一个成员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.您可以使用普通的char数组,但这很乏味,因此String类提供了一种处理文本的便捷方法。

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

A char consists of a single character and should be specified in single quotes. char 由单个字符组成,应该用单引号指定。 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';例如char ch='a';

String s="a";

String s1="aaaa";字符串 s1="aaaa";

In terms of ASCII values you can say char is a single ASCII value ranging from 0-255.就 ASCII 值而言,您可以说 char 是一个范围从 0-255 的单个 ASCII 值。 Whereas String is a collection of ASCII values.而 String 是 ASCII 值的集合。 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

97 32 98 32 99 32 100 32 101 32 102 32 103 32 104 105 106 107 108 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'.因为 97 是小“a”的 ASCII 值。 32 is the ASCII value for space. 32 是空格的 ASCII 值。 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.字符串出现在变量中。即它们是 perls 中的文本项。 A character consist of 16bits.一个字符由 16 位组成。 While the lenght of a string is unlimited.而字符串的长度是无限的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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