home processing download documents tutorial python tutorial gallery source about
 Tutorials (back to the list of tutorials)

For Beginners of Processing and Java

     Variables

Variables are used to store data. Each variable has its own type. Processing (and its parent language Java) has several "primitive" types which is used used frequently and basis of other data types.

// Integer
int i=0;
int num1 = 100, num2 = -5;

// Float (floating point number)
float a = 0.0;
float minimum = 0.00001;
float fval = 10f; //'f' at the end specifies to be float even when number looks like an integer

// Double (double precision floating point number)
double value1 = 0.5;
double value2 = .1;
double value3 = 0.; //'.' specifies to be double
double value4 = 1d; //'d' specifies to be double
double x = 0., y= -1.414214, z = 2.236;

// String (strictly speaking String is not a primitive type but a class, but it's a very fundamental class in Java.)
String txt = "word"; //sandwiched by double-quotation
String happyMsg = "The result was successful.";
String sadMsg = "The result was a total failure.";

For more extensive information, please see the official Java tutorial page.


     Variable Naming Convention

There are several different naming conventions existing for the variable name.
  1. To name a variable with a decently short and very concise name to ease the work of typing.

  2. To name a variable with descriptive words. When the name has multiple words, it's typical in Java to concatenate words having the first letter of each word in capital, except the first word.

  3. To name a variable with short name of type in the beginning like "int" for int, "dbl" for double, "str" for String, and then descriptive words follows in the same way with #2.

// Naming Convention #1
int i=0;
int xnum=100;
double x=10.0;
double len=20;
double dist1=10, dist2=100; // without '.' it can still be double, actually
String errMsg="no data found";

// Naming Convention #2
int pointCount=10;
double xOffsetDistance=20.0;
double angleOfArc=2.5;
double sumOfTotalXShift=10.24;
String errorMessage="still no data found";

// Naming Convention #3
int intCounter=0;
int intTotalYNumber=10;
double dblPointSize=5.5;
double dblVectorLength=2.2;
String strErrorMessage="please search somewhere else";

The bottom line is that a name of variable should not be confusing. Many people recommend #2 and #3 but #3 is not very common in Java and Processing's and iGeo's system variables are named in somewhere between #1 and #2.


     Statements

A statement is a line you've already seen in the example codes above ending with semicolon(;). Missing semicolon at the end is a common mistake in beginners. Please don't forget. One statement can span multiple lines.

// statements spanning multiple lines
int xnum = 10,
  ynum=20, znum=30;

double intervalBetweenPlanes =
       20.5;

int total =
  xnum +
  ynum +
  znum;


     Comments

You can write comments in the code with // as you've already seen in the example codes. // comments out text after the sign until the end of line. Commented out texts has no effect on codes. There is also /* ... */ paired signs to comment out multiple lines or a part of line in the middle. You can nest // inside /* ... */ but you cannot nest /* ... */ inside /* ... */.

// comment example 1

int i=-10; // comment example 2

double x=20 /* comment about x */ , y=-10 /* about y*/ ,
   z = 20 /* about z*/ ;

/*
 Multiple-line commenting sentences.
 Longer description about something 
 about the code below. 
*/

/*
 This is OK to have
 // this comment
 inside this comment.
*/

/*
 This is NOT OK to have
 /*
   this comment 
 */
 inside this comment.  
 This will cause error.
*/

It's highly recommended to add comments to your codes constantly because it'd make the code easier to be read not only by others but also by yourself maybe several days later when you don't exactly remember the intention of what you were doing.


(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
ABOUT