- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Characters
In Java, char is the data type used to store characters. However, C/C++ programmers beware that the char in Java is not the same as char in C/C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent characters.
Java uses Unicode
Unicode defines a entirely international character set that can represent all of the characters found in all human languages. It's a unification of dozens of character sets, such as Latin, Arabic, Greek, Hebrew, Cyrillic, and many more.
At the time of Java's creation, Unicode required 16 bits. Thus, in Java char is a 16-bit type. The range of a char is from 0 to 65,536 range. There are no negative chars.
Standard set of Characters
The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the covered 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
Since Java is designed to allow programs to be written for worldwide use, this makes the sense that it would use Unicode to represent the characters. Of course, the use of Unicode is slightly inefficient for languages such as English, Spanish, German, or French, whose characters can easily be controlled within 8 bits. But such is the price that must be paid for global portability.
Java Characters Example
Below is a program that demonstrates the char variables :
/* Java Program Example - Java Characters * This program demonstrates char data type */ public class JavaProgram { public static void main(String args[]) { char ch1, ch2; ch1 = 88; // code for X ch2 = 'Y'; System.out.print("ch1 : " + ch1 + "\nch2 : " + ch2); } }
When the above Java program is compile and run, it will produce the following output:
Notice here that variable ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X. As discussed, the ASCII character set occupies the first 127 values in the Unicode character set. For this cause, all the older tricks that you may have used with characters in other languages will work in Java, also.
Even though char is designed to hold the Unicode characters, it can also be used as an integer type on which you can perform the arithmetic operations. For instance, you can add two characters together, or increment the value of a character variable. Consider the below example program :
Example
/* Java Program Example - Java Characters * char variables behaves like integers */ public class JavaProgram { public static void main(String args[]) { char ch1; ch1 = 'X'; System.out.println("ch1 contains " +ch1); ch1++; // increments ch1 System.out.println("ch1 is now " + ch1); } }
When the above Java program is compile and run, it will produce the following output:
In the above program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1 holding Y, the next character in the ASCII (and Unicode) sequence.
Escape Sequences in Java
A character preceded by the backslash (\) is an escape sequence and has particular meaning to the compiler.
As you see, the newline character (\n) has been used frequently in System.out.println() statements to advance to the next line after the string is printed.
Look at the table given here, which shows the Java's escape sequences:
Escape Sequence | Description |
---|---|
\n | Inserts a newline |
\b | Inserts a backspace |
\f | Inserts a form feed |
\r | Inserts a carriage return |
\' | Inserts a single quote character |
\" | Inserts a double quote character |
\\ | Inserts a backslash character |
\t | Inserts a tab |
When an escape sequence is encountered in a print statement, the compiler interprets it consequently. Let's look at the following example:
If you want to put quotes within quotes then you must use the escape sequence, \", on the interior quotes:
/* Java Program Example - Java Characters */ class JavaProgram { public static void main(String args[]) { System.out.println("He said \"Hello!\" to me."); } }
It will produce the following result:
More Example
Here are some examples related to characters, that you can go for.
- Check Character is Alphabet or Not in Java
- Check Character is Vowel or Not in Java
- Count Character in String in Java
- Uppercase Character to Lowercase in Java
- Lowercase Character to Uppercase in Java
« Previous Tutorial Next Tutorial »