Google+

38. 'int' data type








Data Types - In order to assign a value to a variable in Java, we have to first declare the variable as any type. For example, if you want to store integers like 10, 100 etc into a variable say 'var', we have to declare the 'var' as type 'int' as shown below -

int var;

Now we can assign an integer value say 100 as shown below -

var = 100; 

Now if you try to assing a non-interger value say "cat", Java will give you an error -

var = "cat";

Hence in Java, we use Data Types to define the variables to store the required type of values. The below examples will give you an understanding of how Data Types help us to declare variables of different types and assign values to the variables.



int is the most commonly used integer data type.


A sample program for demonstration the usage of int data type:

class Sample1
{
     public static void main(String args[])
     {
          int count = 100;    // A variable declared using integer data type int can store integer values like 100 
          System.out.println(count);
      }
 }

The output of this program will be:

100




What happens on passing the decimal values to an integer variable?

class Sample2
{
    public static void main(String args[])
    {
         int count;
         count = 100.123;
         System.out.println(count);
     }
 }

The following error will be displayed on compiling this program:

error: possible loss of precision

      count=100.123;
            ^
  required: int
  found:    double
1 error


'short' data type is also an integer data type but stores less range of values from -32768 to 32767. This is a very less used data type, so I'm not writing it in a separate post.

'byte' data type is also an integer data type but stores less range of values from -128 to 127. This is a very less used data type, so I'm not writing it in a separate post.

Ranges of Integer data types:






Please comment below to feedback or ask questions.

How to assign decimal values to the variables will be explained in the next post.




9 comments:

divya sandhya said...

Hi Arun,

This is Divya. You have said like, Long integer value can range from (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). But, When I try to execute the below code,
class Int1
{
public static void main(String args[])
{
long count = 9223372036854775807;
System.out.println(count);
long count1=count/9;
System.out.print(count1);

}
}

Output is as below:
Int1.java:5: Integer number too large: 9223372036854775807

Could you please clarify on this.

Thanks & Regards
Divya

Arun Motoori said...

@divya sandhya - From the error message, you can clearly see that Long value is consider as integer value by the compiler. In order to tell the compiler that it is a long value, please add the letter L at the end of the literal. i.e. long count = 9223372036854775807L;

ShivvBlog said...

Hi Arun,

When i ran below program i got an error related to invalid flag:

class Sample1
{
public static void main(String args[])
{
int count = 100; // A variable declared using integer data type int can store integer values like 100
System.out.println(count);
}
}

----------------------
Error:

javac: invalid flag: C:\Automation\Practice Java\Test.Java
Usage: javac
use -help for a list of possible options

Tool completed with exit code 2

swathi T said...

@Shivv:File name should be same as Class name.In this case,save your file as Sample1.java and not Test.java.

PREETI ARUN said...

hello, for int data type range is from " -2147483684 to -2147483683 "

foe below program I am getting error as " D:\Java Learning\datatype.java:9: error: integer number too large: -2147483684
int count= -2147483684; "

per my understanding i should not get error because for int datatype i have declared a value within its permissible range .

When I am using datatype as long, I am not getting error . I would like to know why with int datatype I am getting error when variable value is in permissible range.

class datatype
{
public static void main(String args[])
{
int count=-2147483684;
System.out.println(count);


}

}

PREETI ARUN said...

In below program , I have declared a variable count2 having datatype int, my expectation from below pgm was, i should get error as I have assigned a character value "C" to a int count2 varaible but when I am executing the below pgm I am getting o/p as : count2:67

class datatype
{
public static void main(String args[])
{

int count2 = 'C';
System.out.println("count2:" +count2);

}

}

Arun Motoori said...

@Preeti - Wrong integer range is assigned to the int data type variable.

i.e. int count= -2147483684; is provided instead of int count= -2147483648;

Please correct to resolve the warning.

Arun Motoori said...

@Preeti - Unicode value which represents the letter 'C' will be assigned to the int type variable.

PREETI ARUN said...

Thanks Arun !!

Blog Index