Java Data Types - Numeric Data
Written by Alex Armstrong   
Wednesday, 06 February 2013
Article Index
Java Data Types - Numeric Data
Literals and Expressions
Types of Assignment and Casting
Numeric Output

After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

 cover

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The User Interface - More Swing
  6. Working With Class
  7. Class Inheritance

We already know about variables and how to store data, but we need to look more closely at what types of data Java works with. In many ways the need to consider the "type" of data is something of a throwback to the early days of computing when we worked very closely with how the data was stored. Back then it mattered how much storage you used and different ways of storing the same data were used simply because of efficiency considerations - why use two bytes when one will do? Today such issues are far less important but we still work with the basic data types that were introduced then.

There are also some more theoretical reasons why we should make use of a range of data types and select the ones that just suit what we are trying to do, but all of this become much clearer after we have seen some real examples.

Numbers

The most primitive data type in any computer language is number. You can store a number in a variable but there are different formats used to represent a number and each format takes a different amount of storage. We looked at the basics of numeric types in the chapter Java - Command Line Programs, but there is more to say.

The simplest format records a number with no fractional part,  i.e. it stores integers. When you store an integer you can opt to use one byte, two, four or eight bytes. Obviously the smaller the amount of storage you use the smaller the numerical range you can use.

The standard Java integer data types are:

  • byte 1 byte  -128 to -127
  • short 2 bytes  -32,768 to 32,767
  • int 4 bytes -2,147,483,648 to 2,147,483,647
  • long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80

You can see that byte is rather restrictive and long should be more than enough for anything - although you will find that in computing nothing is ever big enough.

If you want to use an integer type then the default choice is generally int, which gives a good trade off between efficiency and range. Use one of the others if you know that the numbers you want to use are going to be outside of the range of int. You can also select byte or short as a way to tell other programmers what you expect the range of values to be - although documentation or comments would do the job just as well.

To declare a variable of one of the integer types you would use the form:

type variable = initial value;

where the initial value is optional. For example, to declare an int you would use

int myInteger=100;

or

int myInteger;

To declare a byte variable you would use:

byte myByte=127;

If you try to store a value bigger than a variable of a given type can store then you will either be warned at compile time or when you run the program. You can opt to reduce the range or precision of a value so that it will fit into the variable using a cast - more of which later.

In practice the amount of memory a single variable takes up isn't an issue but when you are allocating lots of variable of the same type, in particular when you create an array (see later) this can become an issue.

Floating Point

Of course you probably do want to use numeric values that have fractional parts. The format most often used to store fractional numbers is called "floating point" for reasons that we don't have to go into to make use of it. A floating point variable can store a numeric value with a fractional part but you need to be aware of the range that can be stored and the precision.

Java has two floating point types and trying to pin down the precision and range that they can represent is difficult. Instead it is better to think about the approximate number of digits of precision they provide:

  • float 4 bytes 7 decimal digits
  • double 8 bytes 16 decimal digits

Unless you really need to save storage for some reason using double is and should be the default - 7 digits of precision is not a lot and the extra storage is well worth using.

As in the case of the integer types floating point types are declared using:

float myFloat=0.123;
double myDouble=0.123;

Notice that floating point types are not suitable for recording exact values over a large range such as currency. If you need to do exact financial calculations then you need to use more advanced data types - such as the BigDecimal class. These are not primitive data types but classes that implement more sophisticated ways of representing and storing numbers.

Most programming, however, can get by with integers and floating point formats.

Summary

There are six numeric types four integer and two floating point:

  • byte 1 byte  -128 to -127
  • short 2 bytes  -32,768 to 32,767
  • int 4 bytes -2,147,483,648 to 2,147,483,647
  • long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80
  • float 4 bytes 7 decimal digits
  • double 8 bytes 16 decimal digits


Last Updated ( Wednesday, 06 February 2013 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.