Java Reference
In-Depth Information
Java Language
Declare the package
package name.name.name;
, usually using the inverse of your domain name,
so a project
Wonderful
from the guys at pragprog.com would be in the
package
com.pragprog.wonderful
.
Import a class
import net.canarymod.api.entity.living.humanoid.Player
tells the Java compiler you
want to use this class. You can use a wildcard of “*”—as in
inet.canary-
mod.api.entity.living.humanoid.*;
—but you may get more uninvited classes than
you wanted.
Declare a class
publicclass
name
{}
.
Declare a function
public return-type name
(
argument types and names
), as in
publicintmyFunction()
.
Declare a block of code
Use braces,
{
and
}
.
Declare variables
type name
; as in
inti;
or
Stack<Location>myLocations;
generics need the specific
type(s) added within the angle brackets
<
and
>
.
Assign values to variables
a=10;s="Bob";x=3.1415;
.
Make decisions
Decide which code to run using an
if
(the
else
{} part is optional):
if
(true or false comparison) {
// is true
doThis();
}
else
{
// is false
doOtherThing();
}
Loops
There are three ways to make a loop around a block of code:
•
for-each construct:
for(
type variable
:
collection
)
Example:
for(Playerplayer:
playerList) {
block
}
•
for
loop:
for(inti=0;i<
limit
;i++) {
block
}
•
while
loop:
while(
somethingIsTrue
) {
block
}