대부분의 경우, 1개의 글자 값을 사용하는 경우 primitive char
type을 사용할 것입니다.
예를 들면:
Most of the time, if you are using a single character value, you will use the primitive char
type. For example:
char ch = 'a';
// Unicode for uppercase Greek omega character
char uniChar = '\u03A9';
// an array of chars
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
하지만 객체로 char를 사용할 때가 있는데 - 예를 들면 객체가 예상될 때 메소드 argument로.
자바 프로그래밍 언어는, 이 목적을 위하여 Character
객체 안에서 char
를 "포장하는" 클래스 wrapper 를 제공합니다.
타입 Character
의 객체는 단일 필드를 포함하고 있는데, 그 타입은 char
입니다.
이 클래스 Character는 또한 글자들 조작을 위한 많은 유용한 클래스(즉, static) 메소드들을 제공합니다.
Character
생성자로 Character
객체를 생성할 수 있습니다:
There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" the char
in a Character
object for this purpose. An object of type Character
contains a single field, whose type is char
. This Character class also offers a number of useful class (i.e., static) methods for manipulating characters.
You can create a Character
object with the Character
constructor:
Character ch = new Character('a');
The Java compiler will also create a Character
object for you under some circumstances. For example, if you pass a primitive char
into a method that expects an object, the compiler automatically converts the char
to a Character
for you. This feature is called autoboxing—or unboxing, if the conversion goes the other way. For more information on autoboxing and unboxing, see Autoboxing and Unboxing.
Note: The
Character
class is immutable, so that once it is created, a Character
object cannot be changed.The following table lists some of the most useful methods in the Character
class, but is not exhaustive. For a complete listing of all methods in this class (there are more than 50), refer to the java.lang.Character API specification.
Method | Description |
---|---|
boolean isLetter(char ch) | Determines whether the specified char value is a letter or a digit, respectively. |
boolean isWhitespace(char ch) | Determines whether the specified char value is white space. |
boolean isUpperCase(char ch) | Determines whether the specified char value is uppercase or lowercase, respectively. |
char toUpperCase(char ch) | Returns the uppercase or lowercase form of the specified char value. |
toString(char ch) | Returns a String object representing the specified character value — that is, a one-character string. |
Escape Sequences
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Escape Sequence | Description |
---|---|
\t | Insert a tab in the text at this point. |
\b | Insert a backspace in the text at this point. |
\n | Insert a newline in the text at this point. |
\r | Insert a carriage return in the text at this point. |
\f | Insert a formfeed in the text at this point. |
\' | Insert a single quote character in the text at this point. |
\" | Insert a double quote character in the text at this point. |
\\ | Insert a backslash character in the text at this point. |
When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence
She said "Hello!" to me.
you would write
System.out.println("She said \"Hello!\" to me.");
'Java 배우기' 카테고리의 다른 글
Converting Between Numbers and Strings 숫자와 문자 사이의 변환 (0) | 2016.01.05 |
---|---|
Strings 문자열 (0) | 2016.01.05 |
Questions and Exercises: Numbers 질문과 연습: 숫자 (0) | 2016.01.05 |
Summary of Numbers 숫자의 요약 (0) | 2016.01.05 |
Beyond Basic Arithmetic 기본 산술을 넘어서 (0) | 2016.01.05 |
댓글