简体   繁体   English

Java中的char和Character有什么区别?

[英]What is the difference between char and Character in Java?

我需要知道Java中的char和Character之间的区别是什么,因为当我创建一个java程序时,char工作而Character没有工作。

char is a primitive type that represents a single 16 bit Unicode character while Character is a wrapper class that allows us to use char primitive concept in OOP-kind of way. char是一种原始类型,表示单个16位Unicode字符,而Character是一个包装类,允许我们以OOP方式使用char原始概念。

Example for char, char的例子,

char ch = 'a';

Example of Character, 人物的例子,

Character.toUpperCase(ch);

It converts 'a' to 'A' 它将'a'转换为'A'

From the JavaDoc : 来自JavaDoc

The Character class wraps a value of the primitive type char in an object. Character类在对象中包装基本类型char的值。 An object of type Character contains a single field whose type is char. Character类型的对象包含一个类型为char的字段。 In addition, this class provides several methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa. 此外,该类提供了几种方法来确定字符的类别(小写字母,数字等)以及将字符从大写转换为小写,反之亦然。

Character information is based on the Unicode Standard, version 6.0.0. 字符信息基于Unicode标准版本6.0.0。

So, char is a primitive type while Character is a class. 因此,char是一种原始类型,而Character是一个类。 You can use the Character to wrap char from static methods like Character.toUpperCase(char c) to use in a more "OOP way". 您可以使用Character从像Character.toUpperCase(char c)这样的静态方法中包装char,以便以更“OOP方式”使用。

I imagine in your program there was an 'OOP' mistake(like init of a Character) rather than char vs Character mistake. 我想在你的程序中有一个'OOP'错误(比如一个字符的init)而不是char vs Character错误。

Character is an Object - thus contains a number of static methods eg valueOf(char),toUpperCase() Character是一个Object - 因此包含许多静态方法,例如valueOf(char),toUpperCase()

where char is a primitive data-type 其中char是原始数据类型

char is a primitive type and Character is a class that acts as a wrapper for char. char是一种原始类型,Character是一个充当char的包装器的类。

The point of the Character class is so you can apply a range of methods to your char if needed. Character类的要点是,如果需要,您可以将一系列方法应用于char。

More information here http://docs.oracle.com/javase/tutorial/java/data/characters.html 更多信息请访问http://docs.oracle.com/javase/tutorial/java/data/characters.html

暂无
暂无

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

相关问题 Java中的字符流和字节流有什么区别?C中的字符串和字节与字节有什么区别? - What's the difference Between Character Stream and Byte Stream in Java and Char vs Byte in C? java中的(char)temp和Character.toChars(temp)和String.valueOf(Character.toChars(temp))有什么区别 - what is the difference between (char)temp and Character.toChars(temp) and String.valueOf(Character.toChars(temp)) in java Character.isWhitespace(char) 和 Character.isSpaceChar(char) 有什么区别 - What is the difference between Character.isWhitespace(char) and Character.isSpaceChar(char) Java 中的 Character.isAlphabetic 和 Character.isLetter 有什么区别? - What is the difference between Character.isAlphabetic and Character.isLetter in Java? Java中“char”和“String”的区别 - Difference between “char” and “String” in Java Java中Character(char)的大小是多少? - what is the size of Character(char) in Java? Java中&和&&有什么区别? - What is the difference between & and && in Java? Java中的> =和=>有什么区别? - What is the difference between >= and => in Java? Java和C#中的char之间的区别((char)-1的特殊问题) - Difference between char in Java and C# (particular problem with (char)-1) String.lastIndexOf()char和具有单个字符的字符串之间有区别吗? - String.lastIndexOf() is there a difference between a char and a string with a single character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM