Java Data Types

Almost everything we do in computer programming involves data. In many cases, the data itself is more valuable then the program. Many tax programs are written, but they all work on the same data. We may write tax software that displays fancy charts of your AGI over a period of 5 years, or we may write a program that calculates your net income. These programs are very worthwhile without data.

Java has two main ways of representing data. The first set of data types are called primitives. They are as follows

  • byte (8-bit signed)
  • short (16-bit signed)
  • int (32-bit signed)
  • long (64-bit signed)
  • float (32-bit)
  • double (64-bit)
  • boolean (true/false)
  • char (16-bit unicode characters)

Whole Numbers

Let’s discuss each of these data types in turn starting with byte. A byte is smallest integer variable available to Java programmers and can hold values between -128 and 127. I generally use bytes in situations where I need to be conscious of my memory usage or when I’m programming on the web and need to transfer information from one site to another.

We can make a byte like this

byte b = 120; //This variable stores the number 120

Next up the ladder is the short. Like bytes, shorts contain whole numbers between -32768 and 32767. Use shorts when you need to be concerned about memory usage.

Create a short by

short s = 120; //This variable also store the number 120

The next primitive data type is int. This is one of the most commonly used numeric data type in Java programs. The int primitive data type stores values between -2147483648 and 2147483647.

Create an int by

int i = 1; //This int variable holds the number 1

The long is the final whole number data type. At 64-bits this variable comes in as a monster that can store values between -9223372036854775808 and -9223372036854775807. Longs make great candidate in scientific applications where the program may end up processing large sets of data. I also see them used to store primary keys in database programming.

Create a long by

long l = 100; //This long variable holds the number 100

Decimals and Fractions

There are times where you need to store numeric data that is not a whole number. For example, in many cases you may need to use the number PI (3.14159) in a calculation. Our next two data types hold such numbers.

The first is float, which is a floating point number (hence the name). This is a 32-bit variable that can store 1.4E-45 to 3.4028235E38. Double is the more commoly used data type (64-bits) and stores values ranging from 4.9E-324 to 1.7976931348623157E308.

float f = 5.99; //Make a float that stores the number 5.99
double d = -1.00; //Make a double that stores the number -1.00;

Never store money in these data types! The internal mechanics of these data types cause a loss in percision that can result in inaccuracies. Money should always get stored in the BigDecimal object.

Non Numeric Data

Java has the ability to store non-numeric data. Sometimes all a programmer needs to know if when something it True or False. The boolean data type is used for such a purpose.

boolean bTrue = true; //This boolean is true
bTrue = false; //Now it's false

We can also store unicode characters with the char type.

char c = 'a'; //Store the letter a
char tab = '\t'; //Stores  a tab character

Objects

Java also supports another data type that is not a primitive. These are called objects (to be discussed in another post). At this point there is one critical object to discuss that is called the String. String is a special kind of object that supports words and textual data.

//Since Strings are objects, so we use the 'new' keyword to create them
String str = new String("Hi! I'm a string of letters");

//But you can also do this since Java has special built-in support for Strings
String anotherString = "Hi! I'm another String!";

A working example

Below is a working example program that you can use to see the data types discussed above in action.


public class DataTypes {

    public static void main(String [] args){
        byte byteMin = Byte.MIN_VALUE;
        byte byteMax = Byte.MAX_VALUE;

        System.out.println("Bytes are the smallest data types (8-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in a byte is " + byteMin + " and" +
                " the maximum value is " + byteMax);
        System.out.println("");

        short shortMin = Short.MIN_VALUE;
        short shortMax = Short.MAX_VALUE;

        System.out.println("Shorts are one step larger than bytes (16-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in a short is " + shortMin + " and" +
                " the maximum value is " + shortMax);
        System.out.println("");

        int intMin = Integer.MIN_VALUE;
        int intMax = Integer.MAX_VALUE;

        System.out.println("Integers are one step larger than shorts (32-bit) and represent whole numbers.");
        System.out.println("The smallest value you can store in an integer is " + intMin + " and" +
                " the maximum value is " + intMax);
        System.out.println("");

        long longMin = Long.MIN_VALUE;
        long longMax = Long.MAX_VALUE;

        System.out.println("Longs are the largest of whole number data type (64-bit).");
        System.out.println("The smallest value you can store in a long is " + longMin + " and" +
                " the maximum value is " + longMax);
        System.out.println("");

        float floatMin = Float.MIN_VALUE;
        float floatMax = Float.MAX_VALUE;

        System.out.println("Floats represent decimal types. (32-bit)");
        System.out.println("The smallest value you can store in a float is " + floatMin + " and" +
                " the maximum value is " + floatMax);
        System.out.println("");

        double doubleMin = Double.MIN_VALUE;
        double doubleMax = Double.MAX_VALUE;

        System.out.println("Doubles also represent decimal types. (64-bit)");
        System.out.println("The smallest value you can store in a double is " + doubleMin + " and" +
                " the maximum value is " + doubleMax);
        System.out.println("");

        boolean booleanFalse = false;
        boolean booleanTrue = true;

        System.out.println("Booleans store true or false values.");
        System.out.println("booleanFalse = " + booleanFalse);
        System.out.println("booleanTrue = " + booleanTrue);
        System.out.println("");

        char charMin = Character.MIN_VALUE;
        char charMax = Character.MAX_VALUE;

        System.out.println("char represents a 16-bit unicode character");
        System.out.println("The smallest char value is " + charMin + " and the largest is " + charMax);
        System.out.println("");

        String string = new String("I'm a string of text");
        System.out.println("Java also has the Object data type. This is different from our other data types.");
        System.out.println("It this example, we have and object that is a String. Strings hold text.");
        System.out.println("string = " + string);
    }
}

This is the output of the program when executed

Bytes are the smallest data types (8-bit) and represent whole numbers.
The smallest value you can store in a byte is -128 and the maximum value is 127

Shorts are one step larger than bytes (16-bit) and represent whole numbers.
The smallest value you can store in a short is -32768 and the maximum value is 32767

Integers are one step larger than shorts (32-bit) and represent whole numbers.
The smallest value you can store in an integer is -2147483648 and the maximum value is 2147483647

Longs are the largest of whole number data type (64-bit).
The smallest value you can store in a long is -9223372036854775808 and the maximum value is 9223372036854775807

Floats represent decimal types. (32-bit)
The smallest value you can store in a float is 1.4E-45 and the maximum value is 3.4028235E38

Doubles also represent decimal types. (64-bit)
The smallest value you can store in a double is 4.9E-324 and the maximum value is 1.7976931348623157E308

Booleans store true or false values.
booleanFalse = false
booleanTrue = true

char represents a 16-bit unicode character
The smallest char value is  and the largest is ￿

Java also has the Object data type. This is different from our other data types.
It this example, we have and object that is a String. Strings hold text.
string = I'm a string of text
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s