Copyright

Java Integer: Max Value & Min Value

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java ArrayList Add Method: Code & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Java String Constant Pool
  • 0:57 Java Constants
  • 2:11 Checking for Max & Min
  • 2:49 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor
Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Expert Contributor
Elaine Chan

Dr. Chan has a Ph.D. from the U. of California, Berkeley. She has done research and teaching in mathematics and physical sciences.

Integer values in Java can only hold a certain range of values. This lesson will discuss the minimum and maximum values for an integer and provide code examples for finding these values.

Java String Constant Pool

An integer in Java is a whole number, either positive or negative, including zero. However, there are lower and upper limits to integers. The upper limit is 231 - 1. The lower limit is -231. Why the - 1 on the upper limit? This is because we need to count the number zero as part of the range.

When you declare an integer value in Java, you are unable to set its value past the minimum or maximum. Not even by one number! Most developer tools will warn you if the number is going over, like this warning in the code:


Java integer limit too big


We've stated the upper and lower limits to the integer. The upper limit is 231 - 1, but what is that number? Thankfully, there's a good way to print and display the actual integer values of the minimum and maximum allowed values. There are two variables that Java provides for us to use.

Java Constants

There are actually two constant fields in Java that we can use for this. A constant is a type of variable (that doesn't really vary) that holds a value that will not change. Typically, constants are all uppercase. When you declare them in your code you use the data type of final. This means they cannot be changed by anyone, ever.

These two constant values are actually part of the Integer class. Java wraps all data types (int, float, double, string, integer) into larger parent classes, complete with their own fields and methods. These classes are called wrapper classes because they encompass the primitive types into a class that you can use in code. The Integer class holds the constant values for minimum and maximum integer values, as well as other methods that we'll see in more advanced Java programming.

Therefore, if you want to display the minimum and maximum, you simply write a routine to show them, like the one below:

System.out.println("Integer Min = " + Integer.MIN_VALUE);
System.out.println("Integer Max = " + Integer.MAX_VALUE);

When the program runs, the following output is displayed:


Java MIN MAX output


To unlock this lesson you must be a Study.com Member.
Create your account

Additional Activities

Java Integer Activity

  1. What happens if you divide integer 99 by integer 10?
  2. What is the maximum value of an integer?
  3. Given two integers, which one is larger?
  4. Increment operator on an integer with ++ postfix on right side example.
  5. Increment operator on an integer with ++ prefix on right side example.


Answers


1. What happens if you divide integer 99 by integer 10 ?


You get 9 from the following code:

class Main {

public static void main( String [ ] args) {

System.out.println("Dividing two integers results in dropping the decimal part");

System.out.println(99/10);

System.out.println("Dividing float by integer results same as dividing float by float");

System.out.println(99./10);

}

}


2. What is the maximum value of an integer?

2147483647


Use the output from this code:

class Main {

public static void main(String [ ] args) {

System.out.println("Maximum value of integer is");

System.out.println(Integer.MAX_VALUE);

}

}


3. Given two integers, which one is larger?


public static void main(String [ ] args) {

int x =10;

int y=20;

System.out.println("Maximum value is");

System.out.println(Integer.max(x,y));

}

}


4. Increment operator on an integer with ++ postfix on right side example.

Are the x and y values the same?


class Main {

public static void main(String args) {

int x =1;

int y = x++;

System.out.println("x = " + x);

System.out.println("y = " + y);

}

}


5. Increment operator on an integer with ++ prefix on right side example.


class Main {

public static void main(String args) {

int x =1;

int y =++ x;

System.out.println("x = " + x);

System.out.println("y = " + y);

}

}

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support