Posts Tagged ‘Java’

Array Declarations – Processing vs Arduino

September 15, 2009

When I need a 20 second description of the Processing versus Arduino Languages I’ve said that Arduino is a subset of Processing. That analogy helps to quickly convey the idea that both languages have many similarities while indicating a difference in features. That analogy however is not accurate. Like both Processing and Arduino the Java language has deep roots in the C programming language. Unlike Arduino, Processing is built on top of Java with features that simplify its use for people not wanting to dive into a computer science degree. With these languages you can understand how to perform certain programming techniques quickly. To a great extent though, by combing Arduino and Processing when you learn to program in one you learn how to program in the other. Allocating and initializing arrays, however, is one place where the differences in these programming languages shows up.

Defining Arrays

When writing programs you work with data and use variables as a way to store that data. Arrays provide an essential way to define a single variable capable of holding a sequence of similar data. By using arrays you give the computer an easy way to navigate through each of the pieces of data programmatically.

I received the following question about array initialization:

It is my understanding that…
{
int[] array = new int[3];
array[0] = 1;
array[1] = 2;
array[3] = 3;
}
AND
{
int[] array = {1, 2, 3};
}
…have the same effect. I am simply curious as to why the second example does not require the new function (also, would new be considered a function?).

Array References:

Processing Keywords

First a word about the Processing language’s keywords. When I pasted the supplied source code into Processing and pushed Run, the code ran without complaining about using “array” as the variable name. Using keywords as variable names can cause problems and should be avoided. One way to identify keywords is by the color coding in the editor. The word “array” is shown using the same color as other keywords “int” and “new”.

 

Invalid variable name.

Processing keyword used as variable name.

Array Declarations In Processing

In the following screen shot I’ve used the variable names “myArray”, “anotherArray”, and “vintageArray”. These variable names show up as black text in the editor.

 

Valid Processing Array Declarations

Valid Processing Array Declarations

int[] myArray = new int[3];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

int[] anotherArray = {1, 2, 3};

int vintageArray[];

To understand the methods of allocating a variable array it helps to know that Java is an object oriented language. Furthermore, all variable types in Java are objects. The first declaration:

line 01. int[] myArray = new int[3];

Using the keyword new is consistent with many object oriented languages as the way to allocate an instance of an object class. The new keyword is not a function, it is an operator. This feature in Processing is a result of using the Java language. The following breakdown shows how this statement is evaluated:

  • int[] identifies the variable type will be an integer array.
  • myArray is the variable name.
  • new is the operator used to allocate a new object type.
  • int[3] identifies the object class and the number of array positions.

Two things happen in the example code’s variable array declarations.

  • Allocate
  • Initialize

This statement on line 01 only allocated positions in your program’s memory to hold the data values, it is not initializing the data.

The array declaration on line 06 combines allocation and initialization of each array position value.

line 06. int[] anotherArray = {1, 2, 3};

This is a shortcut in the way this object is being declared. The Java back-end understands the shortcut and doesn’t require the new keyword.

The Processing language reference doesn’t describe the method on line 08 as shown when declaring the vintageArray variable.

line 08. int vintageArray[];

This method is available from Processing and Java’s heritage in the C language.

Array Declarations With Arduino


int myArray[3];
int initializedArray[] = {1,2,3,4,5};
int partialArray[10] = {1,2,3};
int *ptrArray;

void setup()
{
}

void loop()
{
}

Arduino differs from Processing by using the C language definitions for arrays.  From the array declarations with Arduino source code:

Line 01. int myArray[3];

The major change is where the square brackets “[” and “]” are placed. In the C language they are found just after the variable name as in Array[3] above.

Line 02. int initializedArray[] = {1,2,3,4,5};

This line allocates the variable initializedArray to allow up to five array positions. It then initializes the values with the numbers 1 through 5 in consecutive array positions.

Line 03.  int partialArray[10] = {1,2,3};

This line allocates the variable partialArray to allow up to ten array positions. It then initializes the first three values with the numbers 1, 2, and 3 in consecutive array positions.

Line 04.  int *ptrArray;

The statement on line 04 is a valid C language allocation of a pointer named ptrArray. The use of pointers is a topic for another article. See Some Pointers For Memory Allocation.

(c) 2009 – Vince Thompson