Saturday, 6 February 2016

Features of Java 8 Version

Java 8 Enhanced Features


                Java is the world number one development platform and Java 8 is the latest version available for the revolutionary programming language. Java 8 includes features for productivity, improved polyglot programming, ease of use, performance and improved security.

Java 8 Features

                 Here we can see few features added to the Java 8 Version.


  • Lamda Expressions
  • Virtual Extension Methods
  • Date and Time API'
  • Nashhorn Javascript Engine
  • Improved Security
  • Parallel Operations
  • Type Annotations



Friday, 5 February 2016

Java program to open mspaint window

How to open MS Paint using Java Code


              We can open the Microsoft Paint using the Java program listed below

Program:


//A small java program to open Microsoft Paint window on the screen.
import java.util.*;
import java.io.*;

class MSPaint {
  public static void main(String[] args) {
    Runtime rs = Runtime.getRuntime();

    try {
      rs.exec("mspaint");
    }
    catch (IOException e) {
      System.out.println(e);
    }  
  }
}

Output:




Call by reference and Call by value

Java uses call by value. That means all parameters to methods are pass by value or call by value,the method gets a copy of all parameter values and the method cannot modify the contents of any parameter variables that are passed to it.
Please see sample programs below.

call by value: 
class One
{
void display(int i,int j)
{
i*=4;
j/=4;
}  
}
class Two
{
public static void main(String as[])
{
int a=10;
int b=20;
System.out.println("before calling");
System.out.println("a="+a);
System.out.println("b="+b);
One sample=new One();
sample.display(a,b);
System.out.println("after calling");
System.out.println("a="+a);
System.out.println("b="+b);
}
}
OUTPUT:
before calling
a=10
b=20
after calling
a=10
b=20

call by reference:
class One
{
int a,b;
One(int i,int j)
{
a=i;
b=j;
}
void display(One ab) //pass an object
{
ab.a*=2;
ab.b/=2;
}
}

class Two
{
public static void main(String as[])
{
One sample=new One(10,20);
System.out.println("before calling");
System.out.println("a="+sample.a);
System.out.println("b="+sample.b);
sample.display(ab);
System.out.println("after calling");
System.out.println("a="+sample.a);
System.out.println("b="+sample.b);
}
}

Output:
before calling
a=10
b=20
after calling
a=20
b=10

By above example programs hope understood the difference between call by value and call by reference.


Tuesday, 2 February 2016

Constructors in Java

Java Constructors


               Constructors in Java is a special type of method that is used to initialize an object.

               In other words, Constructor in Java is a bit of code that allows you to create objects from a class. 'New' is the keyword used to call a constructor.

Constructors In Java
Constructors In Java

Rules for Constructors in Java


  1. Constructor name should match the name of the class it resides.
  2. Constructor must not have a return type. 
  3. If we do not manually include a constructor, a default constructor will be automatically generated by the compiler in Java. 
  4. Constructors invoked implicitly. i.e, invoked using the keyword 'new'.
  5. Constructors in Java cannot be abstract, static, final or synchronized. 

Example for Default Constructor


class DemoConstructor
{
     public DemoConstructor()
     {
         System.out.println("Default constructor");
     }
}

Example for Parameterized Constructor


class DemoConstructor
{
      public DemoConstructor(int num, String str)
      {
           System.out.println("Parameterized constructor");
      }
}