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:

Related Posts:

  • Java static block program Java programming language offers a block known as static which is executed before main method executes. Below is the simplest example to understand f… Read More
  • Java methods Java program consists of one or more classes and a class may contain method(s). A class can do very little without methods. In this tutorial we will… Read More
  • Using multiple classes in Java program Java program can contain more than one i.e. multiple classes. Following example Java program contain two classes: Computer and Laptop. Both classes h… Read More
  • Java program to swap two numbers This java program swaps two numbers using a temporary variable. To swap numbers without using extra variable see another code below. Swapping u… Read More
  • Java while loop Java while loop is used to execute statement(s) until a condition holds true. In this tutorial we will learn looping using Java while loop examples. … Read More

0 Comments:

Post a Comment