|Java - Command Line Programs|
Page 2 of 4
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:
where type is one of the standard variable types -
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.
defines two integer variables and
defines three single precision floating point numbers.
You can store whole numbers in the integer variables:
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:
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
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:
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.
displays 10 on the console but:
The difference is that 010 is 8 in octal.
A character constant is surrounded by single quotes, e.g.
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.
is illegal but
You can also initialize variables when you declare them as in:
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:
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.