Java Primitive data Types. - Neuroon Networks

Breaking

Monday, February 26, 2018

Java Primitive data Types.

           There are eight primitive data types in java. In this tutorial we are going to talk about those eight data types as well as how to use them in arithmetic operations ( + , - , * , /  and %) and relation operations (< , > , <= , >=) and also equality operations (== , !=).

          First learn about the eight data types.


  1. boolean 
    • true or false.
    • Default Value - false.
    • Size - 1 bit
  2. byte
    • Twos complement integer.
    • Default Value - 0
    • Size -  8 bits.
  3. char
    • Unicode character.
    • Default Value -  \u000.
    • Size - 16 bits. 
  4. short
    • Twos complement integer. 
    • Default Value - 0.
    • Size -  16 bits.
  5. int
    • Twos complement integer.
    • Default Value - 0 .
    • Size - 32 bits.
  6. long
    • Twos complement integer. 
    • Default Value - 0 . 
    • Size - 64 bits.
  7. float
    • IEEE 754 floating point.
    • Default Value - 0.0 .
    • Size -  32bits.
  8. double
    • IEEE 754 floating point. 
    • Default Value - 0.0 . 
    • Size -  32bits.

Given below is a example to show how to declare the variables in java.




    its very simple just as the other programing languages. now i will show you how to assign a value to a variable.


    In this case you have to be more careful. Because you have the limits it means every data type has its own way. in char you have to give your character within the single quotes (example ' a' ). And the other reason is some of the data types has maximum and minimum values that can be assigned to them.



    Lets see how to print the value of the variable.



    Now i am gonna show you how the variables gets the values with the arithmatic operations.


    In here do you know why the 1/2=0.0 in the java , its because of the data type,

    When you do 1/2 that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).

    If you convert any one of them to double, you'll get a double result.

    so if you want the real value for the 1/2 you have to do it like

    double c = 1.0/2; or  double c = 1/2.0;
    Another way to do it is you have to assign the value like this
    float f=1;
    double c=f/2;

     % operation it give the value of the reminder after devision.
    As a example 10%3=1 (3*3+1 so answer is !).


    Now lets see what the relation operations and the  equality operations can do.

    What actually relation operations and the  equality operations do. They just compare two or more characters numbers or anything and give the boolean value as the out put. We use these thing mainly in the if else clauses. we will discuss them later.

    Can you guess the Out for the following code.



    Try these things. See you guys in next tutorials.
    Thank you

    No comments:

    Post a Comment