Friday, 18 April 2014

Multiplication Table using Java

Simple Multiplication Table in Java

Java Code to print Multiplication Table


//A small java program to print the Multiplication Table


import java.util.Scanner;
class MultiplicationTable
{
   public static void main(String args[])
   {
      int n, c;
      System.out.println("Enter an integer to print it's multiplication table");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      System.out.println("Multiplication table of "+n+" is :-");
      for ( c = 1 ; c <= 10 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}


Output:

        The output of the program is as shown below

Mutiplication Table


Wednesday, 16 April 2014

Lambda in Java 8

Lambda Expression in Java


Quick notes on How to use the Lambda feature in new Java


Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Java code can be improved with the inclusion of lambda expressions.

Syntax of Lambda Expression



(argument) -> (body)


A lambda expression is composed of three parts.

Argument List Arrow Token          Body
(int x, int y)             ->                     x + y

Simple Example for Lambda Expression


(int x, int y) -> x + y

() -> 42

(String s) -> { System.out.println(s); }


           The first expression takes two integer arguments, named x and y, and uses the expression form to return x+y. The second expression takes no arguments and uses the expression form to return an integer 42. The third expression takes a string and uses the block form to print the string to the console, and returns nothing.