Almost all computer languages require the user to distinguish between
things like integers, real numbers and strings. While VB has its all
VBScript do very little to distinguish data types, these are the
There is all kinds of data
Even within this range of types of data one needs to consider issues about the precision of the representation within the computer. Things that are true or false require nowhere near as much memory to store as the numbers associated with space travel.
But this motivates the classification of data into what are usually called data types.
In Java there are some standard data types listed in the following tables. We also present some information about the ranges of representation and the amount of memory that is used.
First we list what are called the primitive data types. Then we will present some derived data types that are worked with in their own classes.
|Numeric Data Types|
Range of Values
|byte||-128 to 127||1 (8 bits)|
|short||-32768 to 32767||2 (16 bits)|
|int||-2147483648 to 2147483647||4 (32 bits)|
|long||-922337203684547758808 to 922337203684547758807||8 (64 bits)|
|float||3.4 E-38 to 3.4 E+38||4 (32 bits)|
|double||1.7 E-308 to 1.7 E+308||8 (64 bits)|
|Character Data Types|
Range of Values
|char||0 to 65535||2 (16 bits)|
|Logical Data Types|
Range of Values
|boolean||true or false||1 bit|
|From these primitive data types, more complicated data
types can be developed. For example, dates, time and strings.
In Java, times are dealt with inside a Date class. That is date objects are used to represent both dates and times. There are also a number of classes to do things like format dates. These classes have a large variety of methods and properties for working with dates and times.
Strings are built from series of characters. They are worked on within the String class. This class has a large variety of methods and properties for working with strings.
A Program for Integers. The following Java application just requests that the user input two different integers. It then computes their product and display the result.
public class IntegerApp
} // end class IntegerApp
|Notice that this application has the same basic
structure as our last application.
But there are also differences.
You need to follow the usual steps to run this program.
Then you should get the following sequence of message boxes, though you are unlikely to choose the same numbers as I did.
First the two input boxes.
|Then the message box displaying the inputs and the solution.|
|Now, for the sake of seeing how easy it is to make
functioning programs blow up, you should run the program and try inputting
You should notice that each of these pairs of inputs causes the program to stop functioning. None of the inputs can be parsed into integers in the program even though they can be read in as strings. We will address these sorts of issues later.
A Long Application. Well, if you think this application is long, then wait until later in the semester. Actually, it is a slight modification of the preceding application where we attempt to be able to handle bigger integers.
You should save the following file as LongApp.java.
public class LongApp
} // end class LongApp
|Now you should try compiling and running this
application using the following inputs.
Notice how you get the following answer for the first pair, but nothing but errors for the others. Some integers are still too large to be represented in a computer using standard primitive data types.
|A Float Application.
Now we will modify this application one more time so that you can use
floating point or real number inputs. The changes shouldn't be
Save this file as FloatApp.java.
public class FloatApp
} // end class FloatApp
|Now you want to compile and execute the program using
the following pairs of inputs.
For the first pair you should get the following result.
|For the second pair you should get an
answer but it will be in scientific notation
using exponents. The 3rd and 4th options should cause the program to
Finally, you should know that if you want to be able to deal with greater precision and/or much larger floating point numbers you should change all the
in order to work with double precision numbers. The lower case double represents the primitive data type. The upper case Double represents the class that works with these data types.
It should make sense that in order to parse the string entries into doubles you will use a command like the following.
firstNumber = Double.parseDouble(firstUserEntry);