Showing posts with label floating-point. Show all posts
Showing posts with label floating-point. Show all posts

Monday, February 25, 2013

What is a variable?


Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.

Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.

There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.

For instance, in a Java program, we could do the following:

String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;


The words String and int are types of Java variables - or data types.

Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.

Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.

String whatTheDogSaid = “Right on partner number 2!”;

A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.

Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.

int numOfStudentsOnBus = 22; 
double dogWeight = 12.2;

The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.

A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.

There are many data types in the Java language!

Here's a cat:


Sunday, June 3, 2012

the double data type

A double is a numeric data type. Suitable for storing floating point (decimal) numbers such as weight, length, etc. Any kind of number with a "point something' in it such as 7.6, 10.5, 1.2, etc.

Where a double has not been assigned a value, by default it's value is 0.0d.

The format for declaring a double variable is:

   Type identifier;

Where the Type is double and the identifier is weight:

   double weight;

...and where a value of 100.50 has been assigned:

   double weight = 100.50;


You can declare a double in one statement and assign it a value in another statement:

   double weight;
   // other statements...
   weight = 100.50;



See also:

"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )
"Java For Complete Beginners - Double variables" ( http://bit.ly/ZzYTJ9 )