Java Data Types

Data types specifies what type and which size of data can be stored in a variable.

Suppose, you want to eat rice and drink water. you will take rice on a plate and water in a glass. You will not take rice in a glass and water on a plate. That sounds funny. Also, you can take rice in a bowl and water in a bottle. Moreover, you can specify which size of plate or glass you want to put items like a small plate or big glass.


So think that you are taking them in a container but while putting them, you are choosing different types and sizes of containers depending on which types of things you want.

In java, you have to specify which type and which size of data you want to put in a variable.

There are 2 types of data types in Java

  1. Primitive data types
  2. Non-primitive data types

Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods.
There are 8 types of primitive datatype:

  1. int
  2. float
  3. double
  4. char
  5. byte
  6. boolean
  7. long
  8. short

Non-Primitive Data Types: These data types are not actually defined by the programming language but are created by the programmer. They are also called “reference variables” or “object references” since they reference a memory location that stores the data.
There are no specific types of non-primitive data types. Some example as:

  1. class
  2. object
  3. interface
  4. abstract
  5. dictionary
  6. array
  7. list
  8. String

etc are Non-Primitive data types.
Example:

public class Main {
    public static void main(String[] args) {
        String name="salman"; //non-primititive
        int age=27; //primitive
    }

}

you can convert from a datatype to another data type. This is called type casting.
Here is an example:

public class Main {
    public static void main(String[] args) {
        int a=10;
        double b=10.5;
        System.out.println("value of b "+(int)b);
        System.out.println("value of a "+(double)a);
    }

}

Output:
value of b 10 //previous value of b was 10.5
value of a 10.0 // previous value of a was 10



about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *