Write a Java program to swap the first and last elements of an array and create a new array

Source Code:

public class P21 {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int temp = a[0];
a[0] = a[a.length-1];
a[a.length-1] = temp;
for(int i=0; i<a.length; i++) System.out.print(a[i] + " ");
}
}

Output: