The use of variables in Java is not simple. Depending on which type of variable different rules need to be followed.
int | Integers between -231 and 231-1 |
long | Integers between -263 and 263-1 |
double | Decimal number with double precision |
boolean | Can only have the value of 1 (true) and 0 (false) |
char | Characters (e.g. a key of the keyboard) |
Before using a simple variable, its name, data type and value must be set. This is called declaration and initialization of the variable.
The declaration of a variable assigns the name and the data type and saves the necessary space in the memory.
The initialization assigns a value to the variable.
The declaration and the initialization can done in one step:
int i = 0;
double x = 3.14;
boolean odd = true;
The following rules need to be obeyed when assigning a variable name:
// Tu12.java import ch.aplu.turtle.*; public class Tu12 { Turtle t = new Turtle(); public Tu12() { double s = 140; while (s > 5) { square(s); t.left(180); s = s * 0.9; } } void square(double size) { for (int i = 0; i < 4; i++) { t.forward(size); t.right(90); } } public static void main(String[] args) { new Tu12(); } } |
The next two example shows the difficulty of the visibility of instance and local variables. The first example, Tu13a, does not compile correctly, because the variable i is declared in the block of the for-loop and so only visible there. It is a local variable and can not be seen by the block after the loop (joe.forward(10 * i)). If i had been declared outside the for-loop, as in the second example Tu13, the program would compile and run without any error.
// Tu13.java import ch.aplu.turtle.*; import java.awt.Color; public class Tu13 { Turtle t = new Turtle(); public Tu13() { t.hideTurtle(); t.clear(Color.blue); t.setPenColor(Color.yellow); for (int i = 0; i < 50; i++) { double x = -180 + 360 * Math.random(); double y = -180 + 360 * Math.random(); star(x, y); } } void star(double x, double y) { t.setPos(x, y); t.fillToPoint(x, y); for (int i = 0; i < 6; i++) { t.forward(10); t.right(140); t.forward(10); t.left(80); } } public static void main(String[] args) { new Tu13(); } } |
In this last example the Turtle joe is not declared as an instance variable, but as a local variable inside the constructor. If the method square(Turtle t) did not receive the Turtle as a parameter it would not compile correctly, because the variable joe is local and so only visible in the constructor. But since the square-method does receive it as a parameter it compiles and runs perfectly.
// Tu14.java import ch.aplu.turtle.*; import java.awt.Color; public class Tu14 { Turtle t = new Turtle(); public Tu14() { t.speed(-1); t.wrap(); t.fillToPoint(0, 0); for (int i = 0; i < 200; i++) { Color c = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), 255); t.setPenColor(c); if (Math.random() < 0.5) t.right(90); else t.left(90); t.forward(60 * Math.random()); } t.hideTurtle(); } public static void main(String[] args) { new Tu14(); } } |
Either the variable joe is declared as an instance variable outside the constructor or the method square() must receive the parameter (Turtle t). All actions inside the square-method are using the parameter t. When calling the square-method with square(joe) inside the constructor the parameter joe will be sent to it.