Pages

Monday 30 September 2013

Data types of Java

                      Java has eight primitive data types. The below table show you all the primitive data types. 


Data Type classification
Data types
Width in Bits
Integer Data Types
byte
8
short
16
int
32
long
64
Floating-Point Data Types
float
32
double
64
Character
char
16
Boolean
boolean
------



  •        Character data type of Java:

                     Characters in Java are implemented using char keywords. Character in Java is 16 bit number  value which represents a two byte Unicode character.

      Default value of char in java is ‘u\0000’, is a Unicode representation of null value.

Note: - In C/C++ programming character has 8 bit width and will not use Unicode representation. 

Program example for Characters:
 
/** Program example of characters
 *
 * @author Jaivanth
 *
 */
public class SampleCharacter
{
            public static void main(String[] args)
            {
           

   char ch1,ch2,ch3,ch4; //character declaration
  
   ch1='J'//characters initialization
   ch2=65;
   ch3='V';
   ch4=65;   //65 is ASCII value of 'A'
  
   System.out.println("I Love "+ch1+ch2+ch3+ch4);//print instruction
  
   }
}

Note: Some characters are not possible to enter directly; there are many escape sequences that will help in entering those characters. Some of these escape sequences are listed in table below.

Character Escape Sequence
Description
\’
Single Quote
\”
Double Quote
\\
Backslash
\r
Carriage Return
\n
New Line
\t
Tab
\b
Backspace
\f
Form Feed


  •       Boolean data types of Java:

           A Boolean data type returns only two logical values, true or false. The values of true or false will not be converted into any numerical representation. The storage requirement depends on the underlying hardware/software implementation. The size of Boolean is not necessarily to be known by a programmer. Some books/articles even say that size of Boolean depends on the JVM present in machine.
 

Operators of Java:

        The operators are broadly classified into six types. The table below shows list of data types:


Classification of Java Operators
Arithmetic Operators
Bitwise Operators
Relational Operators
Boolean Logical Operators
Assignment Operators
The ‘?’ Operator


  •        Arithmetic Operators:

   
              Lets’ divide arithmetic operators into two types one containing single character and the other containing two characters. This is done just to help to remember the operators more easily. 


Single Character Arithmetic Operators
Details
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
Two Character Arithmetic Operators
Details
++
Increment
--
Subtraction
+=
Addition Assignment
-=
Subtraction Assignment
*=
Multiplication Assignment
/=
Division Assignment
%=
Modulus Assignment

                Increment and Decrement Operators:
                                Both the increment and decrement operators are divided into two types:
1.     Pre-increment/decrement (ex: ++a/--a)
2.     Post-increment/decrement (ex: a++/a--)


  •      Bitwise Operators:


              Java defines many bitwise operators. These operators can be applied to long, int, short, char and byte.
               To make it easy to remember we shall divide bitwise operators into bitwise logical operators and Shift Operators.


Bitwise Logical Operators
Details
~
Unary NOT
&
AND
|
OR
^
Exclusive-OR/X-OR
&=
Bitwise AND Assignment
|=
Bitwise OR Assignment
^=
Bitwise X-OR Assignment

Shift Operators
Details
>> 
Shift Right
<< 
Shift Left
>>> 
Shift right zero fill
<<=
Shift left assignment
>>>=
Shift right zero fill assignment



  •     Relational Operator:


               Relational operator is used to determine the relationship between operands.
The table below shows relational operators in Java.

Relational Operator
Details
==
Equal to
!=
Not Equal to
Greater then
Lesser then
>=
Greater than or equal to
<=
Lesser than or equal to



  •        Boolean Logical Operators:


              Logical operators will return Boolean values true/false.

Boolean Logical Operators
Details
&
Logical AND
|
Logical OR
^
Logical XOR
||
Short-circuit OR
&&
Short-circuit OR
!
Logical unary NOT
&=
AND assignment
!=
OR assignment
^=
XOR assignment
==
Equal to
!=
Not Equal to
?:
Ternary if-then-else



  •        The Ternary if-then-operator (? :):


                         Java has a special ternary operator that can replace certain if-then-else statements. We shall see an example:

    Expression1? Expression2:Expression3

Expression1 can be any expression that can return a Boolean value. If expression1 is true, then expression2 is executed; else expression3 is executed.


    Example Program for Ternary Operator

/** Sample program implementing
 * Ternary
 * Operator
 *
 * @author Jaivanth
 *
 */

public class SampleTernaryOperator
{
     public static void main(String[] args)
     {
                        int i,t; // declaring variable
                       
                        i=11;  //Initialization
                       
                        t=i<10?20:10; // ternary operator statement
                       
                        if(t==10)  // if block
                        {
                                    System.out.println("I Like Coding");
                                   
                        }
                       
                        i=9;
                       
                        t=i<10?20:10; // ternary operator statement
                       
                        if(t==20) // if block
                        {
                                    System.out.println("I Love Java");
                        }
             }
}



  •         Operator Precedence: