Gadgets

Sunday, 17 February 2013

Data Types in Java


Data types: -
               A data type represents the type of data stored in variable.
Integer data types: -
These data types represent integer numbers 

Data type                     Memory size                           min and max value
1. byte                           1  Byte                                            - 128 to +127
2. short                           2  Bytes                                 - 32768 to +32767
3. int                               4  Bytes                                 - 21474836 to +2147483647
4. long                            8 Bytes                                  -9223372036854775808 to 
                                                     +9223372036854775807                                                                                                                                                                         
Float data types: - Float means floating point numbers. Float data type represent numbers with decimal points. 

Ex: - 1.5, 3.14159,-2.2, 100.0 etc….

         Float means single precision floating point number. Double means double precision floating point number. Precision means accuracy.

Data type                 Memory size                           min and max value
1) float                       4 bytes                                    1.4e-45 to +3.4e38.
2) double                    8 bytes                                    4.1e-324 to+1.8e308.
 e means into 10 powers. (x 10power).

Character data type: -

Data type                Memories size                          min and max valve
  char                     2 bytes                                        0 to 65535 

     This data type represents a single character. In C or CPP languages using  only one byte in a char.
Ex: -    char ch = ‘x’;

    Single character literal write inside the ‘ ’(single code).
    write string in the double codes (“ ”).
A string represent group of characters.
EX: -                                                                                   
            String name=“narendar reddy”;

ASCII: - American Standard Code for Information Interchange.

Boolean data types: - These data types represent only 2 values either a true or false.
EX: -   boolean response=true;

Operators: - An operator is a symbol that performs on an operation.
EX: -
If an operator acts on a single variable, it is called unary operator.
If an operator acts on a 2 variables, it is called binary operator.
If an operator acts on a 3 variables, it is called ternary operator.

Operators in java: -

1)      Arithmetic operators: - + , - , * , / , %
These operators perform basic arithmetic calculations.
EX: - If a=5, b=5;
                                                     Operator
                        a   +   b
   
         Operands
2) Unary operators: -   - , ++ , --
            These operators act upon a single operand.
àUnary minus ( - ):- This operator negates the value of a variable.
EX:-
            int  x=5;
            System.out.println(-x);             o/p:- -5
            System.out.println(-(-x));        o/p:-  5

Increment (++): - This operator increases the value of a variable by ‘1‘.

EX: - public class UnaryOperator {
    public static void main(String args[])
    {
        int a=10;
        System.out.println(" a is"+a); // a=10
        ++a;
        System.out.println(" a is"+a); //a=11
    }
 }
Writing ++ before a variable is called pre – incrementation
Writing ++ after a variable is called post – incrementation

àWhat is the value of the following expression? Given a=7? ++a * a++?
A) A) 49                      B) 64               C) 72               D) none of these

Decrement (--): - This operator decreases the value of a variable by ‘1‘.
EX: - int x=1;
            --x;       O/P:  0
            x--;       O/P: -1
Writing -- before a variable is called pre – decrementation
Writing -- after a variable is called post – decrementation


3) Assignment operators: -  = , += , -= , *= , /= , %=
            a) This operator is used to store a value in to a variable.
EX: - int x=5;
            b) It is used to store the value of a variable in to another variable.
EX: - int  x=y;
            c) It is used to store the value of an expression in to a variable.
EX: - int x = y + z - 4;
            Expressions mean combination of a variables & values.
Note: - We can not write more than 1 variable at the left side of assignment.
EX: - x + y = 15;   // invalid

4) Relational operator: - < , > , <= , >= , == , !=
            These operators are useful to compare to quantities. They are used in construction of condition.
EX: -
            if(a>b)
           if(a<=b)

5) Logical operators: - &&(and) , | |(or) , !(not)
            These operators are useful to create compound condition. The compound condition is condition of several simple conditions.
EX: -
            If(a>b||b>c||c>d)-----;
                                              
            Any one thing is true this will be executed;
            If(x==10&&y!=10)----;
                                                   
            Both are true this will be executed.
            If(!(x==!))------;
            If(!(x==1 && y==2))----;


6) Boolean operators: - & , | , !
            Boolean operators act upon Boolean variables and they return Boolean result
boolean a,b;
a=true;
b=false;
EX: -
            a & b à false
            a | b   à true
            a & a à true                        boolean type results
            b | b   àfalse
            ! a     à false
            ! b     à true


7) Bitwise operators: -  ~ , & , | , ^ , << , >> , >>>
           These operators act up on individual bits binary digits, (0&1) of numbers.

No comments:

Post a Comment