Java Reference
In-Depth Information
Java does try to keep you from mixing apples and oranges. Although it doesn't
know that you're using an int as a “human age,” it does know the difference
between things like an int , a float , a String , and so on. And it won't let you mix
up those types. If you've declared that a variable should hold an integer, you
can't try to store a String in it. This will throw an error:
int age = "Old enough to know better" ; // Error!
We tried to store a string of characters in an int . That won't work, and Java
will complain. You can't go the other way either. Here we're trying to store an
integer value (42) into a variable declared to hold a String :
String answer = 42;
// Error!
But for common types such as numbers and strings, there are ways you can
convert things back and forth as needed, if it makes sense. For instance,
suppose you read a numeric value from something the user typed, and it was
given to you as a String named str . You can make it an int like this:
String str = "1066" ;
int value = Integer .parseInt(str);
// value is now set to 1066 as a number
So you can convert, but you have to do it yourself; Java won't guess for you.
There's a list of common type conversions, including ones we haven't covered
here, in Appendix 5, Cheat Sheets , on page 241 .
Let's try that out.
Plugin: BuildAHouse
I've got a plugin already set up for you; all you need to do is declare some
variables and you can give the /buildahouse command.
First make your way to the downloaded code, and into the BuildAHouse plugin:
$ cd Desktop
$ cd code/BuildAHouse/src/buildahouse
$ ls
BuildAHouse.java MyHouse.java
You're going to edit the file MyHouse.java , which is one small part of this whole
plugin (don't look at the rest yet!). Right now it looks like this:
 
 
Search WWH ::




Custom Search