Tutorials | (back to the list of tutorials) |
// 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.
// 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 spanning multiple lines int xnum = 10, ynum=20, znum=30; double intervalBetweenPlanes = 20.5; int total = xnum + ynum + znum;
// 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.