output something
on the screen, where output should be a suitable Java a command; the something part is changeable, so that I can tell Java to output a text followed by a different text, for example:
output WEHPEX
and then
output WEHEEX
In Java the output instruction is:
System.out.println( something );
So to output the 2 pieces of text as above I have to write:
System.out.println( "WEHPEX" );
System.out.println( "WEHEEX" );
Placing on System.out.println instruction after the other has the effect that the first is executed first and the second… after that. The way the output instruction is written follows Java’s syntax: typically a command will have a name (that should be written always in the same way, for Java to understand which command I’m referring to) and some changeable part, usually written between brackets. In this case the changeable part is a piece of text and in Java’s syntax that is a sequence of characters (letters or numbers) written between 2 quotes. Usually Java commands end with a semi-column ; character, to help Java understand where each command ends and where the next one might start.
For some reasons (what I will discuss later, when the needs arises) in Java I cannot just write my commands without adding something before and after them. This is why when I create a project in NetBeans, the IDE kindly writes for me something like:
public class OxidizedPot{
public static void main(String[] args){
before the place where I will write my commands, and then it adds something like this:
}
}
after. For now I will just regard this as a given fact, the same way that I don't need to have any deeper understanding of why LEGO bricks have specific sizes and come only in certain shapes. It will become clearer as I progress and I have better grasp of the relations among my commands and general Java programming.
It is useful to notice however that whatever these 2 lines mean, the have balanced curly brackets: when a { opens, there is always one that closes, like this } . This suggests that somehow my code is sandwiched in between curly brackets. A piece of code written in between an open and closed curly bracket is called a block in Java. My program consists of a class block that contains a main block, that contains in turn my actual commands to output some text. And the most important thing I need to remember for now is that whatever command I write inside the main block will be executed by Java.
In my OxidizedPot_v0 project I also use a very important feature of programming languages: I declare and use variables. It is very much like giving a name to something, to simplify talking about it. For instance I might want to say: "Look at the male black cat that is currently in a basket near the window and tell me if it is asleep." And later: "Look at the male black cat that is currently in a basket near the window and tell me if it has a collar". If many of my questions involve that cat, it would be more convenient to give it a name, even a made-up one, and use that to speed-up our conversation:
"See that male black cat that is currently in a basket near the window? Let's call it Bob.
Can you tell me if Bob is asleep?
Can you tell me if Bob has a collar?"
Later I might change my mind and decide to call "Bob" a different cat, and it would still make sense to ask you to check if Bob (whichever cat it is at that moment) is sleeping or not.
In Java terms, a variable has a name (here it would be Bob), a value (in this case the description of the specific cat I want to talk about) and a type, usually the same as the type of its value (in the cat example Bob is a cat-type variable, and its value is a cat-type animal). I can always replace a value with a variable in my Java commands, provided the type of the variable matches that of the value. So if it is correct to write a command like this:
System.out.println( "WEHPEX" );
then it is also correct to replace the text (a value of type String, in Java) with a String-type variable:
aaa = "WEHPEX";
System.out.println( aaa );
and in this case Java will not output the text "aaa" but the value of the variable aaa, which is set to the string "WEHPEX" by the command:
aaa = "WEHPEX";
which means: please Java, give the variable aaa the value expressed by the string “WEHPEX”. This is called assigning a value to a variable.
Just to make my life more annoying, Java requires that I declare a variable before I use it, so I have to specify that aaa is not a number or another type of variable, but actually a String-type variable, that will be able to have a String-type value. In Java I do this in the following way:
String aaa;
which declares a variable with name aaa, of type String. The name of the variable is of course up to me, the programmer, and it will in most cases be more meaningful than aaa. Putting it all together, I can declare a variable, then put a value in it, output its value, change the value to a new one and output that one as well:
String aaa;
aaa = "WEHPEX";
System.out.println( aaa );
aaa = "WEHEEX";
System.out.println( aaa );
I must remember to declare my variable exactly once: in fact I cannot use a variable that I did not declare, but when a variable is declared its type is fixed and Java will not allow me to change it later in the program, so re-declaring a variable is also considered an error by Java. After all if Bob is supposed to be a cat, I can use the name for different cats, but if I suddenly decide that Bob is a tractor our conversation will rapidly become very confused. So: declaration must be done exactly once for each variable, however putting a value in the variable (aka assigning a value) can be done as many times as needed.
Another way to think about a variable is as a box that can contain different things at different times. Also, in Java I can define as many variables as I need, and the values of these variables are stored in the memory of my computer. Of course the more boxes I use, the more space they will take up in the memory (but no worries, modern computers have very large memories).
Apart from strings, in Java I can work with numbers. For example I can declare a variable of type int (which stands for integer or whole numbers) and use it to perform calculations (like finding out the area of a square, given how long its side is):
int side;
side = 10;
int square;
square = side * side;
System.out.println( square );
this program makes Java put the whole number 10 in the variable side, then Java computes the result of 10 times 10 and places the result in the variable square. Finally, Java outputs 100, which is the final value of my variable square. In this case I chose my variable names to help me read the code. Even if I did not use int-type variables in project OxidizedPot_v0, I will need both numbers and strings in the development of my game Oxidized Pot.