Pages

Sunday, June 10, 2012

Syntaxis


In this lesson we are going to learn about the syntaxis of the Java programming code. The Java code has classes, inside the classes are the methods, and inside the methods are the sentences. The sentences can be made of three basic types: declaration, conditions and actions. In the declaration we have to create the variables; the conditions decide where way of execution of the code; and the actions are were we act with the variables.

The declaration are written in the following way: first must be the data type, then the name of the variable, and at the end the semi colon. There are two kinds of declaration: for the primitive data types, and for the rest. For the primitive data types the declaration is easy, we only have to assigned the name of the variable and optionally the value. There can be multiple declaration of variables for a same data type if they are followed by comas.

Examle:
double a;
int b = 3;
byte c, d;

For the rest of the data types (arrays, objects...), the variable must be instancied. What does that mean? That the variable needs a constructor to be initialized. The value of a variable which is not instancied is null. To instancied a variable we will use the operator "new" followed by the constructor. Depending on the construtor, they can requiere some arguments. Here is an example:
Object a = new Object();
byte[] b = new byte[4];

There are some objects that can/must be instancied by a subclass because the first one is only an interface. Here is an example:
Calendar cal = new GregorianCalendar();

The conditions are the one which decides where the code will be executed, and in some cases for assigning a value to a variable. They are always inside parentheses, and return a boolean value. They can be in do-while/while loops, for loops, if conditions, and asigning a value to a boolean variable. Here are some examples:
int a = 3, b = 4;
if(a == b)
System.out.println("a is equal to b");
boolean c = (a > b); //c = false

Finally there are the actions. The actions are where the values of the variables are setted or getted, where the mathematical operations will be done... Those instructions must always finish by a semi colon. Here is an example:
int a = 3, b = 4;
a = a + 4;
System.out.println("a is equal to " + a);

Now we are going to talk about the admissible characters. There are the mathematical signs (+, -, *, /, %, =), the logial ones (>, <, ==, !=) and for the commentaries (//, /* */). Then, there are the brackets [] , braces {}, parentheses () and angle brackets <>. The brackets are used in the arrays, to define the index of the array. The braces are used to defined a block of code. This code can own to a class, method or loop. But you can also write some code inside those brackets without any reason. This is made to conserve internal variables only in this block of code. Here is an example:
int a = 3;
{
int b = 2; //this variable is exclusive of this block
a = a + b;
}
int b = 5; //this variable is not the same as the previous one
a = a + b;

And the parentheses are for the methods. There is were we can pass the arguments to the methods. The angle brackets are used to specified a data type in a collection. I will dedicate another article for those data types.

Anothers characters used in the Java code are the following ', ", \, \r, \n... Those are characters used for the texts. The ' is used for determine a signel character. Only one character can be between two '. The " define a string of characters, you can put all the characters inside. And then, there are the escape character and the special ones. The special characters (like \r: carriage return, \n: line return...) are used to make a special behaviour to the text where it is placed. And the escape character is used to print a special character in a text. The special character will never be showed in a text, they will do their own behaviour, and the escape character say that the character following this will be printed in the text. Here is an example:
char a = 'a';
String b = "Hello";
b = b + '\n';
System.out.println(b);
b = b + "this character is printed \\n";
System.out.println(b);

And this is the console output;
Hello 
Hello
this character is printed \n

No comments:

Post a Comment