Java int keyword is used to define one of the eight primitive datatype supported by Java. It provides means to create int type variables which can accept a int value. Following are the characteristics of a int data type.
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
Integer is generally used as the default data type for integral values unless there is a concern about memory.
The default value is 0
Example: int a = 100000, int b = -200000
A int variables represents a reserved memory locations to store int values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store int values in int variables.
Example 1Following example shows the usage of int primitive data type we've discussed above. We've created a int variable as intValue and assigned it a int value. Then this variable is printed.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { int intValue = 2; System.out.println("int: " + intValue); } }Output
int: 2Example 2
Following example shows the usage of int primitive data type in an expression. We've created two int variables and assigned them int values. Then we're creating a new int variable intResult to assign it the sum of int variables. Finally result is printed.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { int intValue1 = 2; int intValue2 = 4; int intResult = intValue1 + intValue2; System.out.println("int: " + intResult); } }Output
int: 6Example 3
Following example shows the usage of int variable with an invalid value. We've created a int variable as intValue and assigned it an out of range value.
package com.tutorialspoint; public class JavaTester { public static void main(String args[]) { int intValue = 2147483650; System.out.println("int: " + intValue); } }Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The literal 2147483650 of type int is out of range at com.tutorialspoint.JavaTester.main(JavaTester.java:5)
java_basic_syntax.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4