Java Int Keyword
Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It has a 32-bit signed; two's complement integer storage capacity. The keyword "int" is used to store an integer value in Java programming. Methods with a return value can be created using int. In Java computer languages, it is a primitive data type.
Important points of int keyword in java:
- The integer that is initiated can be any value between -231 and 231, inclusive.
- Int is a 32-bit unsigned integer with a minimum value of 0 and a maximum value of 232-1 that can be used as of Java 8.
- The default setting of the java int keywordis allocated by 0.
- The java int keyword stores a size of4-byte which is a default size.
- For integral values, int is typically used as a default data type.
- In Java, the int has a wrapper class called Integer.
- While an integer may store a null value, an int cannot.
- In comparison to primitive type, the integer class has the advantage of being usable in collections with generics or object references.
- Like other classes, the integer class requires that you build objects with a single field, called int.
Syntax
The syntax of keyword int is:
int <variable_name><integer_value>;
Examples of Java int Keyword
Example 1:
In this program we will implement the int keyword to print positive and negative values.
public class IntEx1
{
public static void main (String...s)
{
int H1=20;
int H2=-30;
System.out.println("H1: "+H1);
System.out.println("H2: "+H2);
}
}
Output:
H1: 20
H2: -30
Example 2:
This program is to check whether the int keyword stores the decimal values or not.
public class IntEx2
{
public static void main (String...s)
{
int hi=11.5;
System.out.println("hi: "+hi);
}
}
Output:
error: incompatible types: possible lossy conversion from double to int.
Example 3:
This program is to check whether the int keyword stores the float values or not.
public class IntEx3
{
public static void main (String...k)
{
int n=20f;
System.out.println("n: "+n);
}
}
Output:
error: incompatible types: possible lossy conversion from float to int
Example 4:
Let's look at an example to see if the int data type can store char values. In this situation, the compiler implicitly converts the character to an int type and outputs the corresponding ASCII value.
public class IntEx4
{
public static void main (String...s)
{
int n11='c';
System.out.println("n11: "+n11);
}
}
Output:
n11: 99
Example 5:
The minimum and maximum values in this example are stored in the int data type.
public class IntEx5
{
public static void main (String... s)
{
int low value=-2147483648;
int high value=2147483647;
System.out.println("low value: "+low value);
System.out.println("high value: "+high value);
}
}
Output:
low value: -2147483648
high value: 2147483647
Example 6:
This program is to create the method which returns the value of integer.
public class IntEx6 {
public int output (int n)
{
return n;
}
public static void main (String [] s) {
IntEx6 bj=new IntEx6();
System.out.println(bj. Output (30));
}
}
Output:
30
Example 7:
This program is using methods and liocal variables.
public class LocalVariableEx 7 {
public int add (int a) {
int add = 0;
for (int i = 0; i <a; i++) {
add = add + i;
}
return add;
}
public static void main (String [] s) {
LocalVariableEx 7 localVariableEx 7 = new LocalVariableEx 7();
int sum = localVariableEx 7.add (10);
System.out.println("addition of initial 10 numbers -> " + add);
}
}
Output:
addotion of initial 10 numbers -> 45
Example 8:
public class intEx 8{
public static void main (String s []) {
boy age (18);
}
static int boy age (int a) {
System.out.println("Boy age is :"+a);
return0;
}
}
Output
Boy age is: 18