Integers, Floats, and Doubles
It is going to happen. You will be writing a program and you will want to store some data. For this purpose Java has variables.
Variables are like special containers that hold a value for us. There are three parts to a variable; the type, the variable name, and the value the variable is storing. The type of variable refers to the type of data the variable can store, like a number, word, character, or even other data.
The first type of variable I will introduce you to is the byte. First create a new text file called MyVariables.java. Then in it write this code:
public class MyVariables { public static void main(String[] args) {byte myCoolVariable;
myCoolVariable = 5;
System.out.println(myCoolVariable);
} }
Once we compile and run this program, the output will be “5”.
How it works… After the main part of our program, we declare a variable of type byte. We gave it the name “myCoolVariable.” Then on the next line, we assigned it the value of 5. And finally we used the println() method to output the value of “myCoolVariable” to the screen.
A byte is a nice little variable type used for storing whole numbers in the range of -128 to 127. Why can’t you store larger numbers with this variable type?
When we use variables, we also use computer memory. A byte uses 1 byte, or 8 bits, of computer memory. That is only enough memory to store a small number. To hold a larger number, we can use a variable of type short.
A short uses 2 bytes of computer memory, so it can hold a much larger number. A short can store a whole number in the range of -32,768 to 32,767. Not large enough for you? Then you need a variable of type int.
An int uses 4 bytes of computer memory, so it can hold an even larger number. An int can store a whole number in the range of -2,147,483,648 to 2,147,483,647. Still not large enough? Then you need to use a variable of type long.
A long uses 8 bytes of computer memory, so it can hold astronomical numbers. A long can store a whole number in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807!
So now you can store whole numbers. But what about partial numbers that have a decimal point? For that, there is a special type of variable called a float.
A float behaves a little bit differently than the other types of variables. Let’s take a look at one with our “MyVariables” program.
public class MyVariables { public static void main(String[] args) {float myCoolVariable;
myCoolVariable = 5.2f;
System.out.println(myCoolVariable);
} }
Here we have changed myCoolVariable to type float. Then we assigned it the value of 5.2f. Notice that “f” at the end. It is not case sensitive. You could use “F” at the end. But either way, you need the “f” to tell the compiler that this is a floating point literal value. If you try to use just “5.2” you will get an error because “5.2” is a double. You can’t assign a value of type double to a variable of type float.
A float uses 4 bytes of computer memory. It is a little bit harder to define the number range for this type of variable as the decimal value will drop off when the float runs out of bits. If you have no value on the left side of the decimal point, you can get an output accurate up to the eighth decimal place. For example, a float with the value of 0.12345678f can output “0.12345678”. But if you have numbers on the left side of the decimal point, you begin to lose accuracy. A float with the value of 321.12345678f will output something like “321.12344”. As you can see, some of the numbers were dropped off and not even properly rounded.
A double is also a type of floating point value. It uses 8 bytes of memory and therefore twice as precise as a float. When working with decimal values, it is common practice to just use a double.
Comments
Post a Comment