Home Topic 4 Last Next

Miscellaneous SL Java Things

***Note that all these are fine here on their own, because each one makes a good peek back, and review to various things already covered.***

This will serve as a catch-all for any of the things that have "slipped through the (notes) cracks" as we have gone through the standard level programming curriculum. Most of these would have been mentioned a couple of times in class, but I'm not sure each of them ever made it to notes, or if they did, they bear repeating.

Modulus Division

The first thing to note is that integer division in Java cannot deal with fractional amounts directly. Integer arithmetic can only yield integer anwers, due to the limits of memory allocated to the int. (You may recall that 32 bits represent integers, thus giving a range of around -2,000,000,000 to around 2,000,000,000. There are no extra bits allocated to representing fractional amounts.)

So straight-forward integer division is therefore only capable of yeilding the whole number portion of the division. In Java, for example, 7/3 yelids 2, and 19/5 yeilds 3. But there is a Java operator that can come up with, and store the remainder of an integer division. This operator is called modulo, and used the symbol %. So 7%3 yelids 1, since 7 divided by 3 is 2, with a remeainder of 1. And 19%5 yelids 4, since 19 divided by 5 is 3, remainder 4. This kind of remainder division, which uses the modulo operator is called modulus division.

One of the most common uses of modulus division in programming is to yeild a 50:50 chance of something executing by modulus dividing an integer by 2, which will yeild 0 for all even numbers and 1 for all odd numbers. Certainly there are other ways to get a 50:50 split, but sometimes this works well. Here's an example:

if(studentAge % 2 == 0){

//which will be true for all even ages
System.out.println("You are in group 1."); }
else{//which will therefore be all odd ages
System.out.println("You are in group 2.");


Order of Operations

Arithmetic Expressions order of operations

The order of operations of arthmetic and algebraic expressions in Java is similar to what you have experienced in mathematics. Multiplication and division take precidence over addition and subtraction, and expressions within parentheses alway get evaluated first.

In terms of the assignment operators =, +=, -=, *=, and /=, they are executed only after the expressions to their right are evaluated. Take for example the following:

int x = 3;
x+= 4 * 2;

The 4 * 2 is evaluated first and then added to 3, so the result of these two lines is 11, not 14.

But because this often yields unintuitive results, particularly when several operators are combined in the same expression, the IB does not want you to use these at all, even ++ and -- (with the exception of ++ being used in a for loop.)


Boolean Expressions order of operations

Recall that for boolean expressions, there is a particular order of operations also. The big thing to note is that && - AND - takes precedence over || - OR.

In the example if(a || b && c || d && e){
the b && c and the d && e are evaluated before the ors.


Brackets, Braces, and Parentheses

Remember the terms we should be using for the following:

"Brackets": [ ]

"Braces": { }
(They are sometimes called "curly braces")

"Parentheses": ( )



The word "key" in programming usually refers to a unique value which is being searched for. So, often, in search algorithms, the variable taken in as a parameter which will be searched for is called key.


"In-line" Conditional Statements

An in-line conditional statemtent is a where there is only one line to be executed if the condition evaluates to true, so it is not necessary to continue the statement on a new line after the if part. For example:

if(isSunnyOutside) putOnSunglasses();

The two other ways of doing this would be:



if(isSunnyOutside) {

All three are correct, though it's probably best at this stage to always put braces.

BUT note that braces are required when there is more than one line to your conditional block. For example:

    System.out.println("Put on your swimsuit.");
System.out.println("Let's go to the beach.");
System.out.printline("I love Prague.");

Assumming that pragueIsTropical is false, the "Put on your swimsuit." line will be passed over, but not the "Let's go to the beach." line. The indentation is nice, but indentation alone does not control blocks; in Java, only the braces do, for multiline blocks. If the intention was for both of those lines to be printed if pragueIsTropical was true, then braces needed to be put around the multiline block, as follows:


{ System.out.println("Put on your swimsuit.");
System.out.println("Let's go to the beach."); }
System.out.printline("I love Prague.")

Signatures of Methods

The signature of a method is the specific nature of its header line (the header line being the first line of the method). So the signature has four parts:

Optionally the static modifier may need to be added if it is a method being called directly from the static main method.

Here are a few of examples of method headers with varying kinds of signatures:

public int[] bubbleSort(int[] arr)

protected void anotherMethod(int x, String y, boolean z)

public static void main(String [] args)

So, for example, we would say the header of the first above methods is public int[] bubbleSort(int[] arr).

And we would say the signature of the first one is: a public method named bubbleSort, which takes in one parameter - an int array, and returns an int array.

(The diagram from the section above helps clarify this.)