Friday, July 4, 2008

JAVA-5

Increment and decrement operators:-
Java has ++ and -- operators like C. class Count {
public static void main (String args[]) {
for (int i = 0; i < 50; i++) {
System.out.println(i);
}

}

}
decrement operators class Count {
public static void main (String args[]) {
for (int i = 50; i > 0; i--) {
System.out.println(i);
}

}

}


Print statements:-
class PrintArgs {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}

}

}
$ java PrintArgs Hello there!
Hello
there!
System.out.println() prints its arguments followed by a platform dependent line separator (carriage return (ASCII 13, \r) and a linefeed (ASCII 10, \n) on Windows, linefeed on Unix, carriage return on the Mac)
System.err.println() prints on standard err instead.
You can concatenate arguments to println() with a plus sign (+), e.g.
System.out.println("There are " + args.length + " command line arguments");
Using print() instead of println() does not break the line. For example, System.out.print("There are ");
System.out.print(args.length);
System.out.print(" command line arguments");
System.out.println();
System.out.println() breaks the line and flushes the output. In general nothing will actually appear on the screen until there's a line break character


Fibonacci Numbers:-
class Fibonacci {
public static void main (String args[]) {

int low = 0;
int high = 1;

while (high < 50) {
System.out.println(high);
int temp = high;
high = high + low;
low = temp;
}

}

}
Example
Addition
while loop
Relations
Variable declarations and assignments

No comments: