Data Types

 

Introduction.  Almost all computer languages require the user to distinguish between things like integers, real numbers and strings.  While VB has its all inclusive data type and scripting languages such as JavaScript and VBScript do very little to distinguish data types, these are the exception.

There is all kinds of data

  • numeric
    • integers
    • real
  • character
    • individual characters
    • words
    • strings
  • dates
  • time
  • logical/Boolean
    • true/false
    • yes/no

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
Data Type

Range of Values

Bytes
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
Data Type

Range of Values

Bytes
char 0 to 65535 2 (16 bits)

 

Logical Data Types
Data Type

Range of Values

Bytes
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.

 

import javax.swing.JOptionPane;

public class IntegerApp
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry;
int firstInteger, secondInteger, result;

// Prompt user for input
firstUserEntry = JOptionPane.showInputDialog("Please enter an integer:");
secondUserEntry = JOptionPane.showInputDialog("Please enter another integer:");

// convert these string values to integers
firstInteger = Integer.parseInt(firstUserEntry);
secondInteger = Integer.parseInt(secondUserEntry);

result = firstInteger * secondInteger;

JOptionPane.showMessageDialog( null, "The product of the values you entered " + firstInteger +
" and " + secondInteger + ", is " + result,
"Integer Product", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

} // end method main

} // end class IntegerApp

 

Notice that this application has the same basic structure as our last application. 
  • The filename and the name of the overall inclusive class are the same
  • We receive inputs from the user using JOptionPane
  • We display results to the user using JOptionPane

But there are also differences.

  • We bring in two different inputs
    • both are brought in as strings
  • They must be converted to integers
  • These integers are multiplied
  • The results, as well as an echo of inputs are displayed to the user
    • notice how a larger mixture of literal strings and variables are concatenated to develop what is displayed to the user

You need to follow the usual steps to run this program.

  1. move to the directory containing the program file
  2. javac IntegerApp.java
  3. java IntegerApp

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 the following.
  1. 23.4  and  35.776
  2. abc  and  def
  3. leave both blank
  4. 1234567890  and  9876543210

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.

 

import javax.swing.JOptionPane;

public class LongApp
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry;
long firstNumber, secondNumber, result;

// Prompt user for input
firstUserEntry = JOptionPane.showInputDialog("Please enter an integer:");
secondUserEntry = JOptionPane.showInputDialog("Please enter another integer:");

// convert these string values to longs
firstNumber = Long.parseLong(firstUserEntry);
secondNumber = Long.parseLong(secondUserEntry);

result = firstNumber * secondNumber;

JOptionPane.showMessageDialog( null, "The product of the values you entered " + firstNumber +
" and " + secondNumber + ", is " + result,
"Long Product", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

} // end method main

} // end class LongApp

 

Now you should try compiling and running this application using the following inputs.
  1. 123456789  and  987654321
  2. 12.3  and 45.3
  3. 12345678900  and 98765432100

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 surprising.

Save this file as FloatApp.java.

import javax.swing.JOptionPane;

public class FloatApp
{

public static void main( String args[] )
{

String firstUserEntry, secondUserEntry;
float firstNumber, secondNumber, result;

// Prompt user for input
firstUserEntry = JOptionPane.showInputDialog("Please enter an integer:");
secondUserEntry = JOptionPane.showInputDialog("Please enter another integer:");

// convert these string values to floats
firstNumber = Float.parseFloat(firstUserEntry);
secondNumber = Float.parseFloat(secondUserEntry);

result = firstNumber * secondNumber;

JOptionPane.showMessageDialog( null, "The product of the values you entered " + firstNumber +
" and " + secondNumber + ", is " + result,
"Long Product", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

} // end method main

} // end class FloatApp

 

Now you want to compile and execute the program using the following pairs of inputs.
  1. 23.56  and  56.31
  2. 123456789  and  987654321
  3. both entries blank
  4. abc  and  def

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 terminate.

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

  • float   to   double
  • Float  to  Double

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);