Java Reference
In-Depth Information
The important part here is that you're telling Java the type of the variable
you want to make—in this case, a whole number (no fractional part or decimal
point). A whole number is known as an integer, which is abbreviated in Java
as int .
There are lots of types in addition to int , including float and double , which hold
fractional numbers (with decimal points); String , which holds a sequence of
characters such as "Hello,World!" ; types you can create yourself, such as NunChuck
or CowBell ; and types that Minecraft defines, such as Player and Location . Any
time you declare a variable in Java, you need to specify the type for that
variable. Java can't guess; you need to tell it.
You can create the variable and assign its value in one step, or create the
variable first and then assign it later. In either case you can put a new value
in that variable any time you want.
This is okay:
int age = 15;
age = 39;
age = 21;
However, you can't declare it a second time. This won't work:
int age = 15;
int age = 21;
// Error!
Instead, only declare it as int age once, then use age = to change the value if
needed.
Now, just because you “labeled the box” age doesn't mean it really has to hold
an age. There's nothing to stop you from putting some other number in that
box:
int age = 2048;
That's perfectly legal Java code, as 2048 is a perfectly reasonable number. It
might be okay for the age of a historical relic, but it's not a realistic age for a
person. Setting that age for a person would be stupid, but there are no laws
against stupidity. That means it's up to you to give variables names that make
sense. Renaming this variable to something like ageInYears might make more
sense, to avoid problems like the famous crash of the Mars climate orbiter,
where one set of programmers used metric units and the other used Imperial
units. Oops. 1
1. http://mars.jpl.nasa.gov/msp98/news/mco990930.html
 
 
 
Search WWH ::




Custom Search