-
class JMM112 {
-
public static void main (String[] args) {
-
int j = 0;
-
for (int i = 0; i++ < 2;) do
-
System.out.print(i);
-
while (j++ < 2);
-
}
-
}
-
Hello Sonal Kumar! Welcome to TSDN!
OK, let's go through it line after line.
Line 1: class JMM112 {
With this line you do two things:
- You define a class named "JMM112"
- You open the declaration of that class (with the bracelet)
Line 2: public static void main (String[] args) {[/i]
This line defines a function/method (void) named main. This method will be accessible for any other class (public) and without creating an object from the class (static). Also, this method has parameters: an Array (which is a type of Collection) of Strings (which are any string of characters, e.g. "Hello").
Then you open the declaration of the method (with the bracelet).
The main method has a very special meaning: It is the method, which is called, when ever you start your program. (The main method - sound's reasonable, doesn't it?) Anything, that your program should do, has to be called (directly or indirectly) in this method.
Line 3: int j = 0;
This Line defines and declares a new variable called
j with the type
integer and the value 0. The semicolon closes this command.
Line 4: for (int i = 0; i++ < 2;) do
This line is a bit complicated, as it does several things.
- You start a so called for-loop.This loop defines a variable i of the type integer and with the value 0. The loop should run, as long as i < 2 is valid and every time it finishes a run, it should increment i (= add 1 to it). This is stated in the expression i++ which is, for the moment, identical to i = i + 1; or ++i. Then comes a semicolon and an empty expression (there could be something behind the semicolon and before the bracet).
The normal way to declare this loop would be
-
for (int i = 0; i < 2; i++)
-
however both declarations do the same. - You start a so called do-while-loop. This loop will do something, then check, if the condition under which it should do so is still fulfilled and if so will repeat that.
Line 5: System.out.println(i);
For the first time in this short program, you call a function.
System.out.println will print something to the console (e.g. commandline or console). In this case, it will print the current value of the variable
i.
Line 6: while (j++ < 2);
This is the second half of the
do-while-loop. It checks, if
j < 2 and if so repeats whatever came after
do, after it has incremented the variable
j.
After this line, the brackets, which were opened previously are closed again. Every bracket, which you open within your program MUST be closed at some point.
OK, if you have more questions or have problems understanding something, just ask again! :-)
Greetings,
Nepomuk