# Primitive Data Types and Basic Language Rules for Java

Last time we discussed some basic concepts related to object-oriented programming. Two major ideas we defined were objects and variables. In this article, we will continue our discussion of variables by explaining how they hold and interact with data.

Back before man could walk upright and before manicurists began to pluck grizzly hair from the knuckles of our slope-browed ancestors, man has been deciding, in a very primitive way, what type of objects fit into what types of objects. Could water fit into a stylish saber-toothed-tiger bag? Could an enormous prehistoric coconut be contained within the skull cavity of a giant sloth? And who would win in a race, a giant turtle or that enormous sloth?

Millennia later, man is asking similar questions within the programming world. When we last left off, we were discussing variables and their naming conventions. Now that we know how to name them, we will learn what type of data best fits into each one.

Before you can use a variable, you have to declare your variable, which means that you must give it both a name and a type. This is how a declaration looks:

long beer = 10000;

This tells the program that the type of variable is long, with the delicious name of beer, and that it holds the value 10,000. When declaring a variable, it isn’t necessary to assign it a value. We’ll discuss this more later in the Literals section.

Below is a list of the different Primitive Data Types.

Byte is an 8-bit data type that holds values in the range of -128 to 127. It is sometimes used to save space in arrays, in place of integers, as it does not take up as much space. Why use an integer to store the number 100, when you can use a byte and save four times the space? The default value is 0.

Short is a 16-bit data type that holds values in the range of -32,768 to 32,767. Similar to our friend the byte, the Short (he prefers vertically challenged) can also be used to save space within an array, replacing an integer, as it is twice as small as an integer. The default value is 0.

Int is a 32-bit data type that holds values from -2,147,483,648 to 2,147,483,647. This is the standard data type for numbers; however, if you find yourself needing more space you can use long, or if you want to save memory, you can use the byte or short in its place, so long as the number falls within the value range of the aforementioned data types. The default value is 0.

Long is a 64-bit data type that holds values from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. If you can’t fit your number in that range, too bad for you. Default value is 0L.

Float is a 32-bit data type. It is used for floating point numbers (not for precise numbers, such as currency). Its default value is 0.0f.

Double is a 64-bit data type. It too is used for floating point numbers (and again, not for precise numbers such as currency). To save space, you may use float instead. Its default value is 0.0d

Boolean is a data type that holds two types of values: True or False. By default, its value is set at False.

Char is used to store any character. It is a 16-bit data type with the value of U0000. It works with the unicode system, within a range of `u0000'` (or 0) to `'uffff` (or 65535).

String is not technically a data type, but it can be used to hold sentences, and so forth. Its default value is null.

Literals are a string constant or an explicit number. A literal is a constant value assigned to a variable.

{mospagebreak title=Working with Arrays}

While they won’t let you see through walls like their cousin, the X-ray, Arrays act as a holding cell for multiple values of the same type. It’s like taking a group of variables and some glue and sticking them all together. When you create an array, you must determine its length. This length determines the number of placeholders within the array; this number  does not change once the array is created.

A good way to think of it is like an egg carton. When you go to the store you can buy six eggs or twelve eggs (or if you are an eggomaniac, you can opt for the deluxe 24 pack, but come on, let’s not get clucking crazy). Each little crate within that pack was at one point empty. Then they placed it on an assembly line and let the chickens do their thing. Soon, an egg was placed into each holder. Since the carton was only created to hold 12 eggs, you can’t put anymore into it. And since it is an egg carton, you can’t place anything but eggs within it.

Anything you put into an array is referred to as an element. And every element has an index number for you to to refer to it.

In the above display, you will notice there are five boxes, meaning the array has a length of five. Each box of the array has an index number, starting with the number zero and working its way to the number four. Also, you will notice that Index 0 has something inside it, or an element. The other indexes are empty and are waiting to be assigned a value.

The way we create and assign a value is shown below.

class ArrayXray {

public static void main(String[] args) {

int[] YourArray; // declaring an array of integers

YourArray = new int[5]; // allocates memory for 5 integers

YourArray[0] = 100; // initializes or sets the first element

YourArray[1] = 200; // initializes second element

YourArray[2] = 300; // so forth

YourArray[3] = 400;

YourArray[4] = 500;

In the above example, you have created a new array named "Your Array." We made the array an integer (int) data type and assigned it five elements. Then we gave each of those elements a value. Once you have created your array, you can not change it later without recreating the original array.

We also initialized the array using the word new. Without doing so we would have been yelled at by the compiler and received an F on our report card, not to mention an error message.

You can also declare an array in the following manner:

int[] YourArray = {100, 200, 300, 400, 500};

You’ll note in the above sample that the length of the array is determined by the number of values between the "{" and "}" brackets.

{mospagebreak title=Multidimensional Arrays}

If we want to get crazy (and deciding to learn any programming language is a sure indicator that you do), Multidimensional Arrays offer you a way to get there. The following code demonstrates how to declare a two-dimensional array.

Int YourTwoDimensionalArray [] [] = new int [8] [8];

What the above code does is give us an 8×8 array. Think of the way a chess board looks: a grid of 8 rows and 8 columns.

Each of the above boxes would hold two indexes; the left index is used for the row, the right index is used for column

When you allocate memory for a multidimensional array, you only have to set memory for the left dimension. You can allocate the right dimension at a later time, as in the following code.

Int YourTwoDimensionalArray [] [] = new [5] [0]

YourTwoDimensionalArray[0] = new int [2]

YourTwoDimensionalArray[0] = new int [7]

YourTwoDimensionalArray[0] = new int [20]

YourTwoDimensionalArray[0] = new int [25]

YourTwoDimensionalArray[0] = new int [12]

Note also that if you ever need to know the length of an array the following code will print it to your screen:

System.out.println(YourArray.length);

{mospagebreak title=A Few More Rules}

Java likes to separate the men from the boys, the hair from your head, and the code that you write. To do this, we use separators (didn’t see that coming did you?).

Separators

Below is a list of separators and their functions.

 How it Looks Name Function . Period Separates package names from subpackages and classes. Can also be used to separate a variable or method from a reference variable. ; Semicolon Ends statements. , Comma Separates identifiers when declaring variables. {} Braces No not for your teeth. Contains the values of initialized arrays. Also for declining a block of code, classes, methods, local scopes. [] Brackets For declaration of array types or dereferencing array values. () Parentheses Holds lists of parameters in method definition, defining precedence in expressions, holding expressions in control statements, and encasing cast types

Keywords

As part of its diverse vocabulary, Java has 49 reserved keywords. These keywords cannot be used to name a variable, class, or method, and will not magically open doors (rely on good ole’ "open sesame" for that one). You want to see what they look like in a table? Well here they are:

 abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while

Final Notes

That concludes this episode of Beginning Java Programs. In the next article we will discuss Operators and putting those lazy variables to work.