Home Topic 4 Last Next

IB CS Syllabus preface to the next section:

"Use of programming languages

Sub-programs and objects support abstraction, which facilitates: ease of debugging and maintenance, reuse of code, modularity.

There is no programming language specified in the SL/HL core. However, students must use a language that supports the basic constructs on the approved notation sheet."


Define the terms: variable, constant, operator, object.


Teaching Note:

Sample Question:


JSR Notes:


variable (as in the 's' of String s = "Hello world";)
- There are actually two useful ways of looking at what a variable is; at higher level of abstraction, we can just look at how a variable behaves in a program. But at a lower level of abstraction, at the hardware level in fact, it is useful to understand how a variable works at a memory level.

Mathematical Programming Definition (higher level of abstraction)

A variable, as in Math, is a named piece of data which can, and often does, vary over the course of a program.

Computer Memory Definition
(more theoretical, at a lower level of abstraction)

A variable is a named shortcut for a memory location holding a particular, declared, kind of data (Do note that objects - see below - are technically also variables; they are variables of a reference type.)

(In either case) For example:

int i = 0;

// i now holds the value of 0. i = i + 3; // Now the value of i has changed, i.e. it has varied. It now is 3, rather than 0.

(Now a fine, fine, knit picky detail. The "variable identifier" is the name we use for the variable. In the example above, "i", where-as the "variable" is the actual shortcut for the memory location where a certain value resides, and so, in fact, the particular value of that data.
The best analogy I can think of for this is as folows: a "person's identifier" is "John", and yet the "person" himself is "John"; by the first John, we mean the person's name/identifier, and the second John we mean the person.)


constant - a variable which is actually not allowed to vary. Constants are declared constant in Java with the keyword final. And the convention across many programming languages is to declare them in ALL CAPITALS. But note that just because you name a variable in all caps, that doesn't make it a constant; it's the reserved word final as part of the variable declaration that does.

For example:

private static final ARRAYSIZE = 10;


operator - an operator is a symbol which performs an action on either one or two "operand"; these are called, respectively "unary" and "binary" operators. In terms of the kinds of operators, generally, there are arithmetic operators and logical operators.

For example:

System.out.println(n + n1); // + is a binary operator which returns the sum of the two operands.

System.out.println(n < n1) // < is a binary operator which returns whether or not the first operand is less than the second operand.

System.out.println(n++); // ++ is a unary operator which increments its operand by one.

Here are all of Java's operators:

!   ++   --    +   -    *    /    %   ~    <<   >>   >>>   &    |   <    <=   >   >=    ==    !=   ^    &&   ||    ?:    =    +=   -=    *=    /=    %=

Refer to the Oracle Summary of Operators page. (And see below for an example of the use of "bitwise" operators.*)


*Bitwise stuff, though totally not necessary to know.

public class BitWiseStuff { 
    public static void main(String[] args) { 
        System.out.println(10 << 3); 
        //0000 1010 
        //shifted to the left three: 
        //0101 0000 (1 group of 64s + 1 group of 16s = 80) 
        System.out.println(10 >> 3); 
        //0000 1010 
        //shifted to the right 3: 
        //0000 0001 

- In Java, an object itself is actually an address; a reference for where certain data resides in memory. So in terms of definitions, we can say objects are variables, it's just that they are variables of a reference type.

So, yes, objects are literally addresses. But it is also naturally useful to think of objects in terms of the data which is stored at that address; the data "pointed to" by that address. Put slightly differently, yes, the object itself is a 64 bit part of memory which stores an address, but the object, in terms of data used in the program, is the data that is stored beginning at that address.

(By the way, this is opposed to literal variables, which are simply shortcuts for that memory address; the data in the literal case literally resides at that address for which the variable is a shortcut, whereas an the object's data is pointed to by the object reference.)

In Java, we make new objects with the new operator, which will look for and reserve just enough new memory for that particular data type.

For example:

Student s = new Student(); // s refers to the place in memory in which a Student instance will be made and stored.

variable and object diagram


Just a bit more of a clarification...

Actually, both i and s are technically variables, since they are indeed both shortcuts for a particular place in memory, i.e. named memory locations. But as far as this assessment statement goes, in terms of the difference between a variable and an object, well, it would be better thinking about the difference between a "primitive" variable and an object variable. So, a primitive variable is the shortcut for the actual data, whereas an object variable is a shortcut an address of where the object data resides.