Previous lesson Back to the front Next lesson

True or False?


Is it true, or is it false? This is the question we're asking. This is really important - every time we want to compare two numbers, or two names, or just about anything we are asking a true/false type of question. Let's do an easy example first. Delete all the statements in our last program and let's do some comparisons.


class Hello {
	public static void main(String[] args) {
		System.out.println(2 < 3);
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
true
[nurmes]btg:

Wow, this is really exciting! Once again, the computer has done exactly what we've asked. 2 < 3 is a true/false question. In fact we know the answer to this particular true/false question is always true. What our program says is "print the answer to the question 'Is two less than three?'". The answer is true so that's all we get.

Let's make our program a little more descriptive:


class Hello {
	public static void main(String[] args) {
		System.out.println("2 < 3 is " + 2 < 3);
	}
}

[nurmes]btg: javac Hello.java
Hello.java:3: Incompatible type for <. Can't convert java.lang.String to int.
            System.out.println("2 < 3 is " + 2 < 3);
                                               ^
1 error
[nurmes]btg: 

Whoa! Not good! What does that message mean? Well, remember we used + to add something to the end of a string earlier? What is happening here is that the computer is adding 2 to the end of "2 < 3 is " to get "2 < 3 is 2". Which is also a string. Then it's trying to compare that to 3. Now I don't know how you say which is bigger out of "2 < 3 is 2" and 3, and neither does the computer. So it makes a fuss.

But we're not going to let it bother us. Let's use some brackets to make it clear exactly what we mean:


class Hello {
	public static void main(String[] args) {
		System.out.println("2 < 3 is " + (2 < 3));
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 < 3 is true
[nurmes]btg: 

We can compare variables and numbers like 2 (these are known as literals - 2 quite literally is the number two). Let's do a big program to illustrate this:


class Hello {
	public static void main(String[] args) {
	    System.out.println("2 < 3 is " + (2 < 3));
	    System.out.println("2 > 3 is " + (2 > 3));
	    System.out.println(); // this prints a blank line

	    int x = 2;
	    int y = 3;

	    System.out.println("x < 3 is " + (x < 3));
	    System.out.println("x > 3 is " + (x > 3));
	    System.out.println(); // this prints a blank line

	    System.out.println("2 < y is " + (2 < y));
	    System.out.println("2 > y is " + (2 > y));
	    System.out.println(); // this prints a blank line

    	    System.out.println("x < y is " + (x < y));
	    System.out.println("x > y is " + (x > y));
	}
}

Do you like the comments?

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 < 3 is true
2 > 3 is false

x < 3 is true
x > 3 is false

2 < y is true
2 > y is false

x < y is true
x > y is false
[nurmes]btg: 

It really doesn't matter most of the time in Java whether we use literals or variables, or a mixture (one time when it matters is using the ++ operator - how do you do 2++ ?).

We can also do "less than or equal to" using the operator <= and "greater than or equal to" using the operator >=. We'll change our current program to show this:


class Hello {
	public static void main(String[] args) {
	    System.out.println("2 < 3 is " + (2 < 3));
	    System.out.println("2 <= 3 is " + (2 <= 3));
	    System.out.println(); // this prints a blank line

	    int x = 3;
	    int y = 3;

    	    System.out.println("x < y is " + (x < y));
	    System.out.println("x > y is " + (x > y));
	    System.out.println(); // this prints a blank line

    	    System.out.println("x <= y is " + (x <= y));
	    System.out.println("x >= y is " + (x >= y));
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 < 3 is true
2 <= 3 is true

x < y is false
x > y is false

x <= y is true
x >= y is true
[nurmes]btg: 

Let's learn a couple more. First, there is == which means "equal to".


class Hello {
	public static void main(String[] args) {
		System.out.println("2 == 2 is " + (2 == 2));
		System.out.println("2 == 3 is " + (2 == 3));
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 == 2 is true
2 == 3 is false
[nurmes]btg: 

One of the easiest mistakes to make is to confuse == with =. Don't do it. Let's see what happens if we do:


class Hello {
	public static void main(String[] args) {
		System.out.println("2 == 2 is " + (2 = 2));
		System.out.println("2 == 3 is " + (2 == 3));
	}
}

[nurmes]btg: javac Hello.java
Hello.java:3: Invalid left hand side of assignment.
                System.out.println("2 == 2 is " + (2 = 2));
                                                   ^
1 error
[nurmes]btg: 

Notice that it's pointing at the first 2 and saying "Oi! Invalid left hand side of assignment, guv'nor!". Well if yours doesn't say that then it's obviously not set up properly for Cockney Java. Notice that word assignment again. It thinks that we're trying to set the value of something to 2, and it's not too sure about the something. The more observant of you will remember I said that a variable name can't be a number. This is why.

Equally useful (sorry, no pun intended) is != which means "not equal to".


class Hello {
	public static void main(String[] args) {
		System.out.println("2 != 2 is " + (2 != 2));
		System.out.println("2 != 3 is " + (2 != 3));
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 != 2 is false
2 != 3 is true
[nurmes]btg: 

Then there's just ! on it's own, which just means "not" - in other words the opposite of something. Here's some examples:


class Hello {
	public static void main(String[] args) {
		System.out.println("2 < 3 is " + (2 < 3));
		System.out.println("2 > 3 is " + (2 > 3));
		System.out.println("!(2 < 3) is " + !(2 < 3));
	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
2 < 3 is true
2 > 3 is false
!(2 < 3) is false
[nurmes]btg: 

I hope that's clear enough. We'll use it again in our last example, which demonstrates the keywords true and false:


class Hello {
	public static void main(String[] args) {
		System.out.println("true is " + true);
		System.out.println("false is " + false);
		System.out.println(); // this prints a blank line

		System.out.println("!true is " + !true);
		System.out.println("!false is " + !false);
		System.out.println(); // this prints a blank line

		System.out.println("!!true is " + !!true);
		System.out.println("!!false is " + !!false);
	}
}

The last two statements may look a bit odd, but what we're doing is saying "not not", a double negative. For example "I'm not not coming to the pub" means that I am.

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
true is true
false is false

!true is false
!false is true

!!true is true
!!false is false
[nurmes]btg: 

So now when my friends ask me "What does !!!!!false mean?" I can say with confidence "Um...well..." Let's face it, my friends don't ask me those sorts of questions often, it's more likely to be "So, are you not not coming to the pub, or not?"

Now that you're convinced I have totally lost it, it's time to put all this true/false nonsense to some good use.

Iffy Programming!

Too patronising? Too complex? Typing error? Offended by traffic cones? Got a question or something I should add? Send an email to ben_golding@yahoo.co.uk !

visits to this site

The contents of this site are copyright of Ben Golding