Minecraft Blogs / Tutorial

Java Text Tutorial 5 - Other Variables

  • 218 views, 2 today
  • 1
  • 0
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
9
And we're back! Welcome to episode 5 of Java Text Tutorials!



Table of  Contents:
5. There are more types of variables?
     5.1 Explaining Each Type
          5.1.1 Boolean
          5.1.2 Double
          5.1.3 Float
          5.1.4 Long
          5.1.5 Short
          5.1.6 Byte
          5.1.7 Char
     5.2 How are these helpful?



5. There are more types of variables?
Yes. In all simplicity, yes. There are multiple types of variables. Some you should already be familiar with are integers (int) and strings (String). But there are more. There are seven new ones:
1. boolean
2. double
3. float
4. long
5. short
6. byte
7. char

5.1 Explaining Each Type
This section is divided up into subsections for easy reading and to make it more organized. Each subsection explaines one variable type.


5..1.1 Boolean
A boolean is very simple. It can only be true or false. Nothing else can be applied to it. If you do try something like a number or a string it will throw an exception. Therfor these are relatively easy to use. They are setup like this:

boolean justAnotherBoolean = true;

Remember the semicolen as always. What this is saying is that its declaring a variable of type boolean and calling justAnotherBoolean and setting it equal to true.  This is useful for checking if something is true. If you remember from the last tutorial, we had a boolean called isShot and set it equal to true. However if we had expanded on that, whenever a projectile such as a bullet hit the player we could set isShot to true. It was pretty basic and it could not work in a normal game but if we expanded upon it, it could of had been. After we had subtracted the players health we would have had to set it back to false so that the player wouldn't die ;). But before this gets too complicated (it already has) lets get back n track. A boolean can only be true or false.


5.1.2 Double
Doubles are almost the same thing as integers with the exception that they don't need to be whole numbers. If you tried this:

int anInteger = 1.27;

It would throw an error. That is because integers cannot have decimals. That is where double comes in hand. It is useful if you have an integer but you want it to have decimals. Because it is so similar to integers, I will not go into too much detail on that. But here is an example of how one would look:

double aDouble = 1.27;

And that would work.


5.1.3 Float
Floats are a strange topic for me. They are very similar to doubles except the number always end in f. In all technical terms, double is a 64-bit percision IEEE 754 floating point where a float is a 32-bit percision IEEE 754 floating point. What does this all mean? You don't actually need to understand all of the technical terms. What you do need to know is that floats are less precise than doubles. They usually run slightly faster than doubles but are less precise, so don't go trying to make pi a float. Here is an example of a float:

float justAFloat = 1.1f;

Remember the f after the number.


5.1.4 Long
Longs are almost the same thing as doubles (so many references to doubles).  Except longs can be much longer than doubles. If you wanted to write 1000 squared you could (one million ;) ). Longs also have a limit, but I wouldn't worry about that unless you're insane with numbers (no offense :P). Longs usually take longer to load than doubles - not too long - so try not to use them unless you need a very large number.  They are written like this:

long justAnotherLong = 1000000L;

Remember to include a capitalized L at the end to show Java that it is a long not an int


5.1.5 Short
Short is a very small integer. This is one of the ones I use the least. This one is also very strange. It can only have whole numbers and has a very small range of numbers you can use. You only will want to use it for smaller numbers. The thing I find strange is for a short you need to declare a cast for it to work properly (correct me if I'm wrong but I cannot think of another way). This is how a short would look:

short anotherShort = (short) 1000;
.
The cast is: (short) This will be covered in a later topic, likely much later.


5.1.6 Byte
Bytes are very small. According to Oracles JavaDocs a byte can be "useful for saving memory in large arrays, where the memory savings actually matters". This one is probably my second least used. An example of one is here:

byte a = (byte) 1;

Sorry for not much info, but even I am not to aware of how they fully work as I do not really use them.


5.1.7 Char
Chars are used to store single characters in a variable. Very useful for storing characters that are not on a reguar keyboard. Here is an example of one:

char a = 'a';

This one is obviously on your regular keyboard but they make it easier to use stranger symbols without copying and pasting a thousand times.

5.2 How are these helpful?
For any good game/prgram these are very essentials. Now you don't need to use every single one of them, but rather a combination of them for whatever your making! If you remember in the last tutorial we were adding variables. These should essentially work in the same manner. The most common ones you will use will be int, String and double. Sure you may need to use the others, that's just fine! Now go ahead, try them out for yourself!

In the next tutorial we will talk about methods and constructors.
CreditImage From Oracle(www.oracle.com)
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome