Thursday, August 20, 2015

Java programming examples


Example 1: Display message on computer screen.

class First {
 public static void main(String[] arguments) {
 System.out.println("Let's do something using Java technology.");
 }
}

This is similar to hello world java program. Go to cmd to compile and then interpret the program using javac the inbuilt java compiler and then it will interpret it using java and then Class file name. Here you can also use First.class instead of only First in the second line just after java.

Output:




Example 2: Print integers

class Integers {
 public static void main(String[] arguments) {
 int c; //declaring a variable
 
 /* Using for loop to repeat instruction execution */
 
 for (c = 1; c <= 10; c++) {
 System.out.println(c);
 }
 }
}

Output:




If else control instructions:

class Condition {
 public static void main(String[] args) {
 boolean learning = true;
 
 if (learning) {
 System.out.println("Java programmer");
 }
 else {
 System.out.println("What are you doing here?");
 }
 }
}

Output:




Command line arguments:

class Arguments {
 public static void main(String[] args) {
 for (String t: args) {
 System.out.println(t);
 }
 }
}

Output:

0 Comments:

Post a Comment