CSC108 Lecture Notes (Meng Sun)

Variables and Data Types

Variables are the nouns of a programming language-that is, they are the entities (values and data) that act or are acted upon.
A variable declaration always contains two components: For example, "int count;" has type integer and name "count".

All variables in the Java language must have a data type. A variable's data type determines the values that the variable can contain and the operations that can be performed on it.  For example,
 
values it can contain operations which can be performed on it
Integer integral values 
(both positive and negative)
standard arithmetic (+, -, *, / and %) 

Integers can contain only integral values (both positive and negative), and you can use the standard arithmetic operators (+, -, *, / and %) on integers to perform the standard arithmetic operations (addition, subtraction, multiplication, and division, respectively).

Operator / and %: The operator / is the division operator, and % is the remainder operator.  For example,

int i =17 / 3;      // The result is 5
int i = 17 % 3;  // The result is 2, which is the remainder after the division.

Data Types

There are two major categories of data types in the Java language: primitive and reference.

Which primitive types should you know?

The following table lists, by keyword, some of the primitive data types in Java which you should know, their sizes and formats, and a brief description of each.
 
 
Type Size/Format Description
(integers)
int 32-bit two's complement Integer
(real numbers)
double 64-bit IEEE 754 Double-precision floating point
(other types)
char 16-bit Unicode character A single character
boolean true or false` A boolean value (true or false)

Numbers

Numbers come in various favours, but the most important classification is: does it have a fraction? There are times when this advice will be wrong, but at this point in your career, you shouldn't be worrying about those times.
Characters Booleans

Mixed Type Arithmetic and Casting

Sometimes, we would like to work with different data types.  Different data type has different formats/sizes.  For instance, char, int and double are 16-bits, 32-bits and 64 bits, respectively.  The convertion from one data type to the other has to follow ceratin rules.

Promotion Rule

The promotion rules specify how types can be converted to other types without losing data.  For example, an int is automatically converted to a double without changing its value.  However, a double converted to an int trucates the fractional part of the double value.

The promotion rules apply to expressions containing values of two or more data types; such expressions are also referred to as mixed-type expressions.  The type of each value in a mixed-type expression is promoted to the "highest" type in the expression (actually a temporary version of each value is created and used for the expression -- the original values remain unchanged).  The table below lists the primitive data types (boolean, char, int, double) and the types to withch each is allowed to be promoted automatically.
 
Type Allowed promotions
double None (there are no primitive types larger than double)
int double
char int, double
boolean None (boolean values are not considered to be numbers in Java)

Casting
If you have
double d = 3.14;
int i = d;        // this is WRONG!!
The compiler will complain that "this is an illegal assignment".  Becuase a double number has 64-bits whereas an integer (int) has only 32 bits, i.e., a double stores more information than an int.

Converting values to lower types can result in incorrect values.  Therefore, in cases where information may be lost due to conversion, the Java compiler requires the programmer to use a cast operator to force the conversion to occur.

For example, assigning the double

double d = 3.1415926;
to an int type variable
int i;
we would lose accuracy.  So we have to do the conversion explicitly by telling Java that we want to convert the double to an int --
i = (int) d; // now i is 3
Java will make a copy of d, truncate the fractional part of number, and assign the result to i.
Here are some more examples of casting lower types (less bits in its format/size) to

Precedence of Arithmetic Operators

The table below shows the precedence and of the operators.  The operators are shown top-to-bottom in decreasing order of precedence.
Operators Type
() parenthese
++  --  +  -  (type) unary
*   /   % multiplicative
+   - additive
<   <=   >   >= relational
==   != equality
=   +=   -=   *=   /=   %= assignment

Primitive vs Reference Data Types

There are two major categories of data types in the Java language: primitive and reference.

Primitive Data Types

  • A variable of primitive type contains a single value of the appropriate size and format for its type: a number, character, or boolean value.

  • For example, the value of an int is an integer, the value of a char is a 16-bit Unicode character, and so on.
    Reference Data Types
  • Arrays, classes, and interfaces are reference types.
  • The value of a reference type variable, in contrast to that of a primitive type, is a reference to the actual value or set of values represented by the variable.
  • A reference is like your friend's address: The address is not your friend, but it's a way to reach your friend. A reference type variable is not the array or object itself but rather a way to reach it.


  • This is like -- knowing the address of Joe's house, we can locate Joe's house.
    The addressis a reference to Joe's house, not the actual object.

    Example type The variable stores...
    Primitive Data Type int, double, char, boolean the actual value
    Reference Data Type class, array, interfaces a reference (like an address)

    The countChars method uses one variable of reference type, in, which is a Reader object. When used in a statement or expression, the name in evaluates to a reference to the object. So you can use the object's name to access its member variables or call its methods (just as countChars does to call read).