Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Numbers


As we have seen in the Data Types topic, there are two types of Numbers supported by Java. Integers and floating point numbers.


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Any whole number(Be it -4 or 45) can be represented by the Int datatype


Integers


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Below are the data types used for Integers.

  1. int Data Type



    The Data Type for a whole number is int.

    The Int data type is used to store whole numbers from -2147483648 to 2147483647. And takes 4 bytes of memory.

    Example :



    public class MyApplication
    {
    	public static void main(String[] args) {
    	
    		int x = -34535;
    		System.out.println(x);
    	}
    }
    


    Output :



      -34535

  2. byte Data Type



    The Data Type for a whole number is byte.

    The byte data type is used to store whole numbers from -128 to 127. And takes 1 byte of memory.

    Example :



    public class MyApplication {
    	public static void main(String[] args) {
    	
    		byte x = 34;
    		System.out.println(x);
    	}
    }
    


    Output :



      34

  3. short Data Type



    The Data Type for a whole number is short.

    The byte data type is used to store whole numbers from -32768 to 32767. And takes 2 bytes of memory.

    Example :



    public class MyApplication {
    	public static void main(String[] args) {
    		
    		short x = 32535;
    		System.out.println(x);
    	}
    }
    


    Output :



      32535

  4. long Data Type



    Say, we have a large value 3372036854775808.

    Well! In this case int data type will not be able to hold that value.

    And in this scenario, we need a long data type.

    A long data type is used to store whole numbers from -9223372036854775808 to 9223372036854775808. And takes 8 bytes of memory.

    For example 3372036854775808.

    Example :



    public class MyApplication
    {
    	public static void main(String[] args)
    	{
    		long x = 3372036854775808L;
    		System.out.println(x);
    	}
    }
    


    Output :



      3372036854775808


    So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.
    Note : You must use suffix 'L' with the value '3372036854775808', i.e. '3372036854775808L' to denote the value is of long data type.

  5. Float Data Type



    A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 4 bytes.

    For example 5.987.

    Example :



    public class MyApplication
    {
    	public static void main(String[] args)
    	{
    		float x = 5.987F;
    		System.out.println(x);
    	}
    }
    


    Output :



      5.987


    So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.

    float x = 5.987F;


    Just note, we have mentioned 5.987F instead of the number 5.987. This is an additional step you need to write to tell Java that 5.987 is a Floating point number.

  6. double Data Type



    A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 8 bytes.

    It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

    For example 5.987434343434343434343.

    Example :



    public class MyApplication
    {
    	public static void main(String[] args)
    	{
    		double x = 5.98743486215732D;
    		System.out.println(x);
    	}
    }
    


    Output :



      5.98743486215732


    So, in the above example, we have created a variable x of type Double. And assigned the value 5.98743486215732 to it.

    double x = 5.98743486215732D;