Previous lesson Back to the front Next lesson

Odds'n'sods


This is where the pace picks up. Keep the Terminal window and Hello.java handy, because we'll be using both of them to carry on learning stuff. We're covering a lot of little odds and sods here but they'll be useful later. To start with, you should have a Java program just like this one:


class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");
	}
}

Now about this thing called  + . In Java it can do lots of things - it can be used for adding two numbers together, or for adding two strings together, or for adding a number to a string. We say that the plus operator is overloaded. The word operator means that it is a symbol which does something. Overloaded just means that it knows how to do more than one thing (in this case adding strings and numbers). It's easier to demonstrate this with an example:


class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");
		System.out.println("My name is " + "Ben");
		System.out.println("My age is " + 19);
		System.out.println(1 + 1);
		System.out.println("My flat is number " + (1 + 1));
	}
}

I've put the new parts in red for you. Let's see what happens:

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
Hello, world!
My name is Ben
My age is 19
2
My flat is number 2
[nurmes]btg: 

OK? Have a look at the program to make sure you understand why it does what it does. Now let's try making a very small change, and see what happens...


		System.out.println("My flat is number " + 1 + 1);

To save space, I've only shown the changed line in the program. I hope it is clear which part is changed from the red color.

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
Hello, world!
My name is Ben
My age is 19
2
My flat is number 11
[nurmes]btg: 

Oh no! That's the wrong flat - you'll miss the party!
As you can see, it makes a difference whether we use brackets like this "My flat is number " + (1 + 1) or not.

Now we're going to learn about something called a comment. Unlike a statement, it doesn't do anything. In fact, when you use javac, all the comments are just thrown away. The only use for them is to tell you (or someone else looking at your program) what it does. Here's an example:


class Hello {
	public static void main(String[] args) {
		// This is a single line comment which ends at the end of the line
		System.out.println("Hello, world!");
		System.out.println("My name is " + "Ben");
		System.out.println("My age is " + 19);
		System.out.println(1 + 1);
		System.out.println("My flat is number " + 1 + 1);
	}
}

This type of comment starts with // and ends once we get to the end of the line. But there is another type. The second type starts with /* and ends with */. They can be on one line, or two, or as many as you want - it only ends when you reach a */. Look at this:


class Hello {
	public static void main(String[] args) {
		// This is a single line comment which ends at the end of the line
		/* This is the other type of comment */
		System.out.println("Hello, world!");
		/* This is a comment
		spread across
		three lines! */
		System.out.println("My name is " + "Ben");
		System.out.println("My age is " + 19);
		System.out.println(1 + 1);
		System.out.println("My flat is number " + 1 + 1);
	}
}

Change your program so that it contains both types of comment. Now compile and run it again to convince yourself that the comments don't make any difference to the way it works.

[nurmes]btg: javac Hello.java
[nurmes]btg: java Hello
Hello, world!
My name is Ben
My age is 19
2
My flat is number 11
[nurmes]btg: 

Here's a demonstration of comments that is more true to real programming life:


/* This program is pretty simple really
 although it does tell you some personal
 information about me. */
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");

		// This part demonstrates using "+" to add strings together
		System.out.println("My name is " + "Ben");  // prints my name
		System.out.println("My age is " + 19);  // prints my age
		System.out.println(1 + 1);
		System.out.println("My flat is number " + 1 + 1);  // prints my flat no.
	}
}

One last use for comments is for temporarily disabling parts of a program. The easy way to do this is to delete them, of course, but with our way you can get them back later. Suppose we don't want to print my flat number (well, it is wrong after all):


/* This program is pretty simple really
 although it does tell you some personal
 information about me. */
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");

		// This part demonstrates using "+" to add strings together
		System.out.println("My name is " + "Ben");  // prints my name
		System.out.println("My age is " + 19);  // prints my age
		System.out.println(1 + 1);
		//System.out.println("My flat is number " + 1 + 1);  // prints my flat no.
	}
}

If you compile and run this program, it won't print out the flat number at all. After all, that line is now a comment. But if we want that line back again, it's easy, we just remove the // :


/* This program is pretty simple really
 although it does tell you some personal
 information about me. */
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");

		// This part demonstrates using "+" to add strings together
		System.out.println("My name is " + "Ben");  // prints my name
		System.out.println("My age is " + 19);  // prints my age
		System.out.println(1 + 1);
		System.out.println("My flat is number " + 1 + 1);  // prints my flat no.
	}
}

I hope you can see why this is better than deleting a line. Suppose we want to do the same thing with many lines of the program. We could do this:


/* This program is pretty simple really
 although it does tell you some personal
 information about me. */
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");

		// This part demonstrates using "+" to add strings together
		//System.out.println("My name is " + "Ben");  // prints my name
		//System.out.println("My age is " + 19);  // prints my age
		//System.out.println(1 + 1);
		//System.out.println("My flat is number " + 1 + 1);  // prints my flat no.
	}
}

The program now only prints Hello, world!. But why not save typing and do this:


/* This program is pretty simple really
 although it does tell you some personal
 information about me. */
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello, world!");

		// This part demonstrates using "+" to add strings together
		/*
		System.out.println("My name is " + "Ben");  // prints my name
		System.out.println("My age is " + 19);  // prints my age
		System.out.println(1 + 1);
		System.out.println("My flat is number " + 1 + 1);  // prints my flat no.
		*/
	}
}

OK, comments are way too boring to do for very long, so we'll go on to something a bit more interesting.


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