I want to rotate k top element of array for example:
k = 2
array before rotate --> 3 , 4 , 2
array after rotate ---> 3 , 2 , 4

 int[] temp = new int[k];

            for (int i = 0; i < k; i++) {
                if(this.stack[i]!= null){
                temp[i] = this.pop();
                }    else
                    i--;
            }
            for (int i = 0 ; i <k ; i++) {
                this.push(temp[i]);
            }

i want to implement it by stack
can any one help me to optimize it ?

Is there any particular reason you are not using the Java Stack collection? See Java Stac for some help.

I don't see the value in building your own stack collection, unless it's a homework exercise, in which case you need to struggle with it :)

also, what is the logic of your rotation? why doesn't the first element switch places?

also, what is the logic of your 'rotate'? how come the first element of the array doesn't switch places?