Java - Command Line Programs
Written by Mike James   
Article Index
Java - Command Line Programs
Data Types
Operators And An Example
The Complete Program

Java Data Types

Before we can move on to look at the more exciting details of Java, we have to clear up some more of the basics.

In particular, you need to know about what sorts of simple variables and types of data Java supports.

In Java every variable is specified to store just one type of data. If a variable is declared as being a string type which can only hold text then trying to use it to store a number, something you can do arithmetic with, will cause an error.

Notice that this bring us to an important distinction - between what something looks like and what it actually is. You may think that "123" is a number like 123 is a number - but it isn't. Anything between quotes is a string of characters and even if it looks like a number it isn't - its just as much a string of characters as "ABC".  You can't do arithmetic with "ABC" and you can't do arithmetic with "123" - not unless you convert it to a number which is something we will return to. 

This is may seem to be a silly idea and just a way of generating needless error messages, but the idea is that it adds a check on what you are doing. The argument goes that if you start out with a variable that is to be used to work with text and you try to store a number in it then this must be an indication that you have made a mistake and are confused about what your program is doing. 

This approach is called "strong typing" as opposed to weakly typed or dynamic languages.

In Java all variables have to be declared before they are used using:

type name

where type is one of the standard variable types -

 

Type

byte

1 byte

integer

short

2 byte

integer

int

4 byte

integer

long

8 byte

integer

float

4 byte IEEE floating point

double

 

8 byte IEEE floating point

char

 

2 byte Unicode character

boolean

 1 byte true/false

 

Don't worry too much what the different types are all about in any detail as they are easier to understand when you encounter them in action.

Essentially you have four different sizes of integer values, two types of floating point or fractional values, a character type and a Boolean which can be true or false.

Integers are the simplest type of number you can use. An integer is a whole number as opposed to a float or double which always has a fractional part even if it just happens to be a whole number. 

For example

int count, total;

defines two integer variables and

float x,y,z;

defines three single precision floating point numbers.

You can store whole numbers in the integer variables:

count=45:
total=-1;

Notice that you don't specify a fractional part. That is 123 not 123.0.

When you do arithmetic with integers the result is another integer even if division is involved. For example:

a=23; b=10;
c=a/b;

gives the result 2 because 23/10=2.3 which is 2 after you throw away the fractional part. Integer variables really don't have a fractional part and so they don't round or do anything clever they simply throw any factional part away. 

Floats and doubles on the other hand to keep fractional parts of numbers and you can work with them. For example

x=23.0; y=10.0;
z=x/y;

gives the result 2.3 stored in z. Notice the way that 23.0 and 10.0 have a fractional part that just happens to be zero. 

Finally - why are there so many integer types and two fractional types?

The answer for the integer types is that they can hold different ranges of numbers and if you opt for a smaller range of values then the amount of memory used is less. Until you get to some advanced programming there is no reason to worry about saving memory - simply use int when you want to use a whole number.

The same is true for float and double  - double provides increased accuracy over float but uses twice the amount of memory. This again is usually not an issue until you get to more demanding programs. 

Notice that the biggest problem is when to use int and when to use float?

This decision should be a logical one. If the value cannot have a fractional part then always use an int. For example if a variable is counting how many times something can happen use an int because there is no logical sense that something can happen 1.5 times say. 

There are some other tricks of the trade you need to know. When you use a numeric value then it is treated as a decimal number:

total=1234;

but any number starting with a 0 is treated as an octal constant and any starting with 0x is treated as a hex constant.

This can be a source of errors so take care not to include a leading zero.

For example

System.out.println(10);

displays 10 on the console but:

System.out.println(010);

displays 8.

The difference is that 010 is 8 in octal.

A character constant is surrounded by single quotes, e.g.

char c = 'a';

Notice that you can only specify a single character in this way. That is, 'ab' is incorrect and generates an error.

If you want to work with a set of characters then you need to use a string constant (more of strings later) that is surrounded by double quotes.

For example:

'abc'

is illegal but

"abc"

is perfectly OK. This is a particular problem if you already know another language such as JavaScript where ' and " mean the same thing.

You can also initialize variables when you declare them as in:

char c=’a’;

which both declares c to be a char and stores the character a in it.

If you want a real example of variables in use try changing the main method in our earlier example to read:

public static void main(String args[]){
 int a,b,c;
 
a=10;
 b=23;

 c=a+b;
 
System.out.println(c);
}

 

Notice that you can put more than one instruction on a line; as long as they are separated by semicolons, Java treats them as separate instructions.

In fact Java really doesn’t care about program layout and strips line feeds and white space out of a program before it compiles it.

Of course, this isn’t an excuse to write messy looking, difficult to read, programs - use good layout at all times and never take a short cut just to save some typing.