- Java - Inner classes
- Java - Exceptions
- Java - Files and I/O
- Java - Methods
- Java - Regular Expressions
- Java - Date & Time
- Java - Arrays
- Java - Strings
- Java - Characters
- Java - Numbers
- Java - Decision Making
- Java - Loop Control
- Java - Basic Operators
- Java - Modifier Types
- Java - Variable Types
- Java - Basic Datatypes
- Java - Constructors
- Java - Object & Classes
- Java - Basic Syntax
- Java - Environment Setup
- Java - Overview
- Java - Home
Java Object Oriented
- Java - Packages
- Java - Interfaces
- Java - Encapsulation
- Java - Abstraction
- Java - Polymorphism
- Java - Overriding
- Java - Inheritance
Java Advanced
- Java - Documentation
- Java - Applet Basics
- Java - Multithreading
- Java - Sending Email
- Java - Networking
- Java - Serialization
- Java - Generics
- Java - Collections
- Java - Data Structures
Java Useful Resources
- Java - Examples
- Java - Discussion
- Java - Useful Resources
- Java - Quick Guide
- Java - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java - Character Class
Normally, when we work with characters, we use primitive data types char.
Example
char ch = a ; // Unicode for uppercase Greek omega character char uniChar = u039A ; // an array of chars char[] charArray ={ a , b , c , d , e };
However in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char.
The Character class 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.
Example
// Here following primitive char a // is boxed into the Character object ch Character ch = a ; // Here primitive x is boxed for method test, // return is unboxed to char c char c = test( x );
Escape Sequences
A character preceded by a backslash () is an escape sequence and has a special meaning to the compiler.
The newpne character ( ) has been used frequently in this tutorial in System.out.println() statements to advance to the next pne after the string is printed.
Following table shows the Java escape sequences −
Escape Sequence | Description |
---|---|
Inserts a tab in the text at this point. | |
Inserts a backspace in the text at this point. | |
Inserts a newpne in the text at this point. | |
Inserts a carriage return in the text at this point. | |
f | Inserts a form feed in the text at this point. |
Inserts a single quote character in the text at this point. | |
" | Inserts a double quote character in the text at this point. |
\ | Inserts a backslash character in the text at this point. |
When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.
Example
If you want to put quotes within quotes, you must use the escape sequence, ", on the interior quotes −
pubpc class Test { pubpc static void main(String args[]) { System.out.println("She said "Hello!" to me."); } }
This will produce the following result −
Output
She said "Hello!" to me.
Character Methods
Following is the pst of the important instance methods that all the subclasses of the Character class implement −
Sr.No. | Method & Description |
---|---|
1 | Determines whether the specified char value is a letter. |
2 | Determines whether the specified char value is a digit. |
3 | Determines whether the specified char value is white space. |
4 | Determines whether the specified char value is uppercase. |
5 | Determines whether the specified char value is lowercase. |
6 | Returns the uppercase form of the specified char value. |
7 | Returns the lowercase form of the specified char value. |
8 | Returns a String object representing the specified character value that is, a one-character string. |
For a complete pst of methods, please refer to the java.lang.Character API specification.
What is Next?
In the next section, we will be going through the String class in Java. You will be learning how to declare and use Strings efficiently as well as some of the important methods in the String class.
Advertisements