One of the Java primitive types is int (integer, whole number) type. You can use it to create variables which can contain positive and negative whole numbers.
The range of the int values are from -2147483648 until 2147483647.
Example program with some common operations with int:
public class MainClassPrimitiveInteger { public static void main(String[] args) { int myNumber = 5; // Integer (whole number) /** * Use int to represent whole positive and negative numbers. * You can use this type of variables to perform mathematical operations */ // ------------- Add two positive numbers ------------- int myFirstNumber = 100; int mySecondNumber = 200; int resultAddTwoPosNumbers = myFirstNumber + mySecondNumber; System.out.println("myFirstNumber + mySecondNumber = " + resultAddTwoPosNumbers); // ------------- Reuse same variable ------------------ int justPositiveNumber = 20; // it will add 20 + 20 = 40 and overwrite justPositiveNumber justPositiveNumber = justPositiveNumber + justPositiveNumber; System.out.println("justPositiveNumber + justPositiveNumber = " + justPositiveNumber); // ------------- Reuse same variable another example ------------- int anotherPositiveNumber = 10; anotherPositiveNumber += anotherPositiveNumber; // it will add 10 + 10 = 20 and overwrite anotherPositiveNumber System.out.println("anotherPositiveNumber + anotherPositiveNumber = " + anotherPositiveNumber); // ------------- Add Positive number and negative number ------------- int myPositiveNumber = 10; int myNegativeNumber = -5; int resultAddPosNegNumber = myPositiveNumber + myNegativeNumber; // 10 + (- 5) = 10 - 5 = 5 System.out.println("myPositiveNumber + myNegativeNumber = " + resultAddPosNegNumber); // ------------- Subtract two positive numbers ------------- int testNumber = 10; int testNumberSecond = 5; int subtract = testNumber - testNumberSecond; // 10 - 5 = 10 - 5 = 5 System.out.println("testNumber + testNumberSecond = " + subtract); // ------------- Use int numbers with comparison operators ------------- int bigNumber = 2_000; int smallNumber = 20; boolean firstComparison = bigNumber > smallNumber; System.out.println("Is bigNumber > smallNumber " + firstComparison); boolean secondComparison = bigNumber < smallNumber; System.out.println("Is bigNumber < smallNumber " + secondComparison); boolean tirthComparison = bigNumber == smallNumber; System.out.println("Is bigNumber > smallNumber " + tirthComparison); } }
Output:
myFirstNumber + mySecondNumber = 300
justPositiveNumber + justPositiveNumber = 40
anotherPositiveNumber + anotherPositiveNumber = 20
myPositiveNumber + myNegativeNumber = 5
testNumber + testNumberSecond = 5
Is bigNumber > smallNumber true
Is bigNumber < smallNumber false
Is bigNumber > smallNumber false