











Java Program to Add Integers


What is Integers in Java ?
In Java, an int is a primitive data type that represents an integer value. An integer is a whole number, such as -1, 0, 1, 2, and so on. The int data type is generally used to store small to medium-sized values that do not require a lot of precision.
we will explore the concept of the int data type in Java. We will learn about its characteristics, how it is used, and its limitations. Understanding the int data type is essential for writing efficient and accurate Java programs, as it is commonly used for storing small to medium-sized whole numbers.
Range of Integers in java
In Java, the int data type is a 32-bit signed integer, which means that it can represent values ranging from -2147483648 to 2147483647. This range is determined by the number of bits that are used to represent the value.
Here is a pseudo code example of how you can use the range of the int data type in Java:
int min = Integer.MIN_VALUE; // -2147483648 int max = Integer.MAX_VALUE; // 2147483647 System.out.println("The minimum value of an int is: " + min); System.out.println("The maximum value of an int is: " + max);
Note: The int data type is limited to 32 bits, so it may not be sufficient for storing very large or very precise values. In such cases, you may need to use a different data type, such as long, which is a 64-bit signed integer, or BigInteger, which can represent arbitrarily large integers.
Example :
public class Main{ public static void main(String[] args) { int x = 10; int y = 20; // Calculate the sum of x and y int sum = x + y; System.out.println("The sum of x and y is: " + sum); } }
Output :
The sum of x and y is: 30
Example :
public class Main{ public static void main(String[] args) { int a = 10; int b = 20; int c = 30; int d = 40; int e = 50; // Calculate the sum of a, b, c, d, and e int sum = a + b + c + d + e; System.out.println("The sum of a, b, c, d, and e is: " + sum); } }
Output :
The sum of a, b, c, d, and e is: 150
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment