Copyright

Java Data Types: Short, Int & Long

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.

This lesson will provide a overview of the Java primitive Java data types: short, int, and long. These whole-number data types have a range of uses. We will provide syntax and examples of their use in programming.

Java Data Types

Like many programming languages, Java supports several data types. A data type is used to define what kinds of information can be stored into a variable using that data type. It helps to keep data clean and ensure that the right kinds of information are being stored in the right fields.

Think of something that would count steps, or machine hours. You would always want these to be numbers, and not text; so why even allow bad information in the field? Java, and other programming languages, provide data types that ensure this consistency.

We'll cover three of those data types in this lesson: short, int, and long. These are considered the primitive data types. It doesn't mean they are not sophisticated, only that they are are built-in components of a programming language; they are welded into frame. From a data and programming perspective, they store values, not objects, and have a set range of allowed values.

Keep in mind that each of these data types is technically an integer type; therefore, they allow only whole values (values without decimals). They can store negative numbers, but no decimal values or characters.

Variable Declaration

Declaring a variable is a fancy word for creating the variable. Before we get into the specifics of each type, let's review the basic code for declaring/creating our short, int, and long variables.

The following syntax is followed for creating all variable types:

variable type variable name;

We can also assign a value directly to the new variable, for example:

variable type variable name = 7;

Note the semicolon at the end of each line! This is required and tells the computer to process everything before it. Programmers have been known to debug code for hours, only to find a missing semicolon!

Short

In Java, the short data type is the smallest type of the three we cover in this lesson. It's only 2 bytes (16 bits). Like the other data types, it is signed, meaning it accepts both negative and positive values. Of the three data types covered in this lesson, it is the smallest. It accepts values from -32,768 to 32,767.

You may wonder why the values don't go all the way up to 32,768. This is because the number 0 must be counted in the range. And while we're on the subject of zero:

The default value of a short (or int, or long) is zero. This is an important item to remember, since you can save some space and time by not having to always set the variables to a value (if you will assign values later).

The following Java code declares two short variables, sn1 and sn2:

short sn1 = 31000;
short sn2 = -31000;

While it is a valid data type, short is used more for lower-level programming, such as image processing or working with sound processing. It doesn't save much compared to int (the next variable), and doesn't really buy you anything in terms of how the machine uses memory to handle them. Therefore, programmers make more use of int and long.

Int

The next largest data type is the int data type.

In Java, a variable using the int data type consumes 32 bits in memory. An int is a whole number (no decimals) and the valid number range is -2,147,483,648 to 2,147,483,647.

A great use for an int data type is age: 99% of the time, age is reported as a whole, positive value. Kids will tell you that they are five-and-a-half, but most software only cares about your true (non-fractional) age. Therefore, declaring age as an int ensures that rule.

The syntax for declaring an int looks like this:

int customer_age = 18;

Long

A Java long data type can hold the largest integer values. It takes up 64 bits of memory and accepts a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It's useful for storing numbers that outgrow the integer data type.

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

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