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:

At the basic level, this is a simple, "define" kind of assessment statement. But it will be useful to go a bit beyond the basics in all cases.

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 also, 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. Student oldest = sam; // oldest holds the value sam oldest = christopher; // The value of this variable has changed.

As seen in the Student example above, note that variables can be of the primitive type (like int) or the object type; see below for more details.

(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 - There are two senses of "constant" in programming: a variable which is not supposed to change, or simply any number value - i.e. the mathematical sense of constant.

The Mathematical sense of "constant"

Consider the algebrean expression y = 2x + 7. In a mathematical sense, x and y are variables, 2 is a coefficient, and 7 is a contant. In other words, a constant is just a "raw" number. And we use such numbers all the time in coding. And we can refer to them as constants.

For example:

if (2x * 3 > height - 14)

All three of the numbers can be referred to as constants; the constant 2, the constant 3, and the constant 14.

The "variable" sense of "constant"

Used in this sense, constant means a variable which is not allowed to vary. Variables are declared constant in Java with the keyword final. And the convention across Java and many other 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; in Java, 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 "operands"; these are called, respectively "unary" and "binary" operators. In terms of the kinds of operators that exist, generally, there are two categories, arithmetic operators and logical operators.

For example:

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

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

System.out.println(n++); // ++ is a unary arithmetic 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.

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.

This is opposed to primitive type variables and their literal values. Primitive variables (such as variables that are int or double or char) are simply shortcuts for a memory address where the actual, literal value is stored.

variable and object diagram


Just a bit more of a clarification...

The diagram above depicts things the way we often think about them; we tend to think of variables being primitives, and object variables as being objects. But really, above, both i and s are variables; it's just that i is a primitive type variable, and s is an object type variable.