What You Should Know about the Size of Your Java Code

By Barry A. Burd

When you are coding in Java, you will want to take the actual size of your code into consideration. Let’s look at an example. But first. here are today’s new vocabulary words:

  • foregift (fore-gift) n.A premium that a lessee pays to the lessor upon the taking of a lease.
  • hereinbefore (here-in-be-fore) adv.In a previous part of this document.

Now imagine yourself scanning some compressed text. In this text, all blanks have been removed to conserve storage space. You come upon the following sequence of letters:

hereinbeforegiftedit

The question is, what do these letters mean? If you knew each word’s length, you could answer the question:

here in be foregift edit

hereinbefore gifted it

herein before gift Ed it

A computer faces the same kind of problem. When a computer stores several numbers in memory or on a disk, the computer doesn’t put blank spaces between the numbers. So imagine that a small chunk of the computer’s memory looks like the stuff in the image below. (The computer works exclusively with zeros and ones, but the image below uses ordinary digits. With ordinary digits, it’s easier to see what’s going on.)

Size of Java code
Storing the digits 4221.

What number or numbers are stored in this image? Is it two numbers, 42 and 21? Or is it one number, 4,221? And what about storing four numbers, 4, 2, 2, and 1? It all depends on the amount of space each number consumes.

Imagine a variable that stores the number of paydays in a month. This number never gets bigger than 31. You can represent this small number with just eight zeros and ones. But what about a variable that counts stars in the universe? That number could easily be more than a trillion, and to represent 1 trillion accurately, you need 64 zeros and ones.

At this point, Java comes to the rescue. Java has four types of whole numbers. You can declare

intgumballsPerKid;

You can also declare

bytepaydaysInAMonth;

shortsickDaysDuringYourEmployment;

longnumberOfStars;

Each of these types (byte, short, int, and long) has its own range of possible values.

Java’s Primitive Numeric Types
Type Name Range of Values
Whole Number Types
byte –128 to 127
short –32768 to 32767
int –2147483648 to 2147483647
long –9223372036854775808 to 9223372036854775807
Decimal Number Types
float –3.4×1038 to 3.4×1038
double –1.8×10308 to 1.8×10308

Java has two types of decimal numbers (numbers with digits to the right of the decimal point). You can declare

double amount;

You can also declare

floatmonthlySalary;

Given the choice between double and float, you probably want to choose double. A variable of type double has a greater possible range of values and much greater accuracy.

The table above lists six of Java’s primitive types (also known as simple types). Java has only eight primitive types, so only two of Java’s primitive types are missing fromthe table above

As a beginning programmer, you don’t have to choose among the types in the table. Just use int for whole numbers and double for decimal numbers. If, in your travels, you see something like short or float in someone else’s program, just remember the following:

  • The typesbyte, short, int, and long represent whole numbers.
  • The types float and double represent decimal numbers.

Most of the time, that’s all you need to know.