Java int Keyword

The int keyword in Java is used to declare a variable that can hold a 32-bit signed two’s complement integer. This is the most commonly used integer data type, providing a good balance between range and memory usage.

Table of Contents

  1. Introduction
  2. int Keyword Syntax
  3. Understanding int
  4. Examples
    • Basic Usage
    • Arithmetic Operations
    • Array of Integers
  5. Real-World Use Case
  6. Conclusion

Introduction

The int data type is a 32-bit signed integer. It is the default choice for integer values in Java, offering a wide range that is sufficient for most applications.

int Keyword Syntax

The syntax for declaring an int variable is as follows:

int variableName; 

Example:

int age; 

Understanding int

The int data type is a 32-bit signed integer. Its size is 4 bytes. The int data type can be used to store numeric values ranging from -2^31 to 2^31-1.

Minimum Value:

  • -2,147,483,648

Maximum Value:

  • 2,147,483,647

Examples

Basic Usage

To demonstrate the basic usage of the int keyword, we will declare an int variable and assign it a value.

Example

public class IntExample { public static void main(String[] args) { int age = 30; System.out.println("Value of int variable age: " + age); } } 

Output:

Value of int variable age: 30 

Arithmetic Operations

You can perform arithmetic operations on int variables just like with any other numeric data type.

Example

public class IntArithmetic { public static void main(String[] args) { int a = 20; int b = 10; int sum = a + b; int diff = a - b; int product = a * b; int quotient = a / b; int remainder = a % b; System.out.println("Sum: " + sum); System.out.println("Difference: " + diff); System.out.println("Product: " + product); System.out.println("Quotient: " + quotient); System.out.println("Remainder: " + remainder); } } 

Output:

Sum: 30 Difference: 10 Product: 200 Quotient: 2 Remainder: 0 

Array of Integers

An array of int can be declared and used to store a sequence of integer values.

Example

public class IntArrayExample { public static void main(String[] args) { int[] intArray = {10, 20, 30, 40, 50}; for (int i : intArray) { System.out.println("Int value: " + i); } } } 

Output:

Int value: 10 Int value: 20 Int value: 30 Int value: 40 Int value: 50 

Real-World Use Case

Counting and Indexing

In most real-world applications, int is used for counting iterations in loops, indexing arrays, and other numeric calculations that require a moderate range of values.

Example

public class IntLoopExample { public static void main(String[] args) { int total = 0; for (int i = 1; i <= 10; i++) { total += i; } System.out.println("Sum of numbers from 1 to 10: " + total); } } 

Output:

Sum of numbers from 1 to 10: 55 

Conclusion

The int keyword in Java is a versatile and commonly used data type for storing 32-bit signed integer values. It provides a sufficient range for most applications and is the default choice for numeric operations in Java. By understanding and using the int data type, you can efficiently manage and perform various operations on integer values in your Java applications.

Leave a Comment

Scroll to Top