What does this new code String [] and String.value () mean?

I am doing android development and I ran into this:

       String[] strArray = new String[]{
           String.valueOf(1)
      };

      

Why String is instantiated as an object, and what

valueOf(1)

      

really installed here?

+3


source to share


3 answers


It:

String[] strArray = new String[]{
    // ...
};

      

... creates an array String

and initializes it with the entries in the part {...}

. In your case, there is only one entry.



Why String is instantiated as an object, and what

valueOf(1);

As the documentation will tell you , String.valueOf(int)

creates a string representation of the integer value you are passing in.

So, what does this code creates an array String

with one record "1"

. It is unclear why the author would write String.valueOf(1)

and not just "1"

.

+2


source


String.valueOf(...)

      

- a method that parses an object or primitive and creates a string representation of that object. For example, integer 1 will be converted to the string "1" (as in your example).




String[] strArray = new String[]{
       String.valueOf(1)
  };

      

means we create an array of strings (basically an array containing multiple strings) and add a string containing "1" to it (see my explanation above).

+1


source


The code snippet in the question looks wrong. and I hope this should be next.

  String[] s= new String[]{
               String.valueOf(1)
          };

      

String.valueOf (1) "Returns the string representation of the int argument."

values โ€‹โ€‹returned from "String.valueOf (1)" is assigned to the string array object. The value "1" will be stored in the 0th index.

I am a bit confused as to why the question is marked with a voice. If the question is wrong, we can edit and change, right?

0


source







All Articles