Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Friday, February 8, 2013

Week-2: 2.3: Variable : Float and Short

Two more variable types you can use are short and float. The short variable type is used to store
smaller number, and its range is between minus 32,768 and plus 32,767. Instead of using int in
our code on the previous pages, we could have used short instead. You should only use short if
you're sure that the values that you want to hold don't go above 32, 767 or below -32,768.

The double value we used can store really big numbers of the floating point variety. Instead of
using double, float can be used. When storing a value in a float variable, you need the letter "f"
at the end. Like this:

float first_number, second_number, answer;

first_number = 10.5f;
second_number = 20.8f;

for Details

Week-2: 2.2: Variable : Double


The double variable can hold very large (or small) numbers. The maximum and minimum values
are 17 followed by 307 zeros.

The double variable is also used to hold floating point values. A floating point value is one like
8.7, 12.5, 10.1. In other words, it has a "point something" at the end. If you try to store a floating
point value in an int variable, NetBeans will underline the faulty code. If you try to run the
programme, the compiler will throw up an error message.
Let's get some practise using doubles.
Change the int from your previous code to double. So change this:

int first_number, second_number, answer;
to this:
double first_number, second_number, answer;
Now change the values being stored:
first_number = 10.5;
second_number = 20.8;

Visit for Details

Week-2: 2.1: Variable: Integer


Programs work by manipulating data placed in memory. The data can be numbers, text,
objects, pointers to other memory areas, and more besides. The data is given a name, so that it
can be re-called whenever it is need. The name, and its value, is known as a Variable. We'll start
with number values.

To store a number in java, you have lots of options. Whole numbers such as 8, 10, 12, etc, are
stored using the int variable. (The int stands for integer.) Floating point numbers like 8.4, 10.5,
12.8, etc, are stored using the double variable. You do the storing with an equals sign ( = ). Let's
look at some examples (You can use your FirstProject code for these examples).

Click for Details