チュートリアル | (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.";
変数に関するより詳細な情報はJava公式チュートリアルページを参照してください。
// 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";
変数の名前は他の人が読んでも分かりすいものが好まれます。 そのため、一般的に変数名が長めになっても2番と3番の規則が好まれます。 しかしJavaやProcessingでは3番の規則は一般的でなく、このチュートリアルでは 1番と2番の規則を場合に応じて用います。
;
)で区切られる記述は命令文(ステートメント)と呼ばれ、命令の最小単位です。
セミコロンが書かれない場合、Processingはその文が次の行に亘っていることを期待します。
初心者はセミコロンを文末につけるのを忘れることが多いので、忘れないように気をつけてください。
// 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. */
プログラミングのコードは、他の人が読むときや、しばらくしてから自分で読み直すときにわかりやすいように、 常に多くのコメントを残して、どのような動作を意図してコードが書かれたかを記述することが推奨されます。