Java Primitive DataTypes

Java Primitive DataTypes


Java data types will tell us what type of data can variable hold.
Ex:
int x = 10;
In above variable x can hold int data type value.so we must declare any variable with data type before we can use variable.
Java supports 8 primitive data types
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean.

    Integer data types

    Data Type Size Range Default value
    byte 1 byte(8 bits) -2^7 to 2^7-1 or -128 to 127 0
    Short 2 bytes -2^15 to 2^15-1 or -32768 to 327670
    int 4 bytes 2^31 to 2^31-1 or -2147483648 to 21474836470
    long 8 bytes 2^63 to 2^63-1 or -9223372036854775808 to 92233720368547758070
    Example
    package com.practice;

    public class Primitive_Datatype_Demo {

    // main method
    public static void main(String[] args) {
    byte b = 100;// byte data type
    short s = 2435;// short data type
    int i = 2345678;// int data type
    long l = 686968694884l;// long data type
    System.out.println("value of byte data type :" + b);
    System.out.println("value of short data type :" + s);
    System.out.println("value of int data type :" + i);
    System.out.println("value of long data type :" + l);
    }

    }
    Output

    Char data type
    Data Type size min value max valueDefault value
    Char 2 bytes \u0000( or 0)\uffff (or 65,535) \u0000
    Example
    package com.practice;

    public class Primitive_Datatype_Demo {

    // main method
    public static void main(String[] args) {
    char c = 'b';// char data type
    System.out.println("value of char data type :" + c);

    }

    }
    Output

    Floating-point types
    Data Type size RangeDefault value
    float 4 bytes -3.4E^38 to -1.4^45 0.0f
    double 8 bytes -1.4^308 to 1.4^308 0.0
    Example
    package com.practice;

    public class Primitive_Datatype_Demo {
    // main method
    public static void main(String[] args) {
    float f = 10.02f;// float data type
    double d = 10.2345d;//double data type
    System.out.println("value of float data type :" + f);
    System.out.println("value of double data type :" + d);
    }

    }

    Output

    boolean data type
    Data Type size RangeDefault value
    boolean Not applicable true or false false
    Example
    package com.practice;

    public class Primitive_Datatype_Demo {
    // main method
    public static void main(String[] args) {
    boolean b = true;// bolean data type
    boolean f = false;
    System.out.println("value of boolean data type :" + b);
    System.out.println("value of boolean data type :" + f);
    }

    }
    Output

    -->