Basic Java Syntax
Java syntax is based on C++.
Some introductory notes follow, to learn more, read the tutorial from Sun.
- Instruction statements all terminate in ;
- Whitespace (newline, tab, space) is ignored by the compiler except within strings.
So you can split statements over many lines if you like.
- Anything after // is a comment which is ignored, up to end of line. Comments of any length can also be enclosed /*like this*/
- Capital letters matter, Fred and fred are different!
- Brackets:
{} for program loops
() for method parameters (as well as in mathematics)
[] for array indices
- You must specify the type of all new variables.
Basic types are: byte, short, int, long, double, float, boolean
But a type can also be any class you like, including your own classes.
Some simple examples:
Make an integer a and set it to equal 5
int a=5;
Make an array of 3 integers and set the first one to 5
int[] a = new int[3]; a[0]=5;
Note array indices start at 0, so code below will give an error: "array index out of bounds"!
int[] a = new int[3]; a[3]=5;
Make a=5, pass this to a method to calculate b
int a=5; int b=calcsquare(a);
And here's that method which could be elsewhere in the code
int calcsquare(int x) {int y=x*x; return y;}
(note a is picked up as x, and b is returned from y, 25 in this case)
Note that methods are declared using:
return-type methodname(parameter-type parameter-name) {dosomething... ;}
return type can be "void" -in which case it doesn't return anything
Strings (text) are objects in java, but with special properties that you can make a new one with quotes, and can add them
String a="good"; String b=a+" morning";
Note:
a=b is an assignment (sets a to be the value of b)
a==b returns a boolean true/false for use in "if(a==b)" etc.
! means not, so a!=b returns true if they are not equal
i=i+1 , i+=1 , i++ are all the same
Flow control statements may use both () and {} brackets, for example
if (a==b) {dothis(a); andthis(b);}
start with i=0, keep looping until i<n, add one to i at each step
for(i=0; i<5; i++) {j+=i; k+=j;}
Note the following looks confusing but is ok!
Polygon[] polygon=new Polygon[5]; polygon[0]= new Polygon();
this means make a new array called polygon, of 5 objects with class-type Polygon, and set up the first one using the default constructor method Polygon()
Dots are used to connect objects or methods to their parent objects, e.g.
model.mod[5].calcsealevel(model.mod[4].temp[2002]);
You could achieve the same by writing:
m=model.mod; m[5].calcsealevel(m[4].temp[2002]);
Note mod represents the whole array
Arrays can have as many dimensions as you like.
You can initialise arrays using {} brackets, useful for data:
td[][]={ {5,6,7,8}, {1,2,3,4}, {9,10,11,12} };
Putting variable types in brackets converts from one kind to another
The compiler insists you specify this, whenever you might lose information, e.g.
double a=5.4; int b=(int)a; //now b=5, compiler would give an error without (int)
int b=5; double a=b+0.4; //here you didn't need (double) because nothing is lost
Note that "1" is an int, "1.0" is a double, "1.0f" is a float
Calculations tend to preserve the type of the first variable,
so 4.1/3 gives 1.36667 whilst 4/3 gives just 1 (not 1.33333)!
Note if pan is an array of panel objects, graph class extends panel class, and xscale is a method specific to graph class, then the following also works as a type conversion:
if (pan[p] instanceof graph) {thisgraph=(graph)pan[p]; thisgraph.xscale(1900,2100);}
Beware regarding method parameters:
mymethod(x);
void mymethod(variabletype y) {dosomething...;}
If variabletype is a simple number (int, double etc.) or boolean (true/false), this passes just the *value* of x, so if mymethod changes y this won't affect x elsewhere in the code.
But if variable type is any more complex object, it passes the *reference* (imagine location in memory), so if mymethod changes a property of y, this will affect the original x too!