Saturday, October 26, 2013

Character Class

Insurance,Loans,Mortgage,Attorney,Credit,Lawyer,Donate,Degree,Hosting,Claim,Conference Call,Trading,Software,Recovery,Transfer,Gas/Electricity,Classes,Rehab,Treatment, Cord Blood,

In java, The data types used to store characters is char. Java uses Unicode to represent character,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 ,Hebrew and many more,For this purpose , it requires 16 bits , thus in java char is a 16 bit type,the range of a char is 0 to 65,536. There are bi negative chars Since java is designed to allow applets to be written for worldwide use,it makes sense that is would use Unicode to represent characters

Example 
class UsingChar {

    public static void main(String[] args) {
        char c1,c2;
        c1 = 88;
        c2 = 'Y';
        System.out.print("c1 and c2 : ");
        System.out.println(c1+" "+c2);
    }

}
Output :
       c1 and c2 : X Y

Notice that c1 is assigned the value 88, which is the ACCII (and Unicode) value that corresponds to the letter first 127 values in the Unicode character set,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, of to increment the values of a character variable.

Example 
class UsingChar {

    public static void main(String[] args) {
        char c1;
        c1 = 'X';
        System.out.println("c1 is : "+c1);
        c1++;
        System.out.println("c1 is : "+c1);
   
    }
}
Output 
         c1 is : X

         c1 is : Y

In the program, c1 is first given the value X. next c1 is incremented. this results in c1 containing Y the next character in the ASCII (and Unicode) sequence.

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 c = 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.

Escape Sequences:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

The newline character (\n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed.

Following table shows the Java escape sequences:

Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a newline in the text at this point.
\rInserts a carriage return in the text at this point.
\fInserts 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 
class UsingChar {

    public static void main(String[] args) {
        System.out.println("I said \"Yes\" ");
        System.out.println("\n");
        System.out.println(" 1 +\n 1\n --\n 2");
    }
}
Output 
I said "Yes"

 1 +
 1
 --

 2

Character Methods

Here is the list of the important instance methods that all the subclasses of the Character class implement


SNMethods with Description
1isLetter()
Determines whether the specified char value is a letter.
2isDigit()
Determines whether the specified char value is a digit.
3isWhitespace()
Determines whether the specified char value is white space.
4isUpperCase()
Determines whether the specified char value is uppercase.
5isLowerCase()
Determines whether the specified char value is lowercase.
6toUpperCase()
Returns the uppercase form of the specified char value.
7toLowerCase()
Returns the lowercase form of the specified char value.
8toString()
Returns a String object representing the specified character valuethat is, a one-character string.

No comments:

Post a Comment