JAVA performance advantages in an anonymous object analysis

Anonymous array as an example

A.
In java, it provides a way to create an array of objects and at the same time gives the initial value of a simplified written form, indicating that we declare an array.

int[] arr = {13,24,45,67};

The wording does not need to be created by new.
We compare such an approach and our most common writing about:

int[] arr = {13,24,45,67};
System.out.println(arr[0]+"---"+arr[1]+"---"+arr[2]+"---"+arr[3]);
int[] array = new int[]{13,24,45,67};
System.out.println(array[0]+"---"+array[1]+"---"+array[2]+"---"+array[3]);

The results obtained in FIG:
Here Insert Picture Description
found that both methods can be output inside the array elements. These two statements illustrate the same method array.


II
can initialize an anonymous array:

new int[] {14,23,45,67}

This form we can use this:

  1. Declare a return type called static get () is a function of type int array and returns an anonymous array.
public static int[] get(){
        return new int[] {14,23 45,67};
    }
  1. Declare an array call get () function is the main function, and each element of the output array
int[] bc = get();
for(int i = 0 ; i < bc.length ; i++){
    System.out.println(bc[i]);
}
  1. The following results were obtained:
    Here Insert Picture Description
    This method represents a new array will be created and initialized with the values provided in brackets, the size of the array is the number of the initial value.
    And we will be anonymous array as the return value return, we declare an arbitrary function in the main array can receive anonymous array, is not it a lot easier than every time a new array?
int[] arr1 = get();
System.out.println(arr1[0]+"---"+arr1[1]+"---"+arr1[2]+"---"+arr1[3]);
int[] arr2 = get();
System.out.println(arr2[0]+"---"+arr2[1]+"---"+arr2[2]+"---"+arr2[3]);
int[] arr3 = get();
System.out.println(arr3[0]+"---"+arr3[1]+"---"+arr3[2]+"---"+arr3[3]);
int[] arr4 = get();
System.out.println(arr4[0]+"---"+arr4[1]+"---"+arr4[2]+"---"+arr4[3]);

Get results:
Here Insert Picture Description
The results show that our argument is also feasible.


For the title of an anonymous object for the same reason
we want to achieve a method method (Student stu), need to pass an object as a parameter, this time we also have two ways:
1. Declare a variable and initialize
Student stu = new Student ( );
method (STU);
2. declare an anonymous object new a (method () is anonymous object)
method (new new Student ());
create a method method in the class, written in the Student category get, set method.

public static void method(Student stu){
       stu.setName("张三");
       System.out.println(stu.getName());
   }

In the main function Input:

method(new Student());

We can see that we output a "John Doe" in the console.
Here Insert Picture Description


Summary:
anonymous array syntax This saves us space in the stack allocation without a handle is created (a new variable)
so we omit a series of stacks on the right, we reduce the amount of code, the most important enhance the performance of the program.

Guess you like

Origin blog.csdn.net/fight252/article/details/90744873