The int data type is a 32-bit signed two’s complement integer that allows a wide range of values from -2,147,483,648 (-2³¹) to 2,147,483,647 (2³¹ - 1).
Here, integer is generally used as the default data type for integral values unless there is a specific consideration for memory usage.
The default value for an int variable is 0.
Syntax:
Source Code:
import java.util.Scanner;
public class AgeCalculator {
public static void main(String[] args) {
// Create a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Prompt user for their birth year
System.out.print("Enter your birth year: ");
int birthYear = scanner.nextInt(); // Birth year entered as an int
// Get the current year
int currentYear = 2025;
// Calculate age
int age = currentYear - birthYear;
// Display the age
System.out.println("\nYour age is: " + age + " years.");
scanner.close();
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

