Sunday, 6 April 2014

Character data type...

 TARGET LANGUAGE:JAVA

THEORY:(DATA TYPE:Character and Boolean)

Character

  • Data type used to store characters is char.
  • Java uses UNICODE to represent characters
  • Java char is 16 bit type
  • The range of char is 0 to 65,536
  • There are no negative char.
UNICODE-
Unicode provides a unique number for every character,
no matter what the platform, no matter what the program,
no matter what the language.
Unicode Table


EXAMPLE:
//Demonstrates char Data types
 
class charDemo1
{
  public static void main(String args[])
  {
    char ch1,ch2;
    ch1=88;
    ch2='Y';
    System.out.print("ch1 and ch2");
    System.out.println(ch1+" "+ch2);
  }
}

//ch1 and ch2:X Y

-Notice that ch1 is assigned 88,which is ASCII(and UNICODE) value of the corresponding letter X
 
class charDemo2
{
  public static void main(String args[])
  {
    char ch1,ch2;
    ch1='X';
    System.out.println("ch1:"+ch1);  
    ch1++;
   
    System.out.println("ch1:"+ch1);
  }
}

//ch1:X
//ch1:Y

-As ch1 is increamented the ASCII value is increamented.Thus second time in the output we get Y. 

BOOLEAN

Java has a primitive type,called Boolean for logical values.
It can have only one of the two values true,false.

EXAMPLE

class Booltest
{
  public static void main(String args[])
  {
   boolean b;
   b=false;
   System.out.println("b:"+b);

   b=true;
   System.out.println("b:"+b);
  
   if(b)//a Boolean value can control if statement
   {
     System.out.print("This is executed");
   }
b=false;

   if(b)//This is not executed
   {
     System.out.print("This is not executed");
   }

  }
}

Output of relational operator in if statement is Boolean value
eg 10>9


---------------------------------------------
In the next publication  we shall deal with literals
---------------------------------------------


No comments:

Post a Comment