Friends, please help with this question. Met this piece of code in java

public String saveComputingResult(int firstNumber, int secondNumber, String operation) { String[][] arr = new String[2][0]; //** arr[0] = new String[1]; //** arr[1] = new String[1]; //** arr[0][0] = getOperation(operation); arr[1][0] = Integer.toString(getComputingResult(firstNumber, secondNumber, operation)); return arr[0][0] + " - " + arr[1][0]; 

Who can explain the lines highlighted by the comment? A two-dimensional array of the String type is declared, it has two strings and .... 0 !!! columns. What is it like? What does this mean? There are no such examples in Schildt and Ekkele, there are only examples where the size of the array is indicated for the first array, since this is mandatory, and the second [] just remains empty, but what does it mean if there is 0 inside? And what do these lines mean in this ??

 arr[0] = new String[1]; arr[1] = new String[1]; 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Axifive

2 answers 2

An array consisting of two arrays of zero length is declared. That is, each element of the array has a pointer to the physical address where the zero-length array is stored.

When announcing:

 String[][] arr = new String[2][]; 

each element of the array is assigned a value of null instead of a pointer to an address, that is:

 arr[0]=null; arr[1]=null; 

In your case:

 String[][] arr = new String[2][0]; 

same thing as:

  String[][] arr = new String[2][]; arr[0] = new String[0]; arr[1] = new String[0]; 

Zero-length arrays are declared and stored in memory, and arr[0] and arr[1] assigned pointers to addresses in memory.

  • Why do so at all? What is the point of this? Why not just write String [] [] array = new String [2] [1]; array [0] [0] = getOperation (operation); array [1] [0] = Integer.toString (getResult (input1, input2 , operation)); - Serega Bars
  • one
    And who said that so do the right thing? The way your ad in your example only adds work to the garbage collector. Certainly more logical and correct: String[][] array = new String [2][1]; - Axifive
  • Is that better? String [] [] array = new String [2] [1]; - Serega Bars
  • one
    Yes, better. By the way, if at the start of the program no arguments are passed in the command line, the args parameter in the main method is assigned an array of zero length. Here at least one application. - Axifive
  • Thank you, Alexey! - Serega Bars

According to en-SO and common sense when writing

 String[][] arr = new String[2][0]; 

we get an array of 2 arrays of type String with zero length.

Further, in the code you provide, the cells of the array are filled with new arrays with one cell in each:

 arr[0] = new String[1]; arr[1] = new String[1]; 

In this case, because the cells are not initialized then

 if(arr[1][0]==null) System.out.println("NULL!!!111one"); 

will output NULL !!! 111one

Thus, when the cells of the arr array elements are subsequently assigned to other arrays, it does not make sense for them to put a length equal to 0 (or any other digit).

  • Thank you, Yuri !! - Serega Bars