Home
Examples
Labs
Assignment
Marks
Midterm
Final Exam
Resources

Primitive Data Types

A primitive data type is only a value.  It has no methods.  This is because primitive data types are provided as part of the java language - not through a java class definition.



Numeric Primitives

The java language provides many different primitive data types to represent and store numeric values.  However, the only numerical primitives that you will need to know for this course are :
  • int - 32 bit, whole numbers.
  • float - 32 bit, decimal numbers.
  • double - 64 bit, decimal numbers.


The Size of Numeric Primitives

The number of bits describes the amount of space used to store the primitive value in memory.  You do not need to memorise this, but as a matter of interest :
  • 32 bit integers can hold values in the range: -2,147,483,648 to 2,147,483,647.
  • 32 bit floats can hold decimal numbers in the range: +/- 1.40239846 E-45 to +/- 3.40282347 E+38.
  • 64 bit doubles can hold decimal numbers in the range: +/- 4.94065645841246544 E-324 to +/- 1.79769313486231570 E+308.


Just Remember...

  • int values do not have a decimal point.
  • float and double values both have decimal points.
  • double values take up more space in memory than float values, but they offer greater precision.


Numeric Primitive Type Variables

Here are some examples using variables that hold numerical primitive types.  Remember that when you declare a variable to be of a particular type, it can only hold values of that type.  Attempting to assign any other type of value to the variable will cause either an error (as with float to int assignment), or an automatic conversion if one is possible (as with int to float assignment).
  • declaring primitive type numeric variables :
               int myInteger;
               float myFloat;
               double myDouble;

  • declaring and initialising primitive type numeric variables :
               int myInteger = 6;
               float myFloat = 5.05f;
               double myDouble = 5.05;

  • assigning values to primitive type numeric variables :
               myInteger = 6 + 5;
               myFloat = myFloat + 2.0f;
               myDouble = myDouble * 6.0;

  • printing primitive type numeric variables :
               System.out.println(myInteger);
               System.out.println(myFloat);
               System.out.println(myDouble);


Differences Between Primitive Types and Objects

  • Constructors - We have to explicitly create new objects.  New objects are created using a constructor.  With primitive types we just assign a value to the variable, there is no explicit construction process carried out.


  • Methods - Objects have behaviours / methods.  Objects know how to do things.  We can send objects messages asking them to carry out tasks for us.  Primitive types are not smart like objects.  We cannot send messages to primitive types asking them to do personal favours for us.


  • Reference Variables - Objects are large.  They take up a lot more room in memory than primitive data values.  When a constructor creates a new object it doesn't actually return the object, it just returns a reference telling us where we can find the object that was created.  So, a variable for a primitive type contains a value, a variable for an object type contains a reference to where the object can be found.


  • Naming Convention - It is a naming convention in java for primitive data types to start with a lower case letter, and for object types to start with an upper case letter.


Arithmetic in Java

Mathematical Operators :
*
/
%
+
-
++
--
multiplication
division
remainder
addition
subtraction
increment
decrement

Order of Mathematical Operations :
1.
2.
3.
4.
5.
()
++
*
+
=

--
/
-



%


bracketed expressions
increment, decrement
multiply, divide, modulus
add, substract
assignment

** Remember that operations with equal priority are evaluated from right to left.



Example Arithmetic Expressions

These examples are meant to illustrate operator precedence in java.  The comment (shown in blue) describes the value that is ultimately assigned to the variable once the right hand side of the expression is evaluated.  Since the assignment operator has the lowest priority, assignment is always carried out after all other evaluations are completed.

        // i = 3
           int i = 1 + 5 - 3;

        // i = 13
           int i = 3 + 2 * 5;

        // i = 16
           int i = 2 * 6 + 16 / 4;

        // x = 8.5
           float x = 2.0f + 6.5f;

        // x = 3.5
           float x = 3.0f + 5.0f / 10.0f;

        // d = 72
           double d = (1.0 + 8.0) * (2.0 + 6.0);



Integer Division & Remainder

Integer division (sometimes called whole number division) happens when we divide one integer by another.  Because both operands are integers, the result must also be an integer.  The result is the whole number part of the quotient.  To get the remainder you must use the modulus operator.  Remember, there is no fractional part computed with integer division or modulus!

        With integer division:
                  6 / 1 = 6
                  6 / 2 = 3
                  6 / 3 = 2
                  6 / 4 = 1
                  6 / 5 = 1

        With modulus:
                  6 % 1 = 0
                  6 % 2 = 0
                  6 % 3 = 0
                  6 % 4 = 2
                  6 % 5 = 1



Mixed Mode Arithmetic

Try to avoid mixed mode arithmetic when possible.  Think carefully about whether you will ever need to store or compute a decimal point.  The general rule for mixed mode arithmetic when you do carry it out, is that the largest and most complicated numerical type in the expression forces the result of the expression to be of its type.  The goal in mixed mode arithmetic is not to lose any degree of accuracy.  Basically :
  • multiplying an int and a float results in a float value because the float introduces a decimal point.
  • multiplying an int and a double results in a double value since the double introduces a decimal point and a higher level of precision.
  • multiplying a float and a double results in a double value since the double introduces a higher level of precision.


Methods from the Math class

The Math class groups together mathematical functions and constants.  The Math class does not have a constructor so you cannot create Math objects.  Instead, the Math class is defined in a special way, and imported automatically into your java programs.  You can access Math methods by just using the word Math.  Here are some examples of Math methods.  To find out more about the Math class, check out the java API.
        // Calculate the square root of 9.0
           double d = Math.sqrt(9.0);

        // Get the absolute value of -4
           int i = Math.abs(-4);  

        // Get the minimum of int values num1 and num2
           int smallest = Math.min(num1, num2);

        // Get the maximum of float values x1 and x2
           float biggest = Math.max(x1, x2);

        // Calculate 16 cubed
           int i = Math.pow(16,3);


Numeric Input

BufferedReader objects only know how to read Strings.  Even when a BufferedReader reads a numerical value, it is still represented in the computer as a String.  If we need to receive truly numerical input, we must first read in a numerical String from the BufferedReader and then explicitly convert the String to a number.  This is how we convert Strings to numbers in java:
        // Reading a String and converting it to an integer
           String temp = kb.readLine()
           int i = Integer.parseInt(temp);

        // Reading a String and converting it to a float
           String temp = kb.readLine()
           float f = Float.parseFloat(temp);

        // Reading a String and converting it to a double
           String temp = kb.readLine()
           double d = Double.parseDouble(temp); 


Data Type Wrapper Classes

Recall that primitive data types do not have methods - they are stored as just a value.  Sometimes however, we need to carry out operations on, and find out additional information about, a primitive value.  It would be useful to have methods that we could apply to primitive types.  Java provides such methods in Data Type Wrapper Classes.  There is a data type wrapper class for each primitive type.  We used data wrapper classes above to convert the sequence of characters in a String to a numerical primitive.  Integer, Float, and Double are all examples of data type wrapper classes.