Well, it varies... |
We're here to learn about variables. Think of a variable as a little box in the computer's memory with a name, where you can store a value. It's pretty easy. In fact, it's a lot like in maths (don't cringe like that, it's not polite). Now variables have a name, as I said. There are a couple of rules for these names. A name can only contain letters, numbers and the underscore (
_
), and they can't start with a number. Why? Because numbers start with a number. It will be obvious later, trust me. Here are some example names:
Examples of names which are allowed Examples of names which are not allowed
- surname
- matric_no
- _under
- x
- y
- emailAddress
- flat2
- NOISY
- 23RobertsonsClose (it starts with a number)
- 1st_number (it starts with a number)
- 2nd_number (it starts with a number)
- n! (you're only allowed letters, numbers and underscore, not !)
- my name (spaces aren't allowed either)
The other thing about names is called camel notation. It's not a rule but it's normal to do things this way in Java. It's really easy. You start a name with a small letter. If it has two words or more, start all the other words with capitals.
Examples of Camel Notation
- x
- y
- age
- firstName
- surname
- matricNumber
- dateOfBirth
- numberOfHighersPassed
One other thing, you cannot have a variable with the same name as a keyword in Java. Keywords are words like if, else and class. If you don't know what I mean now, don't worry.
Right, that's enough rules, let's do some Java!! Get your Hello.java back to this state:
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }Now before we can do anything with a variable, we have to say that it exists:
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); int x; } }In our case the variable is called x and is of type
int
. We'll explain what exactly that means later, but for now just realise that it means that x can contain any whole number value. Now an empty box is no use for anything apart from putting stuff in, so let's put something in it:
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); int x; x = 2; } }Now that we have done something with x, let's run the program:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello Hello, world! [nurmes]btg:What happened to the two? Well nothing actually; we put it into
x
and left it there. We never told the computer to print out the value ofx
, so it didn't.Putting a value into a variable is called assignment. Just to prove that we have set the value of the variable
x
to two, let's print its value to the screen:
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); int x; x = 2; System.out.println("The value of x is " + x); } }Notice that the line we just added is much like one we saw in a previous program:
System.out.println("My age is " + 19);Just as before, the
+
operator is used to add a number on to the end of a string. The only difference in our new example is that the computer has to first look in the "box" calledx
to get out the number2
and then add it to the string"The value of x is "
.Now then, one thing about Java is that it is full of shorthand - easier ways for programmers to write things. In this example, we can combine creating
x
and assigning it the value2
into one statement:
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); int x = 2; System.out.println("The value of x is " + x); } }But the effect of doing this is exactly the same. This has reduced our program from eight lines to seven. Now that might not sound significant, but in a large program with many variables the length can be significantly reduced this way.
OK, we're done here now. Keep the program you just wrote handy because we'll be adding to it in the next lesson on Mathemagics!
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