Tuesday, December 30, 2014

SOA Question 3

Oracle Mediator is a service component (Oracle SOA Suite) that provides mediation capabilities. Which of the given options is NOT true?

a) Routing
b) Transformation
c) Filtering
d) Cannot modify routing rules at runtime
e) Validation 

SOA Question 2

Services are loosely coupled and self-contained units of functionality.

a) True
b) False

Sunday, December 28, 2014

SOA Question - 1

which statement is TRUE about SOA (Service Oriented Architecture)?

a) It is an Oracle product
b) It is a Microsoft product
c) It is an IBM product
d) It is independent of any vendor, technology or product
e) It is a programming language
f)  It is a modeling language like UML

Tuesday, March 4, 2014

Which Constructors are called?

public class First {
    public First(){
        super();
        System.out.println("First... No Actually Second");
    }
}

public class Second extends First {
    public Second(){
        super();
        System.out.println("Second...No third");
    }
    public static void main(String[] args) {
        Second second= new Second();
    }
}

Will this code run? Answer to that is YES.

OUTPUT : 

First... No Actually Second
Second...No third

Why highlighted statement super() in First class does not give an error?

As we create an object of Subclass'Second', constructors are called in the order of creation of classes. Second() makes call to First() before it executes the instructions in Second(). First() makes a call to its Super class constructor Object()  which is Universal Super class of all Java Objects.

Saturday, February 22, 2014

Date and Time --- Simple Arithmetic

/* Program to add and subtract days and Hours. It can be applied for other fields of date and time too */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;
import java.util.Calendar;


public class DateTimeUtility {
    public static void main(String[] args) {
        int addDays = 4;
        int subtractDays = 10;
        int addHours=3;
        int subtractHours=26;
     
        Calendar cal = Calendar.getInstance();
        //System.out.println(cal);
        //Current date
        System.out.println("Today's date : " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
   
        //Current Time
        System.out.println("Current Time  : " + (cal.get(Calendar.HOUR) ) +
                ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));

        //Adding days
        cal.add(Calendar.DATE, addDays);
        System.out.println("Date (after): " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
        cal = Calendar.getInstance();

        //subtracting days
        cal.add(Calendar.DATE, -subtractDays);
        System.out.println("Date (before): " + (cal.get(Calendar.MONTH) + 1) +
               "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));

       //Adding Hours
        cal.add(Calendar.HOUR, addHours);
        System.out.println("New Time after adding " + addHours +" Hours "+
                cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
        cal=Calendar.getInstance();

       //Subtracting Hours
        cal.add(Calendar.HOUR, -subtractHours);
        System.out.println("Time before " + subtractHours+" Hours "+
                (cal.get(Calendar.HOUR) ) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
    }
}

OUTPUT :

Today's date : 2/22/2014
Current Time  : 6:22:6
Date (after): 2/26/2014
Date (before): 2/12/2014
New Time after adding 3 Hours 9:22:6
Time before 26 Hours 4:22:6

Thursday, January 30, 2014

Something about Overloading...

Overloading allows to use same name of method for multiple methods provided signatures are different. Signatures can be different in number, in type or in order. Example below shows.

public class OverloadDemo {
    public int add(int x, int y){
        return x+y;
    }
    public int add(int x, int y, int z){
        return x+y+z;
    }

    public double add(int x, double y){
        return x+y;
    }

    public double add(double x, int y){
        return x+y;
    }

    public static void main(String[] args) {
        OverloadDemo obj = new OverloadDemo();

        System.out.println(obj.add(10,20));
        System.out.println(obj.add(10,20,56));
        System.out.println(obj.add(10,20.9));
        System.out.println(obj.add(10.7,20));
    }

}

Output :

run:
30
86
30.9
30.7

It is simple. It is a user-friendly feature of Java and all Object-Oriented Programming languages.

Here is a small twist. See the code ...

public class AnotherOverload {
 
    int add (int x, int y){
        System.out.println("Int Overload");
        return x+y;
    }
    int add (short x, short y){
        System.out.println("Short Overload");
        return x+y;
    }
    long add(long x, long y){
        System.out.println("Long overload");
        return x+y;
    }
    double add(double x, double y){
        System.out.println("double overload");
        return x+y;
    }
    float add(float x, float y){
        System.out.println("double overload");
        return x+y;
    }
    public static void main(String[] args) {
        AnotherOverload ob = new AnotherOverload();
        System.out.println("sum of 10 + 20 is "+ob.add(10,20));
        System.out.println("sum of 10.1 + 20.1 is "+ob.add(10.1,20.2));
    }
}
Output:

Int Overload
sum of 10 + 20 is 30
double overload
sum of 10.1 + 20.1 is 30.299999999999997

There are two Questions over here :

10 and 20 are short type values according to the range, rather I can say it falls in the range of byte. But the method is called with an int parameters. Likewise 10.1 and 20.1 are float values but the call is for double. All non-real numeric literals by default are int and all real literals are double.

Here is another interesting scenario...

class IntOverload {
public static void main(String args[]) {
int x=10,y=20;
                short a=5, b=7;

IntOverload ob = new IntOverload();
System.out.println(ob.add(x,y));
        System.out.println(ob.add(a,b));
        
}
        
        short add(short a, short b){
            System.out.println("Short");
            return a+b;
        }

long add(long a, long b){
System.out.println("long");
return a+b;
}

float add(float a, float b){
System.out.println("float");
return a+b;
}

double add(double a, double b){
System.out.println("double");
return a+b;
}
}

This code does not compile and give an error :

When add method is called with explicit short type, it gives compile time error "Required :short, found:int".

All integer type numerics are promoted to int type at the time of computation. So the solution can be achieved by changing the method code. We use explicit cast... just a word of caution when overloading...

 short add(short a, short b){
            System.out.println("Short");
            return (short)(a+b);
        }

Enjoy coding... :)

Monday, January 27, 2014

Understanding final keyword part - 2 Just a small tidbit... 'njoy java

Now, here is an interesting scenario for you, a simple mind excercise.

Java understands final lil different from constant. Say I create a final array with the following code.

public class FinalArray1 {
    private final int[] arr = new int[5];
   
    public FinalArray1(){
        for(int i=0; i<arr.length;i++){
            arr[i]= (int)(Math.random()*25);
        }
    }
    public void printArray(){
        System.out.print("[ ");
        for(int i=0;i<arr.length;i++){
             System.out.print(arr[i]+" ");
        }
        System.out.println("]");
    }
    public void setNthElement(int index,int value){
        if(index<arr.length){
            arr[index]=value;
        }
    }
}

To test whether array is final or not, here is a Tester class:

public class TestFinalArray1 {
    public static void main(String[] args) {
        FinalArray1 arrayObj = new FinalArray1();
        arrayObj.printArray();
        arrayObj.setNthElement(2, (int)(Math.random()*100));
        arrayObj.printArray();
    }
}

On execution, we can see the data of 3rd element is changed.

run:
[ 11 15 11 10 11 ]
[ 11 15 30 10 11 ]
BUILD SUCCESSFUL (total time: 1 second)

So in what sense is it constant? let us take a look at this. We add newArray(int size) method to FinalArray1 class. 

public void newArray(int size){
        arr= new int[size];
    }



we can see an error on compile : "cannot assign a value to final variable arr". arr array reference variable is final and not the array elements.


Thursday, January 23, 2014

What is Wrong?

public class TestFinalVar {
    private final int MAX_CAPACITY;
 
    public TestFinalVar(){
        MAX_CAPACITY=100;
    }
    public TestFinalVar(int capacity){
        this();
        MAX_CAPACITY=capacity;
    }
 
}

The code does not compile. What do you think is NOT Right?

My Answer : after 2 days

Posted by :
Nancy @ 2:29 pm 1/23/2014

Friday, January 17, 2014

Why this code does not compile successfully?

//JustAnInterface.java

public interface JustAnInterface {
     void testIt();
}

//JustAClass.java
public class JustAClass implements JustAnInterface{
    void testIt(String msg){
        System.out.println(msg);
    }

    void testIt(){
       System.out.println("I am from an Interface");
    }
}

If you know the reason : type in comments

Wait for my answer : I'll post it as a comment

By Nancy
@ 6:10 am 1/18/2014


Tuesday, January 7, 2014

Can you explain it?

+ operator behaves in an interesting manner when used with a combination of String and numbers.

Case 1:

public class InterestingClass {
    public static void main(String[] args) {
        int x=25;
        int y=12;
     
        System.out.println("sum is "+x+y);
    }  
}

OUTPUT:

sum is 2512

Case 2:

public class InterestingClass {
    public static void main(String[] args) {
        int x=25;
        int y=12;
     
        System.out.println("sum is "+(x+y));
    }  
}

OUTPUT:

sum is 37

Case 3:

public class InterestingClass {
    public static void main(String[] args) {
        int x=25;
        int y=12;
     
        System.out.println(x+y+" is sum");
    }  
}

OUTPUT:

37 is sum

Reason:

In Case 1, System.out.println("sum is "+x+y); uses println(String x) method of PrintStream class. Statement "sum is"+x+y is processed from left to right. So it is String+number = String. Hence the output is sum is 2512. Numbers are concatenated with the string.

In Case 2, (x+y) are in brackets, hence bracketed expression is processed before concatenation. Hence, result x+y which is 37 is concatenated with the string "sum is ".

In Case 3, System.out.println(x+y+" is sum"); Expression is processed from left to right, x+y, both are number, it adds the numbers and expression is later evaluated as number+String, which results in 37 is sum.

Enjoy Java :)

Posted by Nancy
@12:33 am 01/08/2014 

Sunday, January 5, 2014

Can I call main() from another class?

Can I call main() from another class? Simple answer is “YES”. Here is a simple example.

public class SimpleStarter {
   
    public static void main(String[] args) {
        System.out.println("Main of SimpleStarter class");
    }
  }

Now, we would like to call main() method of SimpleStarter class from another class. As main() is a static method, we can call main by using class name.
public class MainCallerClass {
    public static void main(String[] args) {
        System.out.println("Main of MainCallerClass");
        SimpleStarter.main(args);
    }   
}

When we execute MainCallerClass, here is what we see as output :

Main of MainCallerClass
Main of SimpleStarter class

Posted  by Nancy
@10:12 pm 01/05/2013




Saturday, January 4, 2014

final in Java

Keyword final is used to represent constants. Final is a versatile in a way that it can be used with variable, method and class. To be more precise it can be applied to instance variables, local variables, instance methods, class methods and class itself.

Final Variable

Final variable is a constant and can be assigned value only once. If we need to declare a constant at instance level i.e. a constant pertaining to an individual instance, we can create final variable at instance level.
We can create an uninitialized final instance. Later it can be initialized in variety of ways.
  1. We can initialize it at the time of declaration.

      public class FinalVariableDemo {
           private final int TARGET=52;
           private int achieved;
     }
  
     2.       We can initialize it in the constructor method. Variable will be initialized when an instance is created.

       public class FinalVariableDemo {
            private final int TARGET;
            private int achieved;
            public FinalVariableDemo(){
                 TARGET=25;
           }
           public FinalVariableDemo(int target){
                 this();
                 this.TARGET=target;
         }
    }

      We need to be careful to initialize final variable in one of the constructors and not in both IF one constructor is making a call to another constructor.

        public FinalVariableDemo(int target){
                this();
                this.TARGET=target; // this line is a compiler error
        }

       Java compiler will not allow it and will flag compiler error.

  1. Another place where final variable can be initialized is an anonymous block in the class.
public class FinalVariableDemo {
    private final int TARGET;
    private int achieved;
    {
        TARGET=50;
    }
}
Anonymous block is executed before the constructor is called. If we want to initialize final variable in an anonymous block, we should not initialize it in the constructor.

Note : final instance variable must be initialized before object creation.

public class FinalVariableDemo {
    private final int TARGET;
    private int achieved;
    public static void main(String[] args) {
        FinalVariableDemo anInstance= new FinalVariableDemo();
        anInstance.TARGET=34; // this is an error
    }

Whereas Local final variables can be initialized at the time of declaration.

public class LocalFinalVar {
    public static void main(String args[]){
        final int X=25;
       System.out.println("x ="+X);
  }
}

Or it can be initialized just before its first use.

public class LocalFinalVar {
    public static void main(String args[]){
        final int X;
        X=25;
       System.out.println("x ="+X);
    }
}
                                                                                                                                              ...to be contd

By Nancy
@11:27 am 01/05/2014