Why does println (array) have a weird output? ("[Ljava.lang.String; @ 3e25a5")

I have a string array with four elements in it that I defined. Why, when I print System.out.println(name of Array) , does it not output the elements? But instead it gives me a strange result.

Here is my code ...

 public class GeniusTrial { public static void main(String[]args) { String [] genius = {"Einstein, ", "Newton, ", "Copernicus, ", "Kepler."}; System.out.print(genius); } } 

Here is the result I got:

 [Ljava.lang.String;@3e25a5 
+8
java string arrays
source share
6 answers

The toString() method of the array returns a String describing the identifier of the array, not its contents. This is because arrays do not override Object.toString() , whose documentation explains what you see:

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ 'character and the hexadecimal representation of the unsigned hash code of the object. In other words, this method returns a string equal to the value:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

To get a view of the String array, you can use Arrays.toString(Object[]) .

The String returned by this method consists of representing each toString() element in the order they appear in the array and is enclosed in square brackets ( "[]" ). Adjacent elements are separated by a comma and a space ( ", " ).

For example, calling this method in your array will result in the following String :

 "[Einstein, , Newton, , Copernicus, , Kepler.]" 

Note that double commas and odd distance are due to the fact that your String array element already has punctuation and a space in them. Removal:

 String [] genius = {"Einstein", "Newton", "Copernicus", "Kepler"}; 

Then the method returns this String :

 "[Einstein, Newton, Copernicus, Kepler]" 

It is important to note that using this method does not give you any control over the formatting of the resulting String . . This is useful for quickly checking the contents of an array, but it is limited beyond the scope of this goal. For example, what if you do not need those that enclose square brackets, or want to list each item in turn?

At this point, you should see the value of implementing your own method to display the contents of your array in a way that is specific to your task. As others suggested, practice this with a for loop and create a new resulting String that you want to output.

You can find more information about for loops in this Java Tutorials article . All in all, Java Tutorials are great reading for a beginner and should accompany your course well.

+16
source share

Use the Arrays class to expand the array for you:

 System.out.println(Arrays.toString(genius)); 

will provide you

[Einstein, Newton ,, Copernicus Kepler.]

Double commas are because you included them in your array; delete them and you will get a beautiful comma separated list.

+5
source share

So in your for loop it will be:

 for(i=0;i<genius.length;i++) { system.out.print(genius[i]); } 

So, I will explain what this does. The for loop is structured as follows:

 for(original value; condition for the variable to go through the for loop; what to do to variable at end of for loop) 

So, you start with i = 0 and fill in the condition: I'm less than genius.length (genius.length gives the length of the array, which in this case is 4). This way it will go through the loop and print the genius [i] (which would be a genius [0]), because i = 0. Then it will add one to i (i ++).

It will go through the loop again, because i = 1 fills the condition i am less than genius.length ..... and so on ...

He will go to i = 4 and stop. You might think, what about a genius [4]? Well, the data of the array is named as follows: 1 = arrayname [0], 2nd = arrayname [1] ..... So, the fourth will be a genius [3]. Therefore, when i = 4, it stops and everyone prints.

You can change the print format by replacing system.out.print (genius [i] + ","); This will put a comma and a space after each.

Hope this helps, good luck.

+3
source share

You need a loop:

 for(String gen : genius) { System.out.print(gen); } 

Each object has a toString() method, which is what you see is a toString string array.

+2
source share

Use advanced for loop for iteration like this

 for(String str:genius){ System.out.println(str) } 
+2
source share

You need to iterate over your array (e.g. with a for loop) and print each value separately.

If you try to print an array, it prints information about an object that you won’t be interested in.

+1
source share

All Articles