Characters in java
By: aathishankaran
Characters in java
In java, the data type used to store characters is char. However, C/C++ programmers beware: char in java is not the same as char in C or C++. In C/C++, char is an integer type that is 8 bits wide. This is not the case in java. Instead, java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, katakana, Hangul, and many more. For this purpose, it requires 16 bits. Thus, in java char is a set of characters known as ASCII still ranges from 0 to 255. Since java is designed to allow applets to be written for worldwide use, it makes sense that it would use Unicode to represent characters. Of course, the use of Unicode is somewhat inefficient for languages such as English, German, Spanish, or French, whose characters can easily be contained within 8 bits. But such is the price that must be paid for global portability.
Here is a program that demonstrates char variables:
// Demonstrate char data type.
Class CharDemo {
Public static void main {
Char ch1, ch2;
ch1 = 88;
ch2 = ‘y’;
System.out.print ( “ ch1 and ch2: “);
System.out.println ( ch1 + “ †+ ch2) ;
}
}
This program displays the following output:
Ch1 and ch2: x y
Notice that ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X. As mentioned, the ASCII character set occupies the first 127 values in the Unicode character set. For this reason, all the “old tricks†that you have used with characters in the past will work in java, too.
Even though chars are not integers, in many cases you can operate on them as if they were integers. This allows you to add two characters together, or to increment the value of a character variable. For example, consider the following program.
//char variables behave like integers
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = ‘X’;
System.out.println
( “ ch1 contains “ + ch1);
ch1++;
System.out.println
( “ch1 is now †+ ch1);
}
}
The output generated by this program is shown here:
ch1
contains x
ch1 is now y
In the program, ch1 is first given the value X. Next, ch1 is incremented. This results in ch1 containing Y, the next character in the ASCII (and Unicode) sequence.
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java