Today end our visit in the general features of Java. We will see now how are the comments, operators, loops and arrays of language.
Comments
Java has three kinds of comments. A double slash (/ /) will cause the compiler to ignore everything that is used until the end of the line. Similarly, all that is between the characters / * and * / is also ignored by the compiler. These two forms are similar to comments in C / C + +.
If the first part of a comment starts with / **, is given a special feature of docmentação, and will use the javadoc utility, present in the JDK from Sun With the javadoc, it is possible to document an entire package of classes, it is generated an HTML file automatically from Java code. The comments should be part of the HTML text are marked, appearing between / ** and * /. You can specify additional information for each method using keywords such as stop, returns and exception (documenting parameters, return values and exceptions issued, respectively), for example. These keywords must be preceded by the @ symbol.
Operators
Working with operators in Java is very simple. Declare the following code, two integers. After the declaration, they are added together and result in a third variable.
int x, y;
x = 4;
y = 5;
int z = x + y;
The final value is 9.
Consider a table with the classification of operators:
Classification Operators
Arithmetic + - * /%
Relational Operators <>> = <= ==! = & &
!
Bitwise &
^ <<>>>>> ~ & = = ^ =
Assignment = + = -= / =% =
Assignments bitwise & = = <<=>> =>> = ^ =
Ternary operator?:
+ Increase
Decrement --
The expressions that have multiple operators are resolved according to the position where they are in a hierarchy of precedence. The traders at higher positions in the table of precedence are evaluated first. If several operators located in the same line have equal precedence, they are resolved in order from left to right. The following list shows the table of precedence:
. [] ()
+ + -! ~
* /%
+ -- <<>>>>>>
<> <=> =
==! =
& ^
& & ?: =
and other assignments
- Role bitwise
Loops
Java has three operations loop. The loop for the construction is structured as follows:
for (init; test, post-test)
The first part of the expression is any initialization that occurs at the beginning of the loop. The test is a simple or complex expression. The last part is an expression, as the increment or decrement a variable. The for loop is followed by a final statement or a block of code. For example:
for (k = 0), k <10; k + +) (
/ / .. do something
)
The above code goes in loop ten times.
A while loop is a portion of the test loop:
while (test)
The construction of the ... while performing the test and the end of the loop:
do (
/ / ... do something
) While (test)
The building is used to break out of a loop, while the continue statement jumps to the next iteration of the loop. Can also be used loops labeled, in order to control the location where the control should go in loops complex. If a label after a break statement, then the code exits the loop closest to the label that has coincidental. The following example effectively terminating both loops when the variable j is greater than 100:
int i, j;
quit: for (i = j = 0; i <100; + + i) (
for (int k = 0, k <10; k + +) (
/ / ... do something
if (j> 100)
UPS quit;
)
)
Arrays
Arrays in Java are not simply memory pointers as in C. They are much safer. You may not assign any content to an array, because Java makes sure that it is valid. This avoids many situations where there is a breach of memory access that are difficult to control. If you try to access an invalid array index, Java will launch an obvious exception, and no action is taken.
Because the arrays are Java objects, their semantics is somewhat different from their counterparts in C / C + +. First, you can not declare an array of default size, it must be declared as an uninitialized variable:
int numbers [], / / arrays for integers
Ex_String String [], / / for arrays of string objects
We can declare variables of arrays placing the brackets after the type. This method has a greater readability:
String [] ex_String;
Next, we will use the new constructor to build an object instance and create the array:
int numbers [] = new int [5] / / for integer arrays of length 5
Ex_String String [] = new String [20] / / for arrays of objects string of length 20
The previous examples create an array of integers of length 5 and an array of strings of length 20. Arrays, however, still have nothing. Each item is initialized with a default value. The integers are set by default to 0, and the string are set to null.
Add elements to the array is simple. The system starts at 0 and has integer indices. To assign the first element would be used:
ex_String [0] = "First element"
numbers [0] = 10;
Java does not multidimensional arrays. However, we can simulate them by using arrays of arrays. We can determine the array sizes with pre-determined as follows:
int k [] [] = new int [5] [4];
k [1] [3] = 999 / / assign a value to the array
This example creates an array of 4 x 5 arrays, assigned to the variable k. The second line shows that the assignment of an element to the array is simple and similar to the process of C / C + +.










