How to Add Buttons and Text to Your JavaFX Project
How to Use Fall-Through to Your Advantage in Java Programming
What Is Recursion in Java Programming?

Integer Data Types in Java

An integer is a whole number — that is, a number with no fractional or decimal portion. Java has four integer types, which you can use to store numbers of varying sizes.

Type Number of Bytes Range of Values
byte 1 –128 to +127
short 2 –32,768 to +32,767
int 4 –2 billion to +2 billion
long 8 –4,000 trillion to +4,000 trillion

The most commonly used integer type is int. You can use short or even byte when you know the variable won’t need to store large values, and you can use long when your program will require large values — for example, when calculating the federal deficit.

Java allows you to promote an integer type to a larger integer type. In other words, you can assign the value of a shorter integer type to a longer integer variable, like this:

int xInt;
long yLong;
xInt = 32;
yLong = xInt;

Java does not allow the converse, however. The following code is not valid:

int xInt;
long yLong;
yLong = 32;
xInt = yLong;

In Java 7, you can include underscores to make longer numbers easier to read. Thus, the following statements all assign the same value to the variable xLong:

long xLong = 58473882;
xLong = 58_473_882;
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Local Variables in Java
JavaFX’s Image Class
Getting Help from Eclipse
What Are Java Arrays?
JavaFX Controls: ProgressIndicator and ProgressBar
Advertisement

Inside Dummies.com