Default data types in Java

Default data types of Java of language to store Integer, floating and character constants are int, double and char respectively.

            Default data types of Java to store any integer constant is always an int i.e. Java provides four bytes of memory to hold any integer constant and the range of int data type is various from  –2147483648 to  +2147483647.

class Example 1

 {

     public static void main (String args[ ])

{

            long n= 21474836479;

            System out println (“value=”+n);

}

}

            In the above example, value 21474836479 is an integer constant during the compile time of program, compiler will check the range of default data type for the given constant value 21474836479. But the given constant value is not in the range of int data type therefore interpreter will not able to hold the constant value within the four bytes of memory of int data type and hence compiler will generate error as given below.

Error :  value 21474836479 is too long.

            To solve the above problem, it is necessary to increase the capacity of default data type from 4 bytes to 8 bytes by using suffix L as shown in the following example.

class Example1

{

             public static void main (String args[ ])

            {

                 long n= 21474836479L;

                 System. out.println(“Value=”+n);

            }

}

Output : Value = 21474836479

            In the above Example character L (suffix L) represents long data type to hold constant integer values. During interpretation, interpreter will change its default data type from int to long for the execution of single statement.

Default data type of Java language to hold any float constant is always double i.e. for any floating constant value Java provides 8 bytes of memory.

class Example 2

{

             public static void main (String args[ ] )

            {

                 float n = 1.2;

                System.out.println (“value=”+n);

            }

}

            In the above Example value 1.2 is a floating constant value but the default data type of Java to hold such value is double that is Java provides 8 bytes of memory to hold the above constant value but the variable is having only four bytes of memory therefore during run-time of program Java Interpreter will not able to convert double value into float value hence above program will generate error. To solve the above problem, it is necessary to convert the double value into float value by using type casting as shown in the following example.

class Example 2

{

            public static void main (String args[ ])

            {

                 float n= (float) 0.5;

                 System.out.println (“value =”+n);

            }

}

Output : value = 0.5

class Example3

{

            public static void main (String args[ ])

            {

                 float n = 0.5f;

                 System.out.println (“value=”+n);

            }

}

Output: value =0.5

Leave a Reply

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