home processing download documents tutorial python tutorial gallery source about
 チュートリアル (back to the list of tutorials)

ProcessingとJavaの基礎

     変数

プログラミングにおいて変数は或るデータを保持し、あなたがそのデータを繰り返し参照できるようにします。 変数は更にそのデータの型情報を保持し、そのデータの取扱い方をあなたとコンピュータがプログラム実行前に推測できるようにします。 ここではProcessingでのプログラミングに頻出する型と、変数としてのそれぞれの取り扱い方を紹介します。

// 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公式チュートリアルページを参照してください。


     変数の命名規則

There are several different naming conventions existing for the variable name. 変数の命名方法は、特殊記号を使わない限り基本的には自由に決定することができますが、慣習的にいくつかの 規則が存在します。
  1. コーディングの手間を省くために、できるだけ手短に簡略に名前をつける。

  2. 読んだ時にわかりやすいように、できるだけ記述的に名前をつける。 複数の語を組み合わせてひとつの変数の名前を付けるときには、最初の語の先頭は小文字、以後の語の先頭は大文字にする。

  3. 変数の先頭に、変数の型を示す文字をつける。(例えば"int"をint型に、"dbl"をdouble型に、"str"をString型変数の先頭につける。)

// 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.
*/

プログラミングのコードは、他の人が読むときや、しばらくしてから自分で読み直すときにわかりやすいように、 常に多くのコメントを残して、どのような動作を意図してコードが書かれたかを記述することが推奨されます。


(back to the list of tutorials)

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