Bukkit Coding – (9) Variables

If you have any programming knowledge, chances are you already know how variables work. A variable simply stores information inside of it to be used later. In Java, we must first define the class, and then give it a name, and a value. Here’s an example.

In this example, we’re making an int variable named number and giving it a value of 0.

There several important classes that Java has called primitives. They are Floats, Integers, Strings, Booleans, Longs, Shorts, Bytes, Doubles, and Chars. Those are a lot of classes. Depending on what type of information you’d like to store, as well as the size of the value you’d like to store will depend on which class you should use. However, we’ll mostly be focusing on Integers, Strings, Booleans, and possibly Floats, so I’ll go through these one at a time.

Integers – Integers are defined as int and are used to store whole numbers from around -2billion to +2billion (-2147483648 to 2147483647). If you need a larger or smaller number, you’ll have to use the long class.

Strings – Strings are just strings of text. When defining your own string, it should always go inside of quotes. Here’s an example of defining a String.

Booleans – Booleans can only have the value of true or false. It’s values must not be in quotes. Here’s an example of defining a Boolean.

Floats – Floats are used to store decimals. When giving a value to a float, the letter F must be used at the end of it’s value. Here’s an example of defining a Float.

Variables can also be defined separately without giving it a value.

After defining a variable without a value, we can give it a value later.

Likewise, we can also change the value of a variable.

Bukkit also adds some of its own classes. They include Player, Location, Block, Zombie, etc. We’ll get to these more later.

One last thing I’d like to mention, is that you can concatenate variables, as well as add them together. Here are two examples.

Here, we’re setting string1 to “My name is “, and setting string2 to “elfin8er.”. Then, we’re using concatenation to combine the two strings together. string3 will then have the value “My name is elfin8er.”. Notice how we left a space after “My name is “. That’s because concatenation does not create its own spaces.

Attempting to concatenate two numbers, will result in the numbers being added. In this case, number3 will end up having the value of 350 (150 + 200 = 350).

You can read up more about variables by going HERE.

Be the first to comment

Leave a Reply

Your email address will not be published.


*