Java Int Type

Java Int Type


Int Type In Java
As we have already studied integer variables, int type is used to declare an integer type of the variable by putting int keyword before an identifier.
It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. 
Although you might think that using a byte or short would be more efficient than using an int in situations in which the larger range of int is not needed, this may not be the case. 
The reason is that when byte and short values are used in an expression they are promoted to int when the expression is evaluated.
The int data type is the preferred data type when we create variables with a numeric value, and when the space required by input value is 4 bytes or less.
Now we will see a few Examples - 
Example 1 - Now we will see how to explicitly convert a variable of another datatype into integer data type using the int keyword in parentheses.
int num =(int)6.2322;
System.out.println(num);

 
Output - 
 
6
Example 2 -  
/*
Here is another short example.
Call this file "Sample.java".
*/
class Sample{
public static void main(String args[]) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}

 
Output - A sample output would be -
This is num: 100

The value of num * 2 is 200