Previous lesson Back to the front Next lesson

How to Type


In case you hadn't guessed, this lesson is all about different types of things in Java (see Lecture Note 12). Much like in the real world, everything has a type. Every variable has a type and every literal has a type (remember that something like 2, or -31, or true is a literal). It helps if you think of variables as little boxes which store something from now on. That something doesn't have to be a number - in fact we'll look at some examples which aren't numbers in this lesson. There are some rules about what types of literals can go in what types of variables - but basically it comes down to "You can't put a square peg in a round hole".

So then, onwards! There are two types of type in Java (sorry about that, but there are). The first is primitive types. You've used one of these already (called int). There are only eight of these primitive types (don't worry, I'll explain them in a minute):

Type Size Minimum Value Maximum Value
boolean 1 bit The boolean type can only be true or false.  
char 16 bits \u0000 \uFFFF
byte 8 bits (or 1 byte, surprisingly) Byte.MIN_VALUE (this is -27, or -128) Byte.MAX_VALUE (this is 27 - 1, or 127)
short 16 bits (or 2 bytes) Short.MIN_VALUE (this is -215, or -32768) Short.MAX_VALUE (this is 215 - 1, or 32767)
int 32 bits (or 4 bytes) Integer.MIN_VALUE (this is -231, or -2 147 483 648) Integer.MAX_VALUE (this is 231 - 1, or 2 147 483 647)
long 64 bits (or 8 bytes) Long.MIN_VALUE (this is -263, or -9 223 372 036 854 775 808) Long.MAX_VALUE (this is 263 - 1, or 9 223 372 036 854 775 807)
float 32 bits (or 4 bytes) Float.MIN_VALUE (this is 1.4 x 10-45, or about 0.000 000 000 000 000 000 000 000 000 000 000 000 000 000 001 401 298 464 324 817 070 923 729 583 289 916 131 28). Float.MAX_VALUE (this is 3.4 x 1038, or about 340 282 346 638 528 859 811 704 183 484 516 925 440).
double 64 bits (or 8 bytes) Double.MIN_VALUE (this is 4.9 x 10-324, or about 0.000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 004 940 656 458 412 465 441 765 687 928 682 213 723 650 598 026 143 247 644 255 856 825 006 755 072 702 087 518 652 998 363 616 359 923 797 965 646 954 457 177 309 266 567 103 559 397 963 987 747 960 107 818 781 263 007 131 903 114 045 278 458 171 678 489 821 036 887 186 360 569 987 307 230 500 063 874 091 535 649 843 873 124 733 972 731 696 151 400 317 153 853 980 741 262 385 655 911 710 266 585 566 867 681 870 395 603 106 249 319 452 715 914 924 553 293 054 565 444 011 274 801 297 099 995 419 319 894 090 804 165 633). Double.MAX_VALUE (this is 1.8 x 10308, or about 179 769 313 486 231 570 814 527 423 731 704 356 798 070 567 525 844 996 598 917 476 803 157 260 780 028 538 760 589 558 632 766 878 171 540 458 953 514 382 464 234 321 326 889 464 182 768 467 546 703 537 516 986 049 910 576 551 282 076 245 490 090 389 328 944 075 868 508 455 133 942 304 583 236 903 222 948 165 808 559 332 123 348 274 797 826 204 144 723 168 738 177 180 919 299 881 250 404 026 184 124 858 368).

Phew, what a big number! For the curious among you only, you can take a look at the program I used to help me generate these numbers (don't expect to understand all of it):


import java.math.BigDecimal;

class Hello {
	public static void main(String[] args) {
		System.out.println("The  biggest value of a byte is " + Byte.MAX_VALUE);
		System.out.println("The smallest value of a byte is " + Byte.MIN_VALUE);
		System.out.println();

		System.out.println("The  biggest value of a short is " + Short.MAX_VALUE);
		System.out.println("The smallest value of a short is " + Short.MIN_VALUE);
		System.out.println();

		System.out.println("The  biggest value of an int is " + Integer.MAX_VALUE);
		System.out.println("The smallest value of an int is " + Integer.MIN_VALUE);
		System.out.println();

		System.out.println("The  biggest value of a long is " + Long.MAX_VALUE);
		System.out.println("The smallest value of a long is " + Long.MIN_VALUE);
		System.out.println();

		System.out.println("The  biggest value of a float is " + new BigDecimal(Float.MAX_VALUE));
		System.out.println("The smallest value of a float is " + new BigDecimal(Float.MIN_VALUE));
		System.out.println();

		System.out.println("The  biggest value of a double is " + new BigDecimal(Double.MAX_VALUE));
		System.out.println("The smallest value of a double is " + new BigDecimal(Double.MIN_VALUE));
 	}
}

Okay, so that's all the scary stuff out the way, now to make some sense of it. Notice that I've split the table up into several sections. The first one is concerned with a type called boolean. In fact we have already encountered boolean in the lessons "True or False?", "Iffy Programming" and "Or Else ...". A boolean value is either true, or false. It's that simple. You've used booleans many times already without really realising it. What I now want to convince you, however, is that a boolean can be handled exactly like a number. It's time to write some Java!


class Hello {
	public static void main(String[] args) {
		int i;
		boolean b;
 	}
}

This is a very simple program and you don't have to compile and run it just yet. The point I am making is that we can create a boolean variable the same way as an int variable. In this case we have created an int called i, and a boolean rather imaginatively called b.

So now let's take a look at the values of these two variables:


class Hello {
	public static void main(String[] args) {
		int i;
		boolean b;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

We try to compile and run the program, but...

[nurmes]btg: javac Hello.java
Hello.java:5: Variable i may not have been initialized.
                System.out.println("The value of i is " + i);
                                                          ^
Hello.java:6: Variable b may not have been initialized.
                System.out.println("The value of b is " + b);
                                                          ^
2 errors
[nurmes]btg: 

Oh no, a disaster! It doesn't compile! The reason is that we've not initialized the variables. Whatever that means. Well actually we already know what it means - initializing is setting the variable's value to something. Anything in fact. The reason the compiler is complaining is that it doesn't want to print out the value of i when it's not sure that i has a value. Let's give it a value:


class Hello {
	public static void main(String[] args) {
		int i = 2;
		boolean b;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

And now, let's try again:

[nurmes]btg: javac Hello.java
Hello.java:6: Variable b may not have been initialized.
                System.out.println("The value of b is " + b);
                                                          ^
1 error
[nurmes]btg: 

Well I suppose we should have expected that. So how exactly can we initialize a boolean, then? Well it's just the same. A boolean can only be true or false, so we have to set it to either true or false. Just like this:


class Hello {
	public static void main(String[] args) {
		int i = 2;
		boolean b = true;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

And finally, it works:

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
The value of i is 2
The value of b is true
[nurmes]btg: 

That was incredibly painless. Just to remind you of something we learned a long time ago, we don't have to create and initialize a variable all together on one line:


class Hello {
	public static void main(String[] args) {
		int i;
		i = 2;
		boolean b;
		b = true;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

Trust me, this program runs exactly the same as the previous one. If you don't trust me, try it.

So what else can we do with a boolean? At the moment it doesn't seem very useful. Well, things like 2 > 3 or 2 == 3 evalutate to ("work out to") boolean values - they are "true or false questions", after all. In fact, 2 == 3 evalutates to false just like 2 + 3 evalutates to 5. I'll show you:


class Hello {
	public static void main(String[] args) {
		int i;
		i = 2;
		boolean b;
		b = true;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
		System.out.println("The value of 2 + 3 is " + (2 + 3));
		System.out.println("The value of 2 == 3 is " + (2 == 3));
 	}
}

If you've forgotten why we need the brackets around 2 == 3 and 2 + 3, then pop back to "True or False?" for a quick reminder. Otherwise, carry on:

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
The value of i is 2
The value of b is true
The value of 2 + 3 is 5
The value of 2 == 3 is false
[nurmes]btg: 

The answers shouldn't surprise us too much. However, how about this:


class Hello {
	public static void main(String[] args) {
		int i;
		i = (2 + 3);
		boolean b;
		b = (2 == 3);
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
The value of i is 5
The value of b is false
[nurmes]btg: 

Or even this!


class Hello {
	public static void main(String[] args) {
		int i;
		i = 2 + 3;
		boolean b;
		b = 2 == 3;
		System.out.println("The value of i is " + i);
		System.out.println("The value of b is " + b);
 	}
}

(yes, it does the same thing)

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
The value of i is 5
The value of b is false
[nurmes]btg: 

This version is really scary looking, especially the line b = 2 == 3 but don't worry, it's simple.

Let's look at the line which sets the value of i. We know that 2 + 3 evaluates to 5. All we do is evaluate the part to the right of the =. Then we take the value which it evaluates to (ie 5), and put it into i.

Now let's look at the scary line in exactly the same way. We know that 2 == 3 evaluates to false. All we do is evaluate the part to the right of the =. Then we take the value which it evaluates to (ie false), and put it into b.

Now that wasn't so bad, was it? The point I'm trying to make is that there are a lot of things which you can do with a boolean that you do just the same as with an int. One case where this isn't true is addition - you can't add true to false, it's just silly. Our next program looks at something you can do with a boolean, but not with an int:


class Hello {
	public static void main(String[] args) {
		boolean b;
		b = 2 == 3;

		if (b)
			System.out.println("Is it true...");
		else
			System.out.println("...or is it false?");
 	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
...or is it false?
[nurmes]btg: 

That's right, not only can we do if (2 == 3), but we can do if (b), so long as b is a boolean variable. Let's change the program slightly:


class Hello {
	public static void main(String[] args) {
		boolean b;
		b = 3 == 3;

		if (b)
			System.out.println("Is it true...");
		else
			System.out.println("...or is it false?");
 	}
}

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

Suppose you wanted to do it the other way round, ie only do something when b is false. That's easy, we use NOT (written !) which we learned before:


class Hello {
	public static void main(String[] args) {
		boolean b;
		b = 3 == 3;

		if (!b)
			System.out.println("It's FALSE!");
		else
			System.out.println("It's TRUE! It's TRUE! It really was the snowman, Mummy!");
 	}
}

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
It's TRUE! It's TRUE! It really was the snowman, Mummy!
[nurmes]btg: 

Notice how I've had to swap the true and false messages around. The first one will only be printed if !b (read it as "NOT b") is true, in other words if b is false. For a reminder of the ! operator, click here to go back to "True or False?".

One word of warning. It is possible to write if (b == true) instead of if (b). You can also write if (b == false) instead of if (!b). I wouldn't recommend it. Here's why:

One of my friends just made a mistake which was slightly fatal to the program working, but without causing any errors. NEVER EVER mix up = and ==. Most of the time you won't be able to, but when doing something like if(b == false) you will run into problems if you write if(b = false). This is disastrous because it sets b to false and then checks to see if it is false or not. Of course it will always be false, because you've just set it to that. If you don't get it, just believe me when I tell you that you don't want to make this mistake because it's really hard to spot. To summarise what we talked about before:

x == 2 Compare the value of x to 2.
Evaluates to true (if x equals 2) or false (if x does not equal 2).
b == false Compare the value of b to false.
Evaluates to true (if b equals false) or false (if b does not equal false).
x = 2 Set the value of x to 2.
Always evaluates to 2.
b = false Set the value of b to false.
Always evaluates to false.

If you're not 110% clear on this then you can go back to "True or False?" for some more revision. If you stick to my method where you write if (b) and not if (b == true), then you'll be fine anyway. Right, carry on!

Great. Well, er, um, you probably won't see the point of this now and I'm not even going to try to explain. Just try to understand some of it. There's quite a lot more about boolean, so let's go to the next lesson.


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